blob: a3645c190ef292bd20ef597c9c26e92970b6e5bd [file] [log] [blame]
Radek Krejcid91dbaf2018-09-21 15:51:39 +02001/**
2 * @file xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskob36053d2020-03-26 15:49:30 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcid91dbaf2018-09-21 15:51:39 +02005 * @brief Generic XML parser implementation for libyang
6 *
7 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
8 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
17
18#include "xml.h"
Radek Krejci4b74d5e2018-09-26 14:30:55 +020019
Radek Krejcib1890642018-10-03 14:05:40 +020020#include <assert.h>
Radek Krejci7a7fa902018-09-25 17:08:21 +020021#include <ctype.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020022#include <stdint.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdlib.h>
Radek Krejci4b74d5e2018-09-26 14:30:55 +020024#include <string.h>
Radek Krejcica376bd2020-06-11 16:04:06 +020025#include <sys/types.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "dict.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020030#include "parser_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "printer.h"
32#include "tree.h"
33#include "tree_data.h"
Radek Krejcid91dbaf2018-09-21 15:51:39 +020034
Michal Vaskob36053d2020-03-26 15:49:30 +010035/* Move input p by s characters, if EOF log with lyxml_ctx c */
Michal Vasko63f3d842020-07-08 10:10:14 +020036#define move_input(c,s) ly_in_skip(c->in, s); LY_CHECK_ERR_RET(!c->in->current[0], LOGVAL(c->ctx, LY_VLOG_LINE, &c->line, 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 */
Michal Vasko63f3d842020-07-08 10:10:14 +020039#define ign_xmlws(c) while (is_xmlws(*(c)->in->current)) {if (*(c)->in->current == '\n') {++c->line;} ly_in_skip(c->in, 1);}
Michal Vaskob36053d2020-03-26 15:49:30 +010040
41static LY_ERR lyxml_next_attr_content(struct lyxml_ctx *xmlctx, const char **value, size_t *value_len, int *ws_only,
42 int *dynamic);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020043
Radek Krejci4b74d5e2018-09-26 14:30:55 +020044/**
45 * @brief Ignore any characters until the delim of the size delim_len is read
46 *
47 * Detects number of read new lines.
Michal Vasko63f3d842020-07-08 10:10:14 +020048 * Returns 0 if delim was found, non-zero if was not.
49 */
50static int
51ign_todelim(register const char *input, const char *delim, size_t delim_len, size_t *newlines, size_t *parsed)
Radek Krejcid91dbaf2018-09-21 15:51:39 +020052{
53 size_t i;
54 register const char *a, *b;
55
56 (*newlines) = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +020057 (*parsed) = 0;
58 for ( ; *input; ++input, ++(*parsed)) {
Radek Krejcid91dbaf2018-09-21 15:51:39 +020059 if (*input != *delim) {
60 if (*input == '\n') {
61 ++(*newlines);
62 }
63 continue;
64 }
65 a = input;
66 b = delim;
67 for (i = 0; i < delim_len; ++i) {
68 if (*a++ != *b++) {
69 break;
70 }
71 }
72 if (i == delim_len) {
Michal Vasko63f3d842020-07-08 10:10:14 +020073 /* delim found */
74 return 0;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020075 }
76 }
Michal Vasko63f3d842020-07-08 10:10:14 +020077
78 /* delim not found */
79 return -1;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020080}
81
Radek Krejci4b74d5e2018-09-26 14:30:55 +020082/**
Michal Vaskob36053d2020-03-26 15:49:30 +010083 * @brief Check/Get an XML identifier from the input string.
84 *
85 * The identifier must have at least one valid character complying the name start character constraints.
86 * The identifier is terminated by the first character, which does not comply to the name character constraints.
87 *
88 * See https://www.w3.org/TR/xml-names/#NT-NCName
89 *
90 * @param[in] xmlctx XML context.
91 * @param[out] start Pointer to the start of the identifier.
92 * @param[out] end Pointer ot the end of the identifier.
93 * @return LY_ERR value.
94 */
95static LY_ERR
96lyxml_parse_identifier(struct lyxml_ctx *xmlctx, const char **start, const char **end)
97{
98 const char *s, *in;
99 uint32_t c;
100 size_t parsed;
101 LY_ERR rc;
102
Michal Vasko63f3d842020-07-08 10:10:14 +0200103 in = s = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100104
105 /* check NameStartChar (minus colon) */
106 LY_CHECK_ERR_RET(ly_getutf8(&in, &c, &parsed),
107 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INCHAR, in[0]),
108 LY_EVALID);
109 LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c),
110 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
111 "Identifier \"%s\" starts with an invalid character.", in - parsed),
112 LY_EVALID);
113
114 /* check rest of the identifier */
115 do {
116 /* move only successfully parsed bytes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200117 ly_in_skip(xmlctx->in, parsed);
Michal Vaskob36053d2020-03-26 15:49:30 +0100118
119 rc = ly_getutf8(&in, &c, &parsed);
120 LY_CHECK_ERR_RET(rc, LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INCHAR, in[0]), LY_EVALID);
121 } while (is_xmlqnamechar(c));
122
123 *start = s;
Michal Vasko63f3d842020-07-08 10:10:14 +0200124 *end = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100125 return LY_SUCCESS;
126}
127
128/**
129 * @brief Add namespace definition into XML context.
130 *
131 * Namespaces from a single element are supposed to be added sequentially together (not interleaved by a namespace from other
132 * element). This mimic namespace visibility, since the namespace defined in element E is not visible from its parents or
133 * siblings. On the other hand, namespace from a parent element can be redefined in a child element. This is also reflected
134 * by lyxml_ns_get() which returns the most recent namespace definition for the given prefix.
135 *
136 * When leaving processing of a subtree of some element (after it is removed from xmlctx->elements), caller is supposed to call
137 * lyxml_ns_rm() to remove all the namespaces defined in such an element from the context.
138 *
139 * @param[in] xmlctx XML context to work with.
140 * @param[in] prefix Pointer to the namespace prefix. Can be NULL for default namespace.
141 * @param[in] prefix_len Length of the prefix.
142 * @param[in] uri Namespace URI (value) to store directly. Value is always spent.
143 * @return LY_ERR values.
144 */
145LY_ERR
146lyxml_ns_add(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, char *uri)
147{
148 struct lyxml_ns *ns;
149
150 ns = malloc(sizeof *ns);
151 LY_CHECK_ERR_RET(!ns, LOGMEM(xmlctx->ctx), LY_EMEM);
152
153 /* we need to connect the depth of the element where the namespace is defined with the
154 * namespace record to be able to maintain (remove) the record when the parser leaves
155 * (to its sibling or back to the parent) the element where the namespace was defined */
156 ns->depth = xmlctx->elements.count;
157
158 ns->uri = uri;
159 if (prefix) {
160 ns->prefix = strndup(prefix, prefix_len);
161 LY_CHECK_ERR_RET(!ns->prefix, LOGMEM(xmlctx->ctx); free(ns->uri); free(ns), LY_EMEM);
162 } else {
163 ns->prefix = NULL;
164 }
165
166 LY_CHECK_ERR_RET(ly_set_add(&xmlctx->ns, ns, LY_SET_OPT_USEASLIST) == -1,
167 free(ns->prefix); free(ns->uri); free(ns), LY_EMEM);
168 return LY_SUCCESS;
169}
170
171/**
172 * @brief Remove all the namespaces defined in the element recently closed (removed from the xmlctx->elements).
173 *
174 * @param[in] xmlctx XML context to work with.
175 */
176void
177lyxml_ns_rm(struct lyxml_ctx *xmlctx)
178{
179 unsigned int u;
180
181 for (u = xmlctx->ns.count - 1; u + 1 > 0; --u) {
182 if (((struct lyxml_ns *)xmlctx->ns.objs[u])->depth != xmlctx->elements.count + 1) {
183 /* we are done, the namespaces from a single element are supposed to be together */
184 break;
185 }
186 /* remove the ns structure */
187 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix);
188 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri);
189 free(xmlctx->ns.objs[u]);
190 --xmlctx->ns.count;
191 }
192
193 if (!xmlctx->ns.count) {
194 /* cleanup the xmlctx's namespaces storage */
195 ly_set_erase(&xmlctx->ns, NULL);
196 }
197}
198
Michal Vaskob36053d2020-03-26 15:49:30 +0100199const struct lyxml_ns *
200lyxml_ns_get(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len)
201{
202 unsigned int u;
203 struct lyxml_ns *ns;
204
205 for (u = xmlctx->ns.count - 1; u + 1 > 0; --u) {
206 ns = (struct lyxml_ns *)xmlctx->ns.objs[u];
207 if (prefix && prefix_len) {
208 if (ns->prefix && !ly_strncmp(ns->prefix, prefix, prefix_len)) {
209 return ns;
210 }
211 } else if (!ns->prefix) {
212 /* default namespace */
213 return ns;
214 }
215 }
216
217 return NULL;
218}
219
Michal Vasko8cef5232020-06-15 17:59:47 +0200220/**
221 * @brief Skip in the input until EOF or just after the opening tag.
222 * Handles special XML constructs (comment, cdata, doctype).
223 *
224 * @param[in] xmlctx XML context to use.
225 * @return LY_ERR value.
226 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100227static LY_ERR
228lyxml_skip_until_end_or_after_otag(struct lyxml_ctx *xmlctx)
229{
230 const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */
Michal Vasko63f3d842020-07-08 10:10:14 +0200231 const char *endtag, *sectname;
232 size_t endtag_len, newlines, parsed;
233 int rc;
Michal Vaskob36053d2020-03-26 15:49:30 +0100234
235 while (1) {
236 ign_xmlws(xmlctx);
237
Michal Vasko63f3d842020-07-08 10:10:14 +0200238 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100239 /* EOF */
240 if (xmlctx->elements.count) {
241 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
242 return LY_EVALID;
243 }
244 return LY_SUCCESS;
Michal Vasko63f3d842020-07-08 10:10:14 +0200245 } else if (xmlctx->in->current[0] != '<') {
246 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
247 xmlctx->in->current, "element tag start ('<')");
Michal Vaskob36053d2020-03-26 15:49:30 +0100248 return LY_EVALID;
249 }
250 move_input(xmlctx, 1);
251
Michal Vasko63f3d842020-07-08 10:10:14 +0200252 if (xmlctx->in->current[0] == '!') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100253 move_input(xmlctx, 1);
254 /* sections to ignore */
Michal Vasko63f3d842020-07-08 10:10:14 +0200255 if (!strncmp(xmlctx->in->current, "--", 2)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100256 /* comment */
257 move_input(xmlctx, 2);
258 sectname = "Comment";
259 endtag = "-->";
260 endtag_len = 3;
Michal Vasko63f3d842020-07-08 10:10:14 +0200261 } else if (!strncmp(xmlctx->in->current, "[CDATA[", 7)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100262 /* CDATA section */
263 move_input(xmlctx, 7);
264 sectname = "CData";
265 endtag = "]]>";
266 endtag_len = 3;
Michal Vasko63f3d842020-07-08 10:10:14 +0200267 } else if (!strncmp(xmlctx->in->current, "DOCTYPE", 7)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100268 /* Document type declaration - not supported */
269 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_NSUPP, "Document Type Declaration");
270 return LY_EVALID;
271 } else {
Michal Vasko63f3d842020-07-08 10:10:14 +0200272 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Unknown XML section \"%.20s\".",
273 &xmlctx->in->current[-2]);
Michal Vaskob36053d2020-03-26 15:49:30 +0100274 return LY_EVALID;
275 }
Michal Vasko63f3d842020-07-08 10:10:14 +0200276 rc = ign_todelim(xmlctx->in->current, endtag, endtag_len, &newlines, &parsed);
277 LY_CHECK_ERR_RET(rc, LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_NTERM, sectname), LY_EVALID);
Michal Vaskob36053d2020-03-26 15:49:30 +0100278 xmlctx->line += newlines;
Michal Vasko63f3d842020-07-08 10:10:14 +0200279 ly_in_skip(xmlctx->in, parsed + endtag_len);
280 } else if (xmlctx->in->current[0] == '?') {
281 rc = ign_todelim(xmlctx->in->current, "?>", 2, &newlines, &parsed);
282 LY_CHECK_ERR_RET(rc, LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
Michal Vaskob36053d2020-03-26 15:49:30 +0100283 xmlctx->line += newlines;
Michal Vasko63f3d842020-07-08 10:10:14 +0200284 ly_in_skip(xmlctx->in, parsed + 2);
Michal Vaskob36053d2020-03-26 15:49:30 +0100285 } else {
286 /* other non-WS character */
287 break;
288 }
289 }
290
291 return LY_SUCCESS;
292}
293
Michal Vasko8cef5232020-06-15 17:59:47 +0200294/**
295 * @brief Parse QName.
296 *
297 * @param[in] xmlctx XML context to use.
298 * @param[out] prefix Parsed prefix, may be NULL.
299 * @param[out] prefix_len Length of @p prefix.
300 * @param[out] name Parsed name.
301 * @param[out] name_len Length of @p name.
302 * @return LY_ERR value.
303 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100304static LY_ERR
305lyxml_parse_qname(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
306{
307 const char *start, *end;
308
309 *prefix = NULL;
310 *prefix_len = 0;
311
312 LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end));
313 if (end[0] == ':') {
314 /* we have prefixed identifier */
315 *prefix = start;
316 *prefix_len = end - start;
317
318 move_input(xmlctx, 1);
319 LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end));
320 }
321
322 *name = start;
323 *name_len = end - start;
324 return LY_SUCCESS;
325}
326
327/**
Michal Vasko8cef5232020-06-15 17:59:47 +0200328 * @brief Parse XML text content (value).
329 *
330 * @param[in] xmlctx XML context to use.
331 * @param[in] endchar Expected character to mark value end.
332 * @param[out] value Parsed value.
333 * @param[out] length Length of @p value.
334 * @param[out] ws_only Whether the value is empty/white-spaces only.
335 * @param[out] dynamic Whether the value was dynamically allocated.
336 * @return LY_ERR value.
337 */
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200338static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100339lyxml_parse_value(struct lyxml_ctx *xmlctx, char endchar, char **value, size_t *length, int *ws_only, int *dynamic)
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200340{
Michal Vaskob36053d2020-03-26 15:49:30 +0100341#define BUFSIZE 24
342#define BUFSIZE_STEP 128
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200343
Michal Vaskob36053d2020-03-26 15:49:30 +0100344 const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */
Michal Vasko63f3d842020-07-08 10:10:14 +0200345 const char *in = xmlctx->in->current, *start;
Michal Vaskob36053d2020-03-26 15:49:30 +0100346 char *buf = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +0200347 size_t offset; /* read offset in input buffer */
348 size_t len; /* length of the output string (write offset in output buffer) */
349 size_t size = 0; /* size of the output buffer */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200350 void *p;
Radek Krejci117d2082018-09-26 10:05:14 +0200351 uint32_t n;
Michal Vaskob36053d2020-03-26 15:49:30 +0100352 size_t u;
353 int ws = 1;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200354
Michal Vaskob36053d2020-03-26 15:49:30 +0100355 assert(xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +0200356
Radek Krejcid70d1072018-10-09 14:20:47 +0200357 /* init */
Michal Vaskob36053d2020-03-26 15:49:30 +0100358 start = in;
Radek Krejcid70d1072018-10-09 14:20:47 +0200359 offset = len = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200360
361 /* parse */
362 while (in[offset]) {
363 if (in[offset] == '&') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100364 /* non WS */
365 ws = 0;
Radek Krejcid70d1072018-10-09 14:20:47 +0200366
Michal Vaskob36053d2020-03-26 15:49:30 +0100367 if (!buf) {
368 /* prepare output buffer */
369 buf = malloc(BUFSIZE);
370 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
371 size = BUFSIZE;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200372 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100373
374 /* allocate enough for the offset and next character,
375 * we will need 4 bytes at most since we support only the predefined
376 * (one-char) entities and character references */
Juraj Vijtiukcb017cc2020-07-08 16:19:58 +0200377 while (len + offset + 4 >= size) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100378 buf = ly_realloc(buf, size + BUFSIZE_STEP);
379 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
380 size += BUFSIZE_STEP;
381 }
382
383 if (offset) {
384 /* store what we have so far */
385 memcpy(&buf[len], in, offset);
386 len += offset;
387 in += offset;
388 offset = 0;
389 }
390
Radek Krejci7a7fa902018-09-25 17:08:21 +0200391 ++offset;
392 if (in[offset] != '#') {
393 /* entity reference - only predefined references are supported */
394 if (!strncmp(&in[offset], "lt;", 3)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100395 buf[len++] = '<';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200396 in += 4; /* &lt; */
397 } else if (!strncmp(&in[offset], "gt;", 3)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100398 buf[len++] = '>';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200399 in += 4; /* &gt; */
400 } else if (!strncmp(&in[offset], "amp;", 4)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100401 buf[len++] = '&';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200402 in += 5; /* &amp; */
403 } else if (!strncmp(&in[offset], "apos;", 5)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100404 buf[len++] = '\'';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200405 in += 6; /* &apos; */
406 } else if (!strncmp(&in[offset], "quot;", 5)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100407 buf[len++] = '\"';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200408 in += 6; /* &quot; */
409 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100410 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200411 "Entity reference \"%.*s\" not supported, only predefined references allowed.", 10, &in[offset-1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200412 goto error;
413 }
414 offset = 0;
415 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100416 p = (void *)&in[offset - 1];
Radek Krejci7a7fa902018-09-25 17:08:21 +0200417 /* character reference */
418 ++offset;
419 if (isdigit(in[offset])) {
420 for (n = 0; isdigit(in[offset]); offset++) {
421 n = (10 * n) + (in[offset] - '0');
422 }
423 } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) {
424 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
425 if (isdigit(in[offset])) {
426 u = (in[offset] - '0');
427 } else if (in[offset] > 'F') {
428 u = 10 + (in[offset] - 'a');
429 } else {
430 u = 10 + (in[offset] - 'A');
431 }
432 n = (16 * n) + u;
433 }
434 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100435 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200436 goto error;
437
438 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100439
Radek Krejci7a7fa902018-09-25 17:08:21 +0200440 LY_CHECK_ERR_GOTO(in[offset] != ';',
Michal Vaskob36053d2020-03-26 15:49:30 +0100441 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP,
Radek Krejci7a7fa902018-09-25 17:08:21 +0200442 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
443 error);
444 ++offset;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200445 LY_CHECK_ERR_GOTO(ly_pututf8(&buf[len], n, &u),
Michal Vaskob36053d2020-03-26 15:49:30 +0100446 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
447 "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200448 error);
449 len += u;
450 in += offset;
451 offset = 0;
452 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100453 } else if (in[offset] == endchar) {
Radek Krejci7a7fa902018-09-25 17:08:21 +0200454 /* end of string */
Radek Krejcid70d1072018-10-09 14:20:47 +0200455 if (buf) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100456 /* realloc exact size string */
457 buf = ly_realloc(buf, len + offset + 1);
458 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
459 size = len + offset + 1;
Radek Krejcid70d1072018-10-09 14:20:47 +0200460 memcpy(&buf[len], in, offset);
Michal Vaskob36053d2020-03-26 15:49:30 +0100461
462 /* set terminating NULL byte */
463 buf[len + offset] = '\0';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200464 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200465 len += offset;
Michal Vaskob36053d2020-03-26 15:49:30 +0100466 in += offset;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200467 goto success;
468 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100469 if (!is_xmlws(in[offset])) {
470 /* non WS */
471 ws = 0;
472 }
473
Radek Krejci7a7fa902018-09-25 17:08:21 +0200474 /* log lines */
475 if (in[offset] == '\n') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100476 ++xmlctx->line;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200477 }
478
479 /* continue */
480 ++offset;
481 }
482 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100483
484 /* EOF reached before endchar */
485 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
486
Radek Krejci7a7fa902018-09-25 17:08:21 +0200487error:
Michal Vaskob36053d2020-03-26 15:49:30 +0100488 free(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200489 return LY_EVALID;
490
491success:
Radek Krejcid70d1072018-10-09 14:20:47 +0200492 if (buf) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100493 *value = buf;
494 *dynamic = 1;
495 } else {
496 *value = (char *)start;
497 *dynamic = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200498 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100499 *length = len;
500 *ws_only = ws;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200501
Michal Vasko63f3d842020-07-08 10:10:14 +0200502 ly_in_skip(xmlctx->in, in - xmlctx->in->current);
Michal Vaskob36053d2020-03-26 15:49:30 +0100503 return LY_SUCCESS;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200504
505#undef BUFSIZE
506#undef BUFSIZE_STEP
Radek Krejci7a7fa902018-09-25 17:08:21 +0200507}
508
Michal Vasko8cef5232020-06-15 17:59:47 +0200509/**
510 * @brief Parse XML closing element and match it to a stored starting element.
511 *
512 * @param[in] xmlctx XML context to use.
513 * @param[in] prefix Expected closing element prefix.
514 * @param[in] prefix_len Length of @p prefix.
515 * @param[in] name Expected closing element name.
516 * @param[in] name_len Length of @p name.
517 * @param[in] empty Whether we are parsing a special "empty" element (with joined starting and closing tag) with no value.
518 * @return LY_ERR value.
519 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100520static LY_ERR
521lyxml_close_element(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, const char *name, size_t name_len,
522 int empty)
Radek Krejcid972c252018-09-25 13:23:39 +0200523{
Michal Vaskob36053d2020-03-26 15:49:30 +0100524 struct lyxml_elem *e;
Radek Krejcid972c252018-09-25 13:23:39 +0200525
Michal Vaskob36053d2020-03-26 15:49:30 +0100526 /* match opening and closing element tags */
527 if (!xmlctx->elements.count) {
528 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
529 name_len, name);
530 return LY_EVALID;
531 }
Radek Krejcid972c252018-09-25 13:23:39 +0200532
Michal Vaskob36053d2020-03-26 15:49:30 +0100533 e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
534 if ((e->prefix_len != prefix_len) || (e->name_len != name_len)
535 || (prefix_len && strncmp(prefix, e->prefix, e->prefix_len)) || strncmp(name, e->name, e->name_len)) {
536 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
537 "Opening (\"%.*s%s%.*s\") and closing (\"%.*s%s%.*s\") elements tag mismatch.",
538 e->prefix_len, e->prefix ? e->prefix : "", e->prefix ? ":" : "", e->name_len, e->name,
539 prefix_len, prefix ? prefix : "", prefix ? ":" : "", name_len, name);
540 return LY_EVALID;
541 }
Radek Krejcid972c252018-09-25 13:23:39 +0200542
Michal Vaskob36053d2020-03-26 15:49:30 +0100543 /* opening and closing element tags matches, remove record from the opening tags list */
544 ly_set_rm_index(&xmlctx->elements, xmlctx->elements.count - 1, free);
Radek Krejcid972c252018-09-25 13:23:39 +0200545
Michal Vaskob36053d2020-03-26 15:49:30 +0100546 /* remove also the namespaces connected with the element */
547 lyxml_ns_rm(xmlctx);
Radek Krejcid972c252018-09-25 13:23:39 +0200548
Michal Vaskob36053d2020-03-26 15:49:30 +0100549 /* skip WS */
550 ign_xmlws(xmlctx);
Radek Krejcid972c252018-09-25 13:23:39 +0200551
Michal Vaskob36053d2020-03-26 15:49:30 +0100552 /* special "<elem/>" element */
Michal Vasko63f3d842020-07-08 10:10:14 +0200553 if (empty && (xmlctx->in->current[0] == '/')) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100554 move_input(xmlctx, 1);
555 }
Michal Vasko52927e22020-03-16 17:26:14 +0100556
Michal Vaskob36053d2020-03-26 15:49:30 +0100557 /* parse closing tag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200558 if (xmlctx->in->current[0] != '>') {
559 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
560 xmlctx->in->current, "element tag termination ('>')");
Michal Vaskob36053d2020-03-26 15:49:30 +0100561 return LY_EVALID;
562 }
Michal Vasko52927e22020-03-16 17:26:14 +0100563
Michal Vaskob36053d2020-03-26 15:49:30 +0100564 /* move after closing tag without checking for EOF */
Michal Vasko63f3d842020-07-08 10:10:14 +0200565 ly_in_skip(xmlctx->in, 1);
Michal Vasko52927e22020-03-16 17:26:14 +0100566
Radek Krejcid972c252018-09-25 13:23:39 +0200567 return LY_SUCCESS;
568}
569
Michal Vasko8cef5232020-06-15 17:59:47 +0200570/**
571 * @brief Store parsed opening element and parse any included namespaces.
572 *
573 * @param[in] xmlctx XML context to use.
574 * @param[in] prefix Parsed starting element prefix.
575 * @param[in] prefix_len Length of @p prefix.
576 * @param[in] name Parsed starting element name.
577 * @param[in] name_len Length of @p name.
578 * @return LY_ERR value.
579 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100580static LY_ERR
581lyxml_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 +0200582{
Michal Vaskob36053d2020-03-26 15:49:30 +0100583 LY_ERR ret = LY_SUCCESS;
584 struct lyxml_elem *e;
585 const char *prev_input;
586 char *value;
587 size_t parsed, value_len;
588 int ws_only, dynamic, is_ns;
589 uint32_t c;
Radek Krejcib1890642018-10-03 14:05:40 +0200590
Michal Vaskob36053d2020-03-26 15:49:30 +0100591 /* store element opening tag information */
592 e = malloc(sizeof *e);
593 LY_CHECK_ERR_RET(!e, LOGMEM(xmlctx->ctx), LY_EMEM);
594 e->name = name;
595 e->prefix = prefix;
596 e->name_len = name_len;
597 e->prefix_len = prefix_len;
598 ly_set_add(&xmlctx->elements, e, LY_SET_OPT_USEASLIST);
599
600 /* skip WS */
601 ign_xmlws(xmlctx);
602
603 /* parse and store all namespaces */
Michal Vasko63f3d842020-07-08 10:10:14 +0200604 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100605 is_ns = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200606 while ((xmlctx->in->current[0] != '\0') && !ly_getutf8(&xmlctx->in->current, &c, &parsed) && is_xmlqnamestartchar(c)) {
607 xmlctx->in->current -= parsed;
Michal Vaskob36053d2020-03-26 15:49:30 +0100608
609 /* parse attribute name */
610 LY_CHECK_GOTO(ret = lyxml_parse_qname(xmlctx, &prefix, &prefix_len, &name, &name_len), cleanup);
611
612 /* parse the value */
613 LY_CHECK_GOTO(ret = lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic), cleanup);
614
615 /* store every namespace */
616 if ((prefix && !ly_strncmp("xmlns", prefix, prefix_len)) || (!prefix && !ly_strncmp("xmlns", name, name_len))) {
617 LY_CHECK_GOTO(ret = lyxml_ns_add(xmlctx, prefix ? name : NULL, prefix ? name_len : 0,
618 dynamic ? value : strndup(value, value_len)), cleanup);
619 dynamic = 0;
620 } else {
621 /* not a namespace */
622 is_ns = 0;
623 }
624 if (dynamic) {
625 free(value);
626 }
627
628 /* skip WS */
629 ign_xmlws(xmlctx);
630
631 if (is_ns) {
632 /* we can actually skip all the namespaces as there is no reason to parse them again */
Michal Vasko63f3d842020-07-08 10:10:14 +0200633 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100634 }
Radek Krejcib1890642018-10-03 14:05:40 +0200635 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100636
637cleanup:
638 if (!ret) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200639 xmlctx->in->current = prev_input;
Michal Vaskob36053d2020-03-26 15:49:30 +0100640 }
641 return ret;
642}
643
Michal Vasko8cef5232020-06-15 17:59:47 +0200644/**
645 * @brief Move parser to the attribute content and parse it.
646 *
647 * @param[in] xmlctx XML context to use.
648 * @param[out] value Parsed attribute value.
649 * @param[out] value_len Length of @p value.
650 * @param[out] ws_only Whether the value is empty/white-spaces only.
651 * @param[out] dynamic Whether the value was dynamically allocated.
652 * @return LY_ERR value.
653 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100654static LY_ERR
655lyxml_next_attr_content(struct lyxml_ctx *xmlctx, const char **value, size_t *value_len, int *ws_only, int *dynamic)
656{
657 char quot;
658
659 /* skip WS */
660 ign_xmlws(xmlctx);
661
662 /* skip '=' */
Michal Vasko63f3d842020-07-08 10:10:14 +0200663 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100664 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
665 return LY_EVALID;
Michal Vasko63f3d842020-07-08 10:10:14 +0200666 } else if (xmlctx->in->current[0] != '=') {
667 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
668 xmlctx->in->current, "'='");
Michal Vaskob36053d2020-03-26 15:49:30 +0100669 return LY_EVALID;
670 }
671 move_input(xmlctx, 1);
672
673 /* skip WS */
674 ign_xmlws(xmlctx);
675
676 /* find quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200677 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100678 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
679 return LY_EVALID;
Michal Vasko63f3d842020-07-08 10:10:14 +0200680 } else if ((xmlctx->in->current[0] != '\'') && (xmlctx->in->current[0] != '\"')) {
681 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
682 xmlctx->in->current, "either single or double quotation mark");
Michal Vaskob36053d2020-03-26 15:49:30 +0100683 return LY_EVALID;
684 }
685
686 /* remember quote */
Michal Vasko63f3d842020-07-08 10:10:14 +0200687 quot = xmlctx->in->current[0];
Michal Vaskob36053d2020-03-26 15:49:30 +0100688 move_input(xmlctx, 1);
689
690 /* parse attribute value */
691 LY_CHECK_RET(lyxml_parse_value(xmlctx, quot, (char **)value, value_len, ws_only, dynamic));
692
693 /* move after ending quote (without checking for EOF) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200694 ly_in_skip(xmlctx->in, 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100695
696 return LY_SUCCESS;
697}
698
Michal Vasko8cef5232020-06-15 17:59:47 +0200699/**
700 * @brief Move parser to the next attribute and parse it.
701 *
702 * @param[in] xmlctx XML context to use.
703 * @param[out] prefix Parsed attribute prefix.
704 * @param[out] prefix_len Length of @p prefix.
705 * @param[out] name Parsed attribute name.
706 * @param[out] name_len Length of @p name.
707 * @return LY_ERR value.
708 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100709static LY_ERR
710lyxml_next_attribute(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
711{
712 const char *in;
713 char *value;
714 uint32_t c;
715 size_t parsed, value_len;
716 int ws_only, dynamic;
717
718 /* skip WS */
719 ign_xmlws(xmlctx);
720
721 /* parse only possible attributes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200722 while ((xmlctx->in->current[0] != '>') && (xmlctx->in->current[0] != '/')) {
723 in = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100724 if (in[0] == '\0') {
725 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
726 return LY_EVALID;
727 } else if ((ly_getutf8(&in, &c, &parsed) || !is_xmlqnamestartchar(c))) {
728 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in - parsed), in - parsed,
729 "element tag end ('>' or '/>') or an attribute");
730 return LY_EVALID;
731 }
732
733 /* parse attribute name */
734 LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len));
735
736 if ((!*prefix || ly_strncmp("xmlns", *prefix, *prefix_len)) && (*prefix || ly_strncmp("xmlns", *name, *name_len))) {
737 /* standard attribute */
738 break;
739 }
740
741 /* namespace, skip it */
742 LY_CHECK_RET(lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic));
743 if (dynamic) {
744 free(value);
745 }
746
747 /* skip WS */
748 ign_xmlws(xmlctx);
749 }
750
751 return LY_SUCCESS;
752}
753
Michal Vasko8cef5232020-06-15 17:59:47 +0200754/**
755 * @brief Move parser to the next element and parse it.
756 *
757 * @param[in] xmlctx XML context to use.
758 * @param[out] prefix Parsed element prefix.
759 * @param[out] prefix_len Length of @p prefix.
760 * @param[out] name Parse element name.
761 * @param[out] name_len Length of @p name.
762 * @return LY_ERR value.
763 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100764static LY_ERR
765lyxml_next_element(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
766 int *closing)
767{
768 /* skip WS until EOF or after opening tag '<' */
769 LY_CHECK_RET(lyxml_skip_until_end_or_after_otag(xmlctx));
Michal Vasko63f3d842020-07-08 10:10:14 +0200770 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100771 /* set return values */
772 *prefix = *name = NULL;
773 *prefix_len = *name_len = 0;
774 return LY_SUCCESS;
775 }
776
Michal Vasko63f3d842020-07-08 10:10:14 +0200777 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100778 move_input(xmlctx, 1);
779 *closing = 1;
780 } else {
781 *closing = 0;
782 }
783
784 /* skip WS */
785 ign_xmlws(xmlctx);
786
787 /* parse element name */
788 LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len));
789
790 return LY_SUCCESS;
791}
792
793LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200794lyxml_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyxml_ctx **xmlctx_p)
Michal Vaskob36053d2020-03-26 15:49:30 +0100795{
796 LY_ERR ret = LY_SUCCESS;
797 struct lyxml_ctx *xmlctx;
798 int closing;
799
800 /* new context */
801 xmlctx = calloc(1, sizeof *xmlctx);
802 LY_CHECK_ERR_RET(!xmlctx, LOGMEM(ctx), LY_EMEM);
803 xmlctx->ctx = ctx;
804 xmlctx->line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200805 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100806
807 /* parse next element, if any */
808 LY_CHECK_GOTO(ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name,
809 &xmlctx->name_len, &closing), cleanup);
810
Michal Vasko63f3d842020-07-08 10:10:14 +0200811 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100812 /* update status */
813 xmlctx->status = LYXML_END;
814 } else if (closing) {
815 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
816 xmlctx->name_len, xmlctx->name);
817 ret = LY_EVALID;
818 goto cleanup;
819 } else {
820 /* open an element, also parses all enclosed namespaces */
821 LY_CHECK_GOTO(ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len), cleanup);
822
823 /* update status */
824 xmlctx->status = LYXML_ELEMENT;
825 }
826
827cleanup:
828 if (ret) {
829 lyxml_ctx_free(xmlctx);
830 } else {
831 *xmlctx_p = xmlctx;
832 }
833 return ret;
834}
835
836LY_ERR
837lyxml_ctx_next(struct lyxml_ctx *xmlctx)
838{
839 LY_ERR ret = LY_SUCCESS;
840 int closing;
841 struct lyxml_elem *e;
842
843 /* if the value was not used, free it */
844 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
845 free((char *)xmlctx->value);
846 xmlctx->value = NULL;
847 xmlctx->dynamic = 0;
848 }
849
850 switch (xmlctx->status) {
851 /* content |</elem> */
852 case LYXML_ELEM_CONTENT:
853 /* handle special case when empty content for "<elem/>" was returned */
Michal Vasko63f3d842020-07-08 10:10:14 +0200854 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100855 assert(xmlctx->elements.count);
856 e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
857
858 /* close the element (parses closing tag) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200859 ret = lyxml_close_element(xmlctx, e->prefix, e->prefix_len, e->name, e->name_len, 1);
860 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100861
862 /* update status */
863 xmlctx->status = LYXML_ELEM_CLOSE;
864 break;
865 }
866 /* fallthrough */
867
868 /* </elem>| <elem2>* */
869 case LYXML_ELEM_CLOSE:
870 /* parse next element, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200871 ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len, &closing);
872 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100873
Michal Vasko63f3d842020-07-08 10:10:14 +0200874 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100875 /* update status */
876 xmlctx->status = LYXML_END;
877 } else if (closing) {
878 /* close an element (parses also closing tag) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200879 ret = lyxml_close_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len, 0);
880 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100881
882 /* update status */
883 xmlctx->status = LYXML_ELEM_CLOSE;
884 } else {
885 /* open an element, also parses all enclosed namespaces */
Michal Vasko63f3d842020-07-08 10:10:14 +0200886 ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len);
887 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100888
889 /* update status */
890 xmlctx->status = LYXML_ELEMENT;
891 }
892 break;
893
894 /* <elem| attr='val'* > content */
895 case LYXML_ELEMENT:
896
897 /* attr='val'| attr='val'* > content */
898 case LYXML_ATTR_CONTENT:
899 /* parse attribute name, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200900 ret = lyxml_next_attribute(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len);
901 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100902
Michal Vasko63f3d842020-07-08 10:10:14 +0200903 if (xmlctx->in->current[0] == '>') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100904 /* no attributes but a closing tag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200905 ly_in_skip(xmlctx->in, 1);
906 if (!xmlctx->in->current[0]) {
Michal Vaskof55ae202020-06-30 15:49:36 +0200907 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
908 ret = LY_EVALID;
909 goto cleanup;
910 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100911
912 /* parse element content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200913 ret = lyxml_parse_value(xmlctx, '<', (char **)&xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only,
914 &xmlctx->dynamic);
915 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100916
917 if (!xmlctx->value_len) {
918 /* use empty value, easier to work with */
919 xmlctx->value = "";
920 assert(!xmlctx->dynamic);
921 }
922
923 /* update status */
924 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vasko63f3d842020-07-08 10:10:14 +0200925 } else if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100926 /* no content but we still return it */
927 xmlctx->value = "";
928 xmlctx->value_len = 0;
929 xmlctx->ws_only = 1;
930 xmlctx->dynamic = 0;
931
932 /* update status */
933 xmlctx->status = LYXML_ELEM_CONTENT;
934 } else {
935 /* update status */
936 xmlctx->status = LYXML_ATTRIBUTE;
937 }
938 break;
939
940 /* attr|='val' */
941 case LYXML_ATTRIBUTE:
942 /* skip formatting and parse value */
Michal Vasko63f3d842020-07-08 10:10:14 +0200943 ret = lyxml_next_attr_content(xmlctx, &xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only, &xmlctx->dynamic);
944 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100945
946 /* update status */
947 xmlctx->status = LYXML_ATTR_CONTENT;
948 break;
949
950 /* </elem> |EOF */
951 case LYXML_END:
952 /* nothing to do */
953 break;
954 }
955
956cleanup:
957 if (ret) {
958 /* invalidate context */
959 xmlctx->status = LYXML_END;
960 }
961 return ret;
962}
963
964LY_ERR
965lyxml_ctx_peek(struct lyxml_ctx *xmlctx, enum LYXML_PARSER_STATUS *next)
966{
967 LY_ERR ret = LY_SUCCESS;
968 const char *prefix, *name, *prev_input;
969 size_t prefix_len, name_len;
970 int closing;
971
Michal Vasko63f3d842020-07-08 10:10:14 +0200972 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100973
974 switch (xmlctx->status) {
975 case LYXML_ELEM_CONTENT:
Michal Vasko63f3d842020-07-08 10:10:14 +0200976 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100977 *next = LYXML_ELEM_CLOSE;
978 break;
979 }
980 /* fallthrough */
981 case LYXML_ELEM_CLOSE:
982 /* parse next element, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200983 ret = lyxml_next_element(xmlctx, &prefix, &prefix_len, &name, &name_len, &closing);
984 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100985
Michal Vasko63f3d842020-07-08 10:10:14 +0200986 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100987 *next = LYXML_END;
988 } else if (closing) {
989 *next = LYXML_ELEM_CLOSE;
990 } else {
991 *next = LYXML_ELEMENT;
992 }
993 break;
994 case LYXML_ELEMENT:
995 case LYXML_ATTR_CONTENT:
996 /* parse attribute name, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200997 ret = lyxml_next_attribute(xmlctx, &prefix, &prefix_len, &name, &name_len);
998 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100999
Michal Vasko63f3d842020-07-08 10:10:14 +02001000 if ((xmlctx->in->current[0] == '>') || (xmlctx->in->current[0] == '/')) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001001 *next = LYXML_ELEM_CONTENT;
1002 } else {
1003 *next = LYXML_ATTRIBUTE;
1004 }
1005 break;
1006 case LYXML_ATTRIBUTE:
1007 *next = LYXML_ATTR_CONTENT;
1008 break;
1009 case LYXML_END:
1010 *next = LYXML_END;
1011 break;
1012 }
1013
1014cleanup:
Michal Vasko63f3d842020-07-08 10:10:14 +02001015 xmlctx->in->current = prev_input;
Michal Vaskob36053d2020-03-26 15:49:30 +01001016 return ret;
1017}
1018
1019void
1020lyxml_ctx_free(struct lyxml_ctx *xmlctx)
1021{
1022 uint32_t u;
1023
1024 if (!xmlctx) {
1025 return;
1026 }
1027
1028 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
1029 free((char *)xmlctx->value);
1030 }
1031 ly_set_erase(&xmlctx->elements, free);
1032 for (u = xmlctx->ns.count - 1; u + 1 > 0; --u) {
1033 /* remove the ns structure */
1034 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix);
1035 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri);
1036 free(xmlctx->ns.objs[u]);
1037 }
1038 ly_set_erase(&xmlctx->ns, NULL);
1039 free(xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +02001040}
Radek Krejcie7b95092019-05-15 11:03:07 +02001041
1042LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02001043lyxml_dump_text(struct ly_out *out, const char *text, int attribute)
Radek Krejcie7b95092019-05-15 11:03:07 +02001044{
Radek Krejcibaeb8382020-05-27 16:44:53 +02001045 ssize_t ret = LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +02001046 unsigned int u;
1047
1048 if (!text) {
1049 return 0;
1050 }
1051
1052 for (u = 0; text[u]; u++) {
1053 switch (text[u]) {
1054 case '&':
Radek Krejci241f6b52020-05-21 18:13:49 +02001055 ret = ly_print(out, "&amp;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001056 break;
1057 case '<':
Radek Krejci241f6b52020-05-21 18:13:49 +02001058 ret = ly_print(out, "&lt;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001059 break;
1060 case '>':
1061 /* not needed, just for readability */
Radek Krejci241f6b52020-05-21 18:13:49 +02001062 ret = ly_print(out, "&gt;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001063 break;
1064 case '"':
1065 if (attribute) {
Radek Krejci241f6b52020-05-21 18:13:49 +02001066 ret = ly_print(out, "&quot;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001067 break;
1068 }
1069 /* falls through */
1070 default:
Radek Krejcibaeb8382020-05-27 16:44:53 +02001071 ret = ly_write(out, &text[u], 1);
Radek Krejcie7b95092019-05-15 11:03:07 +02001072 }
1073 }
1074
Radek Krejcibaeb8382020-05-27 16:44:53 +02001075 return ret < 0 ? (-1 * ret) : 0;
Radek Krejcie7b95092019-05-15 11:03:07 +02001076}
1077
Michal Vasko52927e22020-03-16 17:26:14 +01001078LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001079lyxml_get_prefixes(struct lyxml_ctx *xmlctx, const char *value, size_t value_len, struct ly_prefix **val_prefs)
Michal Vasko52927e22020-03-16 17:26:14 +01001080{
1081 LY_ERR ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001082 LY_ARRAY_COUNT_TYPE u;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001083 uint32_t c;
Michal Vasko52927e22020-03-16 17:26:14 +01001084 const struct lyxml_ns *ns;
1085 const char *start, *stop;
1086 struct ly_prefix *prefixes = NULL;
1087 size_t len;
1088
1089 for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) {
1090 size_t bytes;
1091 ly_getutf8(&stop, &c, &bytes);
1092 if (is_xmlqnamestartchar(c)) {
1093 for (ly_getutf8(&stop, &c, &bytes);
1094 is_xmlqnamechar(c) && (size_t)(stop - value) < value_len;
1095 ly_getutf8(&stop, &c, &bytes));
1096 stop = stop - bytes;
1097 if (*stop == ':') {
1098 /* we have a possible prefix */
1099 len = stop - start;
Michal Vaskob36053d2020-03-26 15:49:30 +01001100 ns = lyxml_ns_get(xmlctx, start, len);
Michal Vasko52927e22020-03-16 17:26:14 +01001101 if (ns) {
1102 struct ly_prefix *p = NULL;
1103
1104 /* check whether we do not already have this prefix stored */
1105 LY_ARRAY_FOR(prefixes, u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001106 if (!ly_strncmp(prefixes[u].id, start, len)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001107 p = &prefixes[u];
1108 break;
1109 }
1110 }
1111 if (!p) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001112 LY_ARRAY_NEW_GOTO(xmlctx->ctx, prefixes, p, ret, error);
Radek Krejci1798aae2020-07-14 13:26:06 +02001113 p->id = lydict_insert(xmlctx->ctx, start, len);
1114 p->module_ns = lydict_insert(xmlctx->ctx, ns->uri, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01001115 } /* else the prefix already present */
1116 }
1117 }
1118 stop = stop + bytes;
1119 }
1120 }
1121
1122 *val_prefs = prefixes;
1123 return LY_SUCCESS;
1124
1125error:
1126 LY_ARRAY_FOR(prefixes, u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001127 lydict_remove(xmlctx->ctx, prefixes[u].id);
1128 lydict_remove(xmlctx->ctx, prefixes[u].module_ns);
Michal Vasko52927e22020-03-16 17:26:14 +01001129 }
1130 LY_ARRAY_FREE(prefixes);
1131 return ret;
1132}
1133
1134LY_ERR
1135lyxml_value_compare(const char *value1, const struct ly_prefix *prefs1, const char *value2, const struct ly_prefix *prefs2)
1136{
1137 const char *ptr1, *ptr2, *ns1, *ns2;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001138 LY_ARRAY_COUNT_TYPE u1, u2;
Michal Vasko52927e22020-03-16 17:26:14 +01001139 int len;
1140
1141 if (!value1 && !value2) {
1142 return LY_SUCCESS;
1143 }
1144 if ((value1 && !value2) || (!value1 && value2)) {
1145 return LY_ENOT;
1146 }
1147
1148 ptr1 = value1;
1149 ptr2 = value2;
1150 while (ptr1[0] && ptr2[0]) {
1151 if (ptr1[0] != ptr2[0]) {
1152 /* it can be a start of prefix that maps to the same module */
1153 ns1 = ns2 = NULL;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02001154 u1 = u2 = 0;
Michal Vasko52927e22020-03-16 17:26:14 +01001155 if (prefs1) {
1156 /* find module of the first prefix, if any */
1157 LY_ARRAY_FOR(prefs1, u1) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001158 len = strlen(prefs1[u1].id);
1159 if (!strncmp(ptr1, prefs1[u1].id, len) && (ptr1[len] == ':')) {
1160 ns1 = prefs1[u1].module_ns;
Michal Vasko52927e22020-03-16 17:26:14 +01001161 break;
1162 }
1163 }
1164 }
1165 if (prefs2) {
1166 /* find module of the second prefix, if any */
1167 LY_ARRAY_FOR(prefs2, u2) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001168 len = strlen(prefs2[u2].id);
1169 if (!strncmp(ptr2, prefs2[u2].id, len) && (ptr2[len] == ':')) {
1170 ns2 = prefs2[u2].module_ns;
Michal Vasko52927e22020-03-16 17:26:14 +01001171 break;
1172 }
1173 }
1174 }
1175
1176 if (!ns1 || !ns2 || (ns1 != ns2)) {
1177 /* not a prefix or maps to different namespaces */
1178 break;
1179 }
1180
1181 /* skip prefixes in both values (':' is skipped as iter) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001182 ptr1 += strlen(prefs1[u1].id);
1183 ptr2 += strlen(prefs2[u2].id);
Michal Vasko52927e22020-03-16 17:26:14 +01001184 }
1185
1186 ++ptr1;
1187 ++ptr2;
1188 }
1189 if (ptr1[0] || ptr2[0]) {
1190 /* not a match or simply different lengths */
1191 return LY_ENOT;
1192 }
1193
1194 return LY_SUCCESS;
1195}