blob: 22afda58c7f672b1c1be020ca0db9c4d77b82120 [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/**
Radek Krejci7a7fa902018-09-25 17:08:21 +0200328 * Store UTF-8 character specified as 4byte integer into the dst buffer.
329 * Returns number of written bytes (4 max), expects that dst has enough space.
330 *
331 * UTF-8 mapping:
332 * 00000000 -- 0000007F: 0xxxxxxx
333 * 00000080 -- 000007FF: 110xxxxx 10xxxxxx
334 * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
335 * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
336 *
337 * Includes checking for valid characters (following RFC 7950, sec 9.4)
338 */
339static LY_ERR
Radek Krejci117d2082018-09-26 10:05:14 +0200340lyxml_pututf8(char *dst, uint32_t value, size_t *bytes_written)
Radek Krejci7a7fa902018-09-25 17:08:21 +0200341{
342 if (value < 0x80) {
343 /* one byte character */
344 if (value < 0x20 &&
345 value != 0x09 &&
346 value != 0x0a &&
347 value != 0x0d) {
348 return LY_EINVAL;
349 }
350
351 dst[0] = value;
352 (*bytes_written) = 1;
353 } else if (value < 0x800) {
354 /* two bytes character */
355 dst[0] = 0xc0 | (value >> 6);
356 dst[1] = 0x80 | (value & 0x3f);
357 (*bytes_written) = 2;
358 } else if (value < 0xfffe) {
359 /* three bytes character */
360 if (((value & 0xf800) == 0xd800) ||
361 (value >= 0xfdd0 && value <= 0xfdef)) {
362 /* exclude surrogate blocks %xD800-DFFF */
363 /* exclude noncharacters %xFDD0-FDEF */
364 return LY_EINVAL;
365 }
366
367 dst[0] = 0xe0 | (value >> 12);
368 dst[1] = 0x80 | ((value >> 6) & 0x3f);
369 dst[2] = 0x80 | (value & 0x3f);
370
371 (*bytes_written) = 3;
372 } else if (value < 0x10fffe) {
373 if ((value & 0xffe) == 0xffe) {
374 /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF,
375 * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF,
376 * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */
377 return LY_EINVAL;
378 }
379 /* four bytes character */
380 dst[0] = 0xf0 | (value >> 18);
381 dst[1] = 0x80 | ((value >> 12) & 0x3f);
382 dst[2] = 0x80 | ((value >> 6) & 0x3f);
383 dst[3] = 0x80 | (value & 0x3f);
384
385 (*bytes_written) = 4;
386 }
387 return LY_SUCCESS;
388}
389
Michal Vasko8cef5232020-06-15 17:59:47 +0200390/**
391 * @brief Parse XML text content (value).
392 *
393 * @param[in] xmlctx XML context to use.
394 * @param[in] endchar Expected character to mark value end.
395 * @param[out] value Parsed value.
396 * @param[out] length Length of @p value.
397 * @param[out] ws_only Whether the value is empty/white-spaces only.
398 * @param[out] dynamic Whether the value was dynamically allocated.
399 * @return LY_ERR value.
400 */
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200401static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100402lyxml_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 +0200403{
Michal Vaskob36053d2020-03-26 15:49:30 +0100404#define BUFSIZE 24
405#define BUFSIZE_STEP 128
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200406
Michal Vaskob36053d2020-03-26 15:49:30 +0100407 const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */
Michal Vasko63f3d842020-07-08 10:10:14 +0200408 const char *in = xmlctx->in->current, *start;
Michal Vaskob36053d2020-03-26 15:49:30 +0100409 char *buf = NULL;
Radek Krejci4ad42aa2019-07-23 16:55:58 +0200410 size_t offset; /* read offset in input buffer */
411 size_t len; /* length of the output string (write offset in output buffer) */
412 size_t size = 0; /* size of the output buffer */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200413 void *p;
Radek Krejci117d2082018-09-26 10:05:14 +0200414 uint32_t n;
Michal Vaskob36053d2020-03-26 15:49:30 +0100415 size_t u;
416 int ws = 1;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200417
Michal Vaskob36053d2020-03-26 15:49:30 +0100418 assert(xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +0200419
Radek Krejcid70d1072018-10-09 14:20:47 +0200420 /* init */
Michal Vaskob36053d2020-03-26 15:49:30 +0100421 start = in;
Radek Krejcid70d1072018-10-09 14:20:47 +0200422 offset = len = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200423
424 /* parse */
425 while (in[offset]) {
426 if (in[offset] == '&') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100427 /* non WS */
428 ws = 0;
Radek Krejcid70d1072018-10-09 14:20:47 +0200429
Michal Vaskob36053d2020-03-26 15:49:30 +0100430 if (!buf) {
431 /* prepare output buffer */
432 buf = malloc(BUFSIZE);
433 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
434 size = BUFSIZE;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200435 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100436
437 /* allocate enough for the offset and next character,
438 * we will need 4 bytes at most since we support only the predefined
439 * (one-char) entities and character references */
440 if (len + offset + 4 >= size) {
441 buf = ly_realloc(buf, size + BUFSIZE_STEP);
442 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
443 size += BUFSIZE_STEP;
444 }
445
446 if (offset) {
447 /* store what we have so far */
448 memcpy(&buf[len], in, offset);
449 len += offset;
450 in += offset;
451 offset = 0;
452 }
453
Radek Krejci7a7fa902018-09-25 17:08:21 +0200454 ++offset;
455 if (in[offset] != '#') {
456 /* entity reference - only predefined references are supported */
457 if (!strncmp(&in[offset], "lt;", 3)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100458 buf[len++] = '<';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200459 in += 4; /* &lt; */
460 } else if (!strncmp(&in[offset], "gt;", 3)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100461 buf[len++] = '>';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200462 in += 4; /* &gt; */
463 } else if (!strncmp(&in[offset], "amp;", 4)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100464 buf[len++] = '&';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200465 in += 5; /* &amp; */
466 } else if (!strncmp(&in[offset], "apos;", 5)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100467 buf[len++] = '\'';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200468 in += 6; /* &apos; */
469 } else if (!strncmp(&in[offset], "quot;", 5)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100470 buf[len++] = '\"';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200471 in += 6; /* &quot; */
472 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100473 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200474 "Entity reference \"%.*s\" not supported, only predefined references allowed.", 10, &in[offset-1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200475 goto error;
476 }
477 offset = 0;
478 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100479 p = (void *)&in[offset - 1];
Radek Krejci7a7fa902018-09-25 17:08:21 +0200480 /* character reference */
481 ++offset;
482 if (isdigit(in[offset])) {
483 for (n = 0; isdigit(in[offset]); offset++) {
484 n = (10 * n) + (in[offset] - '0');
485 }
486 } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) {
487 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
488 if (isdigit(in[offset])) {
489 u = (in[offset] - '0');
490 } else if (in[offset] > 'F') {
491 u = 10 + (in[offset] - 'a');
492 } else {
493 u = 10 + (in[offset] - 'A');
494 }
495 n = (16 * n) + u;
496 }
497 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100498 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200499 goto error;
500
501 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100502
Radek Krejci7a7fa902018-09-25 17:08:21 +0200503 LY_CHECK_ERR_GOTO(in[offset] != ';',
Michal Vaskob36053d2020-03-26 15:49:30 +0100504 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP,
Radek Krejci7a7fa902018-09-25 17:08:21 +0200505 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
506 error);
507 ++offset;
Michal Vaskob36053d2020-03-26 15:49:30 +0100508 LY_CHECK_ERR_GOTO(lyxml_pututf8(&buf[len], n, &u),
509 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
510 "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200511 error);
512 len += u;
513 in += offset;
514 offset = 0;
515 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100516 } else if (in[offset] == endchar) {
Radek Krejci7a7fa902018-09-25 17:08:21 +0200517 /* end of string */
Radek Krejcid70d1072018-10-09 14:20:47 +0200518 if (buf) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100519 /* realloc exact size string */
520 buf = ly_realloc(buf, len + offset + 1);
521 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
522 size = len + offset + 1;
Radek Krejcid70d1072018-10-09 14:20:47 +0200523 memcpy(&buf[len], in, offset);
Michal Vaskob36053d2020-03-26 15:49:30 +0100524
525 /* set terminating NULL byte */
526 buf[len + offset] = '\0';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200527 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200528 len += offset;
Michal Vaskob36053d2020-03-26 15:49:30 +0100529 in += offset;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200530 goto success;
531 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100532 if (!is_xmlws(in[offset])) {
533 /* non WS */
534 ws = 0;
535 }
536
Radek Krejci7a7fa902018-09-25 17:08:21 +0200537 /* log lines */
538 if (in[offset] == '\n') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100539 ++xmlctx->line;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200540 }
541
542 /* continue */
543 ++offset;
544 }
545 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100546
547 /* EOF reached before endchar */
548 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
549
Radek Krejci7a7fa902018-09-25 17:08:21 +0200550error:
Michal Vaskob36053d2020-03-26 15:49:30 +0100551 free(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200552 return LY_EVALID;
553
554success:
Radek Krejcid70d1072018-10-09 14:20:47 +0200555 if (buf) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100556 *value = buf;
557 *dynamic = 1;
558 } else {
559 *value = (char *)start;
560 *dynamic = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200561 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100562 *length = len;
563 *ws_only = ws;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200564
Michal Vasko63f3d842020-07-08 10:10:14 +0200565 ly_in_skip(xmlctx->in, in - xmlctx->in->current);
Michal Vaskob36053d2020-03-26 15:49:30 +0100566 return LY_SUCCESS;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200567
568#undef BUFSIZE
569#undef BUFSIZE_STEP
Radek Krejci7a7fa902018-09-25 17:08:21 +0200570}
571
Michal Vasko8cef5232020-06-15 17:59:47 +0200572/**
573 * @brief Parse XML closing element and match it to a stored starting element.
574 *
575 * @param[in] xmlctx XML context to use.
576 * @param[in] prefix Expected closing element prefix.
577 * @param[in] prefix_len Length of @p prefix.
578 * @param[in] name Expected closing element name.
579 * @param[in] name_len Length of @p name.
580 * @param[in] empty Whether we are parsing a special "empty" element (with joined starting and closing tag) with no value.
581 * @return LY_ERR value.
582 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100583static LY_ERR
584lyxml_close_element(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, const char *name, size_t name_len,
585 int empty)
Radek Krejcid972c252018-09-25 13:23:39 +0200586{
Michal Vaskob36053d2020-03-26 15:49:30 +0100587 struct lyxml_elem *e;
Radek Krejcid972c252018-09-25 13:23:39 +0200588
Michal Vaskob36053d2020-03-26 15:49:30 +0100589 /* match opening and closing element tags */
590 if (!xmlctx->elements.count) {
591 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
592 name_len, name);
593 return LY_EVALID;
594 }
Radek Krejcid972c252018-09-25 13:23:39 +0200595
Michal Vaskob36053d2020-03-26 15:49:30 +0100596 e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
597 if ((e->prefix_len != prefix_len) || (e->name_len != name_len)
598 || (prefix_len && strncmp(prefix, e->prefix, e->prefix_len)) || strncmp(name, e->name, e->name_len)) {
599 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
600 "Opening (\"%.*s%s%.*s\") and closing (\"%.*s%s%.*s\") elements tag mismatch.",
601 e->prefix_len, e->prefix ? e->prefix : "", e->prefix ? ":" : "", e->name_len, e->name,
602 prefix_len, prefix ? prefix : "", prefix ? ":" : "", name_len, name);
603 return LY_EVALID;
604 }
Radek Krejcid972c252018-09-25 13:23:39 +0200605
Michal Vaskob36053d2020-03-26 15:49:30 +0100606 /* opening and closing element tags matches, remove record from the opening tags list */
607 ly_set_rm_index(&xmlctx->elements, xmlctx->elements.count - 1, free);
Radek Krejcid972c252018-09-25 13:23:39 +0200608
Michal Vaskob36053d2020-03-26 15:49:30 +0100609 /* remove also the namespaces connected with the element */
610 lyxml_ns_rm(xmlctx);
Radek Krejcid972c252018-09-25 13:23:39 +0200611
Michal Vaskob36053d2020-03-26 15:49:30 +0100612 /* skip WS */
613 ign_xmlws(xmlctx);
Radek Krejcid972c252018-09-25 13:23:39 +0200614
Michal Vaskob36053d2020-03-26 15:49:30 +0100615 /* special "<elem/>" element */
Michal Vasko63f3d842020-07-08 10:10:14 +0200616 if (empty && (xmlctx->in->current[0] == '/')) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100617 move_input(xmlctx, 1);
618 }
Michal Vasko52927e22020-03-16 17:26:14 +0100619
Michal Vaskob36053d2020-03-26 15:49:30 +0100620 /* parse closing tag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200621 if (xmlctx->in->current[0] != '>') {
622 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
623 xmlctx->in->current, "element tag termination ('>')");
Michal Vaskob36053d2020-03-26 15:49:30 +0100624 return LY_EVALID;
625 }
Michal Vasko52927e22020-03-16 17:26:14 +0100626
Michal Vaskob36053d2020-03-26 15:49:30 +0100627 /* move after closing tag without checking for EOF */
Michal Vasko63f3d842020-07-08 10:10:14 +0200628 ly_in_skip(xmlctx->in, 1);
Michal Vasko52927e22020-03-16 17:26:14 +0100629
Radek Krejcid972c252018-09-25 13:23:39 +0200630 return LY_SUCCESS;
631}
632
Michal Vasko8cef5232020-06-15 17:59:47 +0200633/**
634 * @brief Store parsed opening element and parse any included namespaces.
635 *
636 * @param[in] xmlctx XML context to use.
637 * @param[in] prefix Parsed starting element prefix.
638 * @param[in] prefix_len Length of @p prefix.
639 * @param[in] name Parsed starting element name.
640 * @param[in] name_len Length of @p name.
641 * @return LY_ERR value.
642 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100643static LY_ERR
644lyxml_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 +0200645{
Michal Vaskob36053d2020-03-26 15:49:30 +0100646 LY_ERR ret = LY_SUCCESS;
647 struct lyxml_elem *e;
648 const char *prev_input;
649 char *value;
650 size_t parsed, value_len;
651 int ws_only, dynamic, is_ns;
652 uint32_t c;
Radek Krejcib1890642018-10-03 14:05:40 +0200653
Michal Vaskob36053d2020-03-26 15:49:30 +0100654 /* store element opening tag information */
655 e = malloc(sizeof *e);
656 LY_CHECK_ERR_RET(!e, LOGMEM(xmlctx->ctx), LY_EMEM);
657 e->name = name;
658 e->prefix = prefix;
659 e->name_len = name_len;
660 e->prefix_len = prefix_len;
661 ly_set_add(&xmlctx->elements, e, LY_SET_OPT_USEASLIST);
662
663 /* skip WS */
664 ign_xmlws(xmlctx);
665
666 /* parse and store all namespaces */
Michal Vasko63f3d842020-07-08 10:10:14 +0200667 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100668 is_ns = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200669 while ((xmlctx->in->current[0] != '\0') && !ly_getutf8(&xmlctx->in->current, &c, &parsed) && is_xmlqnamestartchar(c)) {
670 xmlctx->in->current -= parsed;
Michal Vaskob36053d2020-03-26 15:49:30 +0100671
672 /* parse attribute name */
673 LY_CHECK_GOTO(ret = lyxml_parse_qname(xmlctx, &prefix, &prefix_len, &name, &name_len), cleanup);
674
675 /* parse the value */
676 LY_CHECK_GOTO(ret = lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic), cleanup);
677
678 /* store every namespace */
679 if ((prefix && !ly_strncmp("xmlns", prefix, prefix_len)) || (!prefix && !ly_strncmp("xmlns", name, name_len))) {
680 LY_CHECK_GOTO(ret = lyxml_ns_add(xmlctx, prefix ? name : NULL, prefix ? name_len : 0,
681 dynamic ? value : strndup(value, value_len)), cleanup);
682 dynamic = 0;
683 } else {
684 /* not a namespace */
685 is_ns = 0;
686 }
687 if (dynamic) {
688 free(value);
689 }
690
691 /* skip WS */
692 ign_xmlws(xmlctx);
693
694 if (is_ns) {
695 /* we can actually skip all the namespaces as there is no reason to parse them again */
Michal Vasko63f3d842020-07-08 10:10:14 +0200696 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100697 }
Radek Krejcib1890642018-10-03 14:05:40 +0200698 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100699
700cleanup:
701 if (!ret) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200702 xmlctx->in->current = prev_input;
Michal Vaskob36053d2020-03-26 15:49:30 +0100703 }
704 return ret;
705}
706
Michal Vasko8cef5232020-06-15 17:59:47 +0200707/**
708 * @brief Move parser to the attribute content and parse it.
709 *
710 * @param[in] xmlctx XML context to use.
711 * @param[out] value Parsed attribute value.
712 * @param[out] value_len Length of @p value.
713 * @param[out] ws_only Whether the value is empty/white-spaces only.
714 * @param[out] dynamic Whether the value was dynamically allocated.
715 * @return LY_ERR value.
716 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100717static LY_ERR
718lyxml_next_attr_content(struct lyxml_ctx *xmlctx, const char **value, size_t *value_len, int *ws_only, int *dynamic)
719{
720 char quot;
721
722 /* skip WS */
723 ign_xmlws(xmlctx);
724
725 /* skip '=' */
Michal Vasko63f3d842020-07-08 10:10:14 +0200726 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100727 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
728 return LY_EVALID;
Michal Vasko63f3d842020-07-08 10:10:14 +0200729 } else if (xmlctx->in->current[0] != '=') {
730 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
731 xmlctx->in->current, "'='");
Michal Vaskob36053d2020-03-26 15:49:30 +0100732 return LY_EVALID;
733 }
734 move_input(xmlctx, 1);
735
736 /* skip WS */
737 ign_xmlws(xmlctx);
738
739 /* find quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200740 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100741 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
742 return LY_EVALID;
Michal Vasko63f3d842020-07-08 10:10:14 +0200743 } else if ((xmlctx->in->current[0] != '\'') && (xmlctx->in->current[0] != '\"')) {
744 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
745 xmlctx->in->current, "either single or double quotation mark");
Michal Vaskob36053d2020-03-26 15:49:30 +0100746 return LY_EVALID;
747 }
748
749 /* remember quote */
Michal Vasko63f3d842020-07-08 10:10:14 +0200750 quot = xmlctx->in->current[0];
Michal Vaskob36053d2020-03-26 15:49:30 +0100751 move_input(xmlctx, 1);
752
753 /* parse attribute value */
754 LY_CHECK_RET(lyxml_parse_value(xmlctx, quot, (char **)value, value_len, ws_only, dynamic));
755
756 /* move after ending quote (without checking for EOF) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200757 ly_in_skip(xmlctx->in, 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100758
759 return LY_SUCCESS;
760}
761
Michal Vasko8cef5232020-06-15 17:59:47 +0200762/**
763 * @brief Move parser to the next attribute and parse it.
764 *
765 * @param[in] xmlctx XML context to use.
766 * @param[out] prefix Parsed attribute prefix.
767 * @param[out] prefix_len Length of @p prefix.
768 * @param[out] name Parsed attribute name.
769 * @param[out] name_len Length of @p name.
770 * @return LY_ERR value.
771 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100772static LY_ERR
773lyxml_next_attribute(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
774{
775 const char *in;
776 char *value;
777 uint32_t c;
778 size_t parsed, value_len;
779 int ws_only, dynamic;
780
781 /* skip WS */
782 ign_xmlws(xmlctx);
783
784 /* parse only possible attributes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200785 while ((xmlctx->in->current[0] != '>') && (xmlctx->in->current[0] != '/')) {
786 in = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +0100787 if (in[0] == '\0') {
788 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
789 return LY_EVALID;
790 } else if ((ly_getutf8(&in, &c, &parsed) || !is_xmlqnamestartchar(c))) {
791 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in - parsed), in - parsed,
792 "element tag end ('>' or '/>') or an attribute");
793 return LY_EVALID;
794 }
795
796 /* parse attribute name */
797 LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len));
798
799 if ((!*prefix || ly_strncmp("xmlns", *prefix, *prefix_len)) && (*prefix || ly_strncmp("xmlns", *name, *name_len))) {
800 /* standard attribute */
801 break;
802 }
803
804 /* namespace, skip it */
805 LY_CHECK_RET(lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic));
806 if (dynamic) {
807 free(value);
808 }
809
810 /* skip WS */
811 ign_xmlws(xmlctx);
812 }
813
814 return LY_SUCCESS;
815}
816
Michal Vasko8cef5232020-06-15 17:59:47 +0200817/**
818 * @brief Move parser to the next element and parse it.
819 *
820 * @param[in] xmlctx XML context to use.
821 * @param[out] prefix Parsed element prefix.
822 * @param[out] prefix_len Length of @p prefix.
823 * @param[out] name Parse element name.
824 * @param[out] name_len Length of @p name.
825 * @return LY_ERR value.
826 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100827static LY_ERR
828lyxml_next_element(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
829 int *closing)
830{
831 /* skip WS until EOF or after opening tag '<' */
832 LY_CHECK_RET(lyxml_skip_until_end_or_after_otag(xmlctx));
Michal Vasko63f3d842020-07-08 10:10:14 +0200833 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100834 /* set return values */
835 *prefix = *name = NULL;
836 *prefix_len = *name_len = 0;
837 return LY_SUCCESS;
838 }
839
Michal Vasko63f3d842020-07-08 10:10:14 +0200840 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100841 move_input(xmlctx, 1);
842 *closing = 1;
843 } else {
844 *closing = 0;
845 }
846
847 /* skip WS */
848 ign_xmlws(xmlctx);
849
850 /* parse element name */
851 LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len));
852
853 return LY_SUCCESS;
854}
855
856LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200857lyxml_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyxml_ctx **xmlctx_p)
Michal Vaskob36053d2020-03-26 15:49:30 +0100858{
859 LY_ERR ret = LY_SUCCESS;
860 struct lyxml_ctx *xmlctx;
861 int closing;
862
863 /* new context */
864 xmlctx = calloc(1, sizeof *xmlctx);
865 LY_CHECK_ERR_RET(!xmlctx, LOGMEM(ctx), LY_EMEM);
866 xmlctx->ctx = ctx;
867 xmlctx->line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200868 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100869
870 /* parse next element, if any */
871 LY_CHECK_GOTO(ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name,
872 &xmlctx->name_len, &closing), cleanup);
873
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 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
879 xmlctx->name_len, xmlctx->name);
880 ret = LY_EVALID;
881 goto cleanup;
882 } else {
883 /* open an element, also parses all enclosed namespaces */
884 LY_CHECK_GOTO(ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len), cleanup);
885
886 /* update status */
887 xmlctx->status = LYXML_ELEMENT;
888 }
889
890cleanup:
891 if (ret) {
892 lyxml_ctx_free(xmlctx);
893 } else {
894 *xmlctx_p = xmlctx;
895 }
896 return ret;
897}
898
899LY_ERR
900lyxml_ctx_next(struct lyxml_ctx *xmlctx)
901{
902 LY_ERR ret = LY_SUCCESS;
903 int closing;
904 struct lyxml_elem *e;
905
906 /* if the value was not used, free it */
907 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
908 free((char *)xmlctx->value);
909 xmlctx->value = NULL;
910 xmlctx->dynamic = 0;
911 }
912
913 switch (xmlctx->status) {
914 /* content |</elem> */
915 case LYXML_ELEM_CONTENT:
916 /* handle special case when empty content for "<elem/>" was returned */
Michal Vasko63f3d842020-07-08 10:10:14 +0200917 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100918 assert(xmlctx->elements.count);
919 e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
920
921 /* close the element (parses closing tag) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200922 ret = lyxml_close_element(xmlctx, e->prefix, e->prefix_len, e->name, e->name_len, 1);
923 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100924
925 /* update status */
926 xmlctx->status = LYXML_ELEM_CLOSE;
927 break;
928 }
929 /* fallthrough */
930
931 /* </elem>| <elem2>* */
932 case LYXML_ELEM_CLOSE:
933 /* parse next element, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200934 ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len, &closing);
935 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100936
Michal Vasko63f3d842020-07-08 10:10:14 +0200937 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100938 /* update status */
939 xmlctx->status = LYXML_END;
940 } else if (closing) {
941 /* close an element (parses also closing tag) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200942 ret = lyxml_close_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len, 0);
943 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100944
945 /* update status */
946 xmlctx->status = LYXML_ELEM_CLOSE;
947 } else {
948 /* open an element, also parses all enclosed namespaces */
Michal Vasko63f3d842020-07-08 10:10:14 +0200949 ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len);
950 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100951
952 /* update status */
953 xmlctx->status = LYXML_ELEMENT;
954 }
955 break;
956
957 /* <elem| attr='val'* > content */
958 case LYXML_ELEMENT:
959
960 /* attr='val'| attr='val'* > content */
961 case LYXML_ATTR_CONTENT:
962 /* parse attribute name, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +0200963 ret = lyxml_next_attribute(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len);
964 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100965
Michal Vasko63f3d842020-07-08 10:10:14 +0200966 if (xmlctx->in->current[0] == '>') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100967 /* no attributes but a closing tag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200968 ly_in_skip(xmlctx->in, 1);
969 if (!xmlctx->in->current[0]) {
Michal Vaskof55ae202020-06-30 15:49:36 +0200970 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
971 ret = LY_EVALID;
972 goto cleanup;
973 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100974
975 /* parse element content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200976 ret = lyxml_parse_value(xmlctx, '<', (char **)&xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only,
977 &xmlctx->dynamic);
978 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100979
980 if (!xmlctx->value_len) {
981 /* use empty value, easier to work with */
982 xmlctx->value = "";
983 assert(!xmlctx->dynamic);
984 }
985
986 /* update status */
987 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vasko63f3d842020-07-08 10:10:14 +0200988 } else if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +0100989 /* no content but we still return it */
990 xmlctx->value = "";
991 xmlctx->value_len = 0;
992 xmlctx->ws_only = 1;
993 xmlctx->dynamic = 0;
994
995 /* update status */
996 xmlctx->status = LYXML_ELEM_CONTENT;
997 } else {
998 /* update status */
999 xmlctx->status = LYXML_ATTRIBUTE;
1000 }
1001 break;
1002
1003 /* attr|='val' */
1004 case LYXML_ATTRIBUTE:
1005 /* skip formatting and parse value */
Michal Vasko63f3d842020-07-08 10:10:14 +02001006 ret = lyxml_next_attr_content(xmlctx, &xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only, &xmlctx->dynamic);
1007 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001008
1009 /* update status */
1010 xmlctx->status = LYXML_ATTR_CONTENT;
1011 break;
1012
1013 /* </elem> |EOF */
1014 case LYXML_END:
1015 /* nothing to do */
1016 break;
1017 }
1018
1019cleanup:
1020 if (ret) {
1021 /* invalidate context */
1022 xmlctx->status = LYXML_END;
1023 }
1024 return ret;
1025}
1026
1027LY_ERR
1028lyxml_ctx_peek(struct lyxml_ctx *xmlctx, enum LYXML_PARSER_STATUS *next)
1029{
1030 LY_ERR ret = LY_SUCCESS;
1031 const char *prefix, *name, *prev_input;
1032 size_t prefix_len, name_len;
1033 int closing;
1034
Michal Vasko63f3d842020-07-08 10:10:14 +02001035 prev_input = xmlctx->in->current;
Michal Vaskob36053d2020-03-26 15:49:30 +01001036
1037 switch (xmlctx->status) {
1038 case LYXML_ELEM_CONTENT:
Michal Vasko63f3d842020-07-08 10:10:14 +02001039 if (xmlctx->in->current[0] == '/') {
Michal Vaskob36053d2020-03-26 15:49:30 +01001040 *next = LYXML_ELEM_CLOSE;
1041 break;
1042 }
1043 /* fallthrough */
1044 case LYXML_ELEM_CLOSE:
1045 /* parse next element, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +02001046 ret = lyxml_next_element(xmlctx, &prefix, &prefix_len, &name, &name_len, &closing);
1047 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001048
Michal Vasko63f3d842020-07-08 10:10:14 +02001049 if (xmlctx->in->current[0] == '\0') {
Michal Vaskob36053d2020-03-26 15:49:30 +01001050 *next = LYXML_END;
1051 } else if (closing) {
1052 *next = LYXML_ELEM_CLOSE;
1053 } else {
1054 *next = LYXML_ELEMENT;
1055 }
1056 break;
1057 case LYXML_ELEMENT:
1058 case LYXML_ATTR_CONTENT:
1059 /* parse attribute name, if any */
Michal Vasko63f3d842020-07-08 10:10:14 +02001060 ret = lyxml_next_attribute(xmlctx, &prefix, &prefix_len, &name, &name_len);
1061 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +01001062
Michal Vasko63f3d842020-07-08 10:10:14 +02001063 if ((xmlctx->in->current[0] == '>') || (xmlctx->in->current[0] == '/')) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001064 *next = LYXML_ELEM_CONTENT;
1065 } else {
1066 *next = LYXML_ATTRIBUTE;
1067 }
1068 break;
1069 case LYXML_ATTRIBUTE:
1070 *next = LYXML_ATTR_CONTENT;
1071 break;
1072 case LYXML_END:
1073 *next = LYXML_END;
1074 break;
1075 }
1076
1077cleanup:
Michal Vasko63f3d842020-07-08 10:10:14 +02001078 xmlctx->in->current = prev_input;
Michal Vaskob36053d2020-03-26 15:49:30 +01001079 return ret;
1080}
1081
1082void
1083lyxml_ctx_free(struct lyxml_ctx *xmlctx)
1084{
1085 uint32_t u;
1086
1087 if (!xmlctx) {
1088 return;
1089 }
1090
1091 if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) {
1092 free((char *)xmlctx->value);
1093 }
1094 ly_set_erase(&xmlctx->elements, free);
1095 for (u = xmlctx->ns.count - 1; u + 1 > 0; --u) {
1096 /* remove the ns structure */
1097 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix);
1098 free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri);
1099 free(xmlctx->ns.objs[u]);
1100 }
1101 ly_set_erase(&xmlctx->ns, NULL);
1102 free(xmlctx);
Radek Krejcib1890642018-10-03 14:05:40 +02001103}
Radek Krejcie7b95092019-05-15 11:03:07 +02001104
1105LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02001106lyxml_dump_text(struct ly_out *out, const char *text, int attribute)
Radek Krejcie7b95092019-05-15 11:03:07 +02001107{
Radek Krejcibaeb8382020-05-27 16:44:53 +02001108 ssize_t ret = LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +02001109 unsigned int u;
1110
1111 if (!text) {
1112 return 0;
1113 }
1114
1115 for (u = 0; text[u]; u++) {
1116 switch (text[u]) {
1117 case '&':
Radek Krejci241f6b52020-05-21 18:13:49 +02001118 ret = ly_print(out, "&amp;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001119 break;
1120 case '<':
Radek Krejci241f6b52020-05-21 18:13:49 +02001121 ret = ly_print(out, "&lt;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001122 break;
1123 case '>':
1124 /* not needed, just for readability */
Radek Krejci241f6b52020-05-21 18:13:49 +02001125 ret = ly_print(out, "&gt;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001126 break;
1127 case '"':
1128 if (attribute) {
Radek Krejci241f6b52020-05-21 18:13:49 +02001129 ret = ly_print(out, "&quot;");
Radek Krejcie7b95092019-05-15 11:03:07 +02001130 break;
1131 }
1132 /* falls through */
1133 default:
Radek Krejcibaeb8382020-05-27 16:44:53 +02001134 ret = ly_write(out, &text[u], 1);
Radek Krejcie7b95092019-05-15 11:03:07 +02001135 }
1136 }
1137
Radek Krejcibaeb8382020-05-27 16:44:53 +02001138 return ret < 0 ? (-1 * ret) : 0;
Radek Krejcie7b95092019-05-15 11:03:07 +02001139}
1140
Michal Vasko52927e22020-03-16 17:26:14 +01001141LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001142lyxml_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 +01001143{
1144 LY_ERR ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001145 LY_ARRAY_COUNT_TYPE u;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001146 uint32_t c;
Michal Vasko52927e22020-03-16 17:26:14 +01001147 const struct lyxml_ns *ns;
1148 const char *start, *stop;
1149 struct ly_prefix *prefixes = NULL;
1150 size_t len;
1151
1152 for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) {
1153 size_t bytes;
1154 ly_getutf8(&stop, &c, &bytes);
1155 if (is_xmlqnamestartchar(c)) {
1156 for (ly_getutf8(&stop, &c, &bytes);
1157 is_xmlqnamechar(c) && (size_t)(stop - value) < value_len;
1158 ly_getutf8(&stop, &c, &bytes));
1159 stop = stop - bytes;
1160 if (*stop == ':') {
1161 /* we have a possible prefix */
1162 len = stop - start;
Michal Vaskob36053d2020-03-26 15:49:30 +01001163 ns = lyxml_ns_get(xmlctx, start, len);
Michal Vasko52927e22020-03-16 17:26:14 +01001164 if (ns) {
1165 struct ly_prefix *p = NULL;
1166
1167 /* check whether we do not already have this prefix stored */
1168 LY_ARRAY_FOR(prefixes, u) {
1169 if (!ly_strncmp(prefixes[u].pref, start, len)) {
1170 p = &prefixes[u];
1171 break;
1172 }
1173 }
1174 if (!p) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001175 LY_ARRAY_NEW_GOTO(xmlctx->ctx, prefixes, p, ret, error);
1176 p->pref = lydict_insert(xmlctx->ctx, start, len);
1177 p->ns = lydict_insert(xmlctx->ctx, ns->uri, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01001178 } /* else the prefix already present */
1179 }
1180 }
1181 stop = stop + bytes;
1182 }
1183 }
1184
1185 *val_prefs = prefixes;
1186 return LY_SUCCESS;
1187
1188error:
1189 LY_ARRAY_FOR(prefixes, u) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001190 lydict_remove(xmlctx->ctx, prefixes[u].pref);
Michal Vasko52927e22020-03-16 17:26:14 +01001191 }
1192 LY_ARRAY_FREE(prefixes);
1193 return ret;
1194}
1195
1196LY_ERR
1197lyxml_value_compare(const char *value1, const struct ly_prefix *prefs1, const char *value2, const struct ly_prefix *prefs2)
1198{
1199 const char *ptr1, *ptr2, *ns1, *ns2;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001200 LY_ARRAY_COUNT_TYPE u1, u2;
Michal Vasko52927e22020-03-16 17:26:14 +01001201 int len;
1202
1203 if (!value1 && !value2) {
1204 return LY_SUCCESS;
1205 }
1206 if ((value1 && !value2) || (!value1 && value2)) {
1207 return LY_ENOT;
1208 }
1209
1210 ptr1 = value1;
1211 ptr2 = value2;
1212 while (ptr1[0] && ptr2[0]) {
1213 if (ptr1[0] != ptr2[0]) {
1214 /* it can be a start of prefix that maps to the same module */
1215 ns1 = ns2 = NULL;
1216 if (prefs1) {
1217 /* find module of the first prefix, if any */
1218 LY_ARRAY_FOR(prefs1, u1) {
1219 len = strlen(prefs1[u1].pref);
1220 if (!strncmp(ptr1, prefs1[u1].pref, len) && (ptr1[len] == ':')) {
1221 ns1 = prefs1[u1].ns;
1222 break;
1223 }
1224 }
1225 }
1226 if (prefs2) {
1227 /* find module of the second prefix, if any */
1228 LY_ARRAY_FOR(prefs2, u2) {
1229 len = strlen(prefs2[u2].pref);
1230 if (!strncmp(ptr2, prefs2[u2].pref, len) && (ptr2[len] == ':')) {
1231 ns2 = prefs2[u2].ns;
1232 break;
1233 }
1234 }
1235 }
1236
1237 if (!ns1 || !ns2 || (ns1 != ns2)) {
1238 /* not a prefix or maps to different namespaces */
1239 break;
1240 }
1241
1242 /* skip prefixes in both values (':' is skipped as iter) */
1243 ptr1 += strlen(prefs1[u1].pref);
1244 ptr2 += strlen(prefs2[u2].pref);
1245 }
1246
1247 ++ptr1;
1248 ++ptr2;
1249 }
1250 if (ptr1[0] || ptr2[0]) {
1251 /* not a match or simply different lengths */
1252 return LY_ENOT;
1253 }
1254
1255 return LY_SUCCESS;
1256}