Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file xml.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 5 | * @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 Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include "xml.h" |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 19 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 20 | #include <assert.h> |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 21 | #include <ctype.h> |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 24 | #include <string.h> |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 25 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 26 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 27 | #include "compat.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 28 | #include "in_internal.h" |
| 29 | #include "out_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 30 | #include "tree.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 31 | #include "tree_schema_internal.h" |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 32 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 33 | /* Move input p by s characters, if EOF log with lyxml_ctx c */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 34 | #define move_input(c, s) \ |
| 35 | ly_in_skip(c->in, s); \ |
| 36 | LY_CHECK_ERR_RET(!c->in->current[0], LOGVAL(c->ctx, LY_VCODE_EOF), LY_EVALID) |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 37 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 38 | /* Ignore whitespaces in the input string p */ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 39 | #define ign_xmlws(c) \ |
| 40 | while (is_xmlws(*(c)->in->current)) { \ |
| 41 | if (*(c)->in->current == '\n') { \ |
| 42 | LY_IN_NEW_LINE((c)->in); \ |
| 43 | } \ |
| 44 | ly_in_skip(c->in, 1); \ |
| 45 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 46 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 47 | static LY_ERR lyxml_next_attr_content(struct lyxml_ctx *xmlctx, const char **value, size_t *value_len, ly_bool *ws_only, |
| 48 | ly_bool *dynamic); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 49 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 50 | /** |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 51 | * @brief Ignore and skip any characters until the delim of the size delim_len is read, including the delim |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 52 | * |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 53 | * @param[in] xmlctx XML parser context to provide input handler and libyang context |
| 54 | * @param[in] in input handler to read the data, it is updated only in case the section is correctly terminated. |
| 55 | * @param[in] delim Delimiter to detect end of the section. |
| 56 | * @param[in] delim_len Length of the delimiter string to use. |
| 57 | * @param[in] sectname Section name to refer in error message. |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 58 | */ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 59 | LY_ERR |
| 60 | skip_section(struct lyxml_ctx *xmlctx, const char *delim, size_t delim_len, const char *sectname) |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 61 | { |
| 62 | size_t i; |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 63 | register const char *input, *a, *b; |
| 64 | uint64_t parsed = 0, newlines = 0; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 65 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 66 | for (input = xmlctx->in->current; *input; ++input, ++parsed) { |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 67 | if (*input != *delim) { |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 68 | if (*input == '\n') { |
| 69 | ++newlines; |
| 70 | } |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 71 | continue; |
| 72 | } |
| 73 | a = input; |
| 74 | b = delim; |
| 75 | for (i = 0; i < delim_len; ++i) { |
| 76 | if (*a++ != *b++) { |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | if (i == delim_len) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 81 | /* delim found */ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 82 | xmlctx->in->line += newlines; |
| 83 | ly_in_skip(xmlctx->in, parsed + delim_len); |
| 84 | return LY_SUCCESS; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 85 | } |
| 86 | } |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 87 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 88 | /* delim not found, |
| 89 | * do not update input handler to refer to the beginning of the section in error message */ |
| 90 | LOGVAL(xmlctx->ctx, LY_VCODE_NTERM, sectname); |
| 91 | return LY_EVALID; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 94 | /** |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 95 | * @brief Check/Get an XML identifier from the input string. |
| 96 | * |
| 97 | * The identifier must have at least one valid character complying the name start character constraints. |
| 98 | * The identifier is terminated by the first character, which does not comply to the name character constraints. |
| 99 | * |
| 100 | * See https://www.w3.org/TR/xml-names/#NT-NCName |
| 101 | * |
| 102 | * @param[in] xmlctx XML context. |
| 103 | * @param[out] start Pointer to the start of the identifier. |
| 104 | * @param[out] end Pointer ot the end of the identifier. |
| 105 | * @return LY_ERR value. |
| 106 | */ |
| 107 | static LY_ERR |
| 108 | lyxml_parse_identifier(struct lyxml_ctx *xmlctx, const char **start, const char **end) |
| 109 | { |
| 110 | const char *s, *in; |
| 111 | uint32_t c; |
| 112 | size_t parsed; |
| 113 | LY_ERR rc; |
| 114 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 115 | in = s = xmlctx->in->current; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 116 | |
| 117 | /* check NameStartChar (minus colon) */ |
| 118 | LY_CHECK_ERR_RET(ly_getutf8(&in, &c, &parsed), |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 119 | LOGVAL(xmlctx->ctx, LY_VCODE_INCHAR, in[0]), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 120 | LY_EVALID); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 121 | LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c), |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 122 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Identifier \"%s\" starts with an invalid character.", in - parsed), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 123 | LY_EVALID); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 124 | |
| 125 | /* check rest of the identifier */ |
| 126 | do { |
| 127 | /* move only successfully parsed bytes */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 128 | ly_in_skip(xmlctx->in, parsed); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 129 | |
| 130 | rc = ly_getutf8(&in, &c, &parsed); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 131 | LY_CHECK_ERR_RET(rc, LOGVAL(xmlctx->ctx, LY_VCODE_INCHAR, in[0]), LY_EVALID); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 132 | } while (is_xmlqnamechar(c)); |
| 133 | |
| 134 | *start = s; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 135 | *end = xmlctx->in->current; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 136 | return LY_SUCCESS; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @brief Add namespace definition into XML context. |
| 141 | * |
| 142 | * Namespaces from a single element are supposed to be added sequentially together (not interleaved by a namespace from other |
| 143 | * element). This mimic namespace visibility, since the namespace defined in element E is not visible from its parents or |
| 144 | * siblings. On the other hand, namespace from a parent element can be redefined in a child element. This is also reflected |
| 145 | * by lyxml_ns_get() which returns the most recent namespace definition for the given prefix. |
| 146 | * |
| 147 | * When leaving processing of a subtree of some element (after it is removed from xmlctx->elements), caller is supposed to call |
| 148 | * lyxml_ns_rm() to remove all the namespaces defined in such an element from the context. |
| 149 | * |
| 150 | * @param[in] xmlctx XML context to work with. |
| 151 | * @param[in] prefix Pointer to the namespace prefix. Can be NULL for default namespace. |
| 152 | * @param[in] prefix_len Length of the prefix. |
| 153 | * @param[in] uri Namespace URI (value) to store directly. Value is always spent. |
| 154 | * @return LY_ERR values. |
| 155 | */ |
| 156 | LY_ERR |
| 157 | lyxml_ns_add(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, char *uri) |
| 158 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 159 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 160 | struct lyxml_ns *ns; |
| 161 | |
| 162 | ns = malloc(sizeof *ns); |
| 163 | LY_CHECK_ERR_RET(!ns, LOGMEM(xmlctx->ctx), LY_EMEM); |
| 164 | |
| 165 | /* we need to connect the depth of the element where the namespace is defined with the |
| 166 | * namespace record to be able to maintain (remove) the record when the parser leaves |
| 167 | * (to its sibling or back to the parent) the element where the namespace was defined */ |
| 168 | ns->depth = xmlctx->elements.count; |
| 169 | |
| 170 | ns->uri = uri; |
| 171 | if (prefix) { |
| 172 | ns->prefix = strndup(prefix, prefix_len); |
| 173 | LY_CHECK_ERR_RET(!ns->prefix, LOGMEM(xmlctx->ctx); free(ns->uri); free(ns), LY_EMEM); |
| 174 | } else { |
| 175 | ns->prefix = NULL; |
| 176 | } |
| 177 | |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 178 | ret = ly_set_add(&xmlctx->ns, ns, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 179 | LY_CHECK_ERR_RET(ret, free(ns->prefix); free(ns->uri); free(ns), ret); |
| 180 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 181 | return LY_SUCCESS; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @brief Remove all the namespaces defined in the element recently closed (removed from the xmlctx->elements). |
| 186 | * |
| 187 | * @param[in] xmlctx XML context to work with. |
| 188 | */ |
| 189 | void |
| 190 | lyxml_ns_rm(struct lyxml_ctx *xmlctx) |
| 191 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 192 | for (uint32_t u = xmlctx->ns.count - 1; u + 1 > 0; --u) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 193 | if (((struct lyxml_ns *)xmlctx->ns.objs[u])->depth != xmlctx->elements.count + 1) { |
| 194 | /* we are done, the namespaces from a single element are supposed to be together */ |
| 195 | break; |
| 196 | } |
| 197 | /* remove the ns structure */ |
| 198 | free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix); |
| 199 | free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri); |
| 200 | free(xmlctx->ns.objs[u]); |
| 201 | --xmlctx->ns.count; |
| 202 | } |
| 203 | |
| 204 | if (!xmlctx->ns.count) { |
| 205 | /* cleanup the xmlctx's namespaces storage */ |
| 206 | ly_set_erase(&xmlctx->ns, NULL); |
| 207 | } |
| 208 | } |
| 209 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 210 | const struct lyxml_ns * |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 211 | lyxml_ns_get(const struct ly_set *ns_set, const char *prefix, size_t prefix_len) |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 212 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 213 | struct lyxml_ns *ns; |
| 214 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 215 | for (uint32_t u = ns_set->count - 1; u + 1 > 0; --u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 216 | ns = (struct lyxml_ns *)ns_set->objs[u]; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 217 | if (prefix && prefix_len) { |
| 218 | if (ns->prefix && !ly_strncmp(ns->prefix, prefix, prefix_len)) { |
| 219 | return ns; |
| 220 | } |
| 221 | } else if (!ns->prefix) { |
| 222 | /* default namespace */ |
| 223 | return ns; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return NULL; |
| 228 | } |
| 229 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 230 | /** |
| 231 | * @brief Skip in the input until EOF or just after the opening tag. |
| 232 | * Handles special XML constructs (comment, cdata, doctype). |
| 233 | * |
| 234 | * @param[in] xmlctx XML context to use. |
| 235 | * @return LY_ERR value. |
| 236 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 237 | static LY_ERR |
| 238 | lyxml_skip_until_end_or_after_otag(struct lyxml_ctx *xmlctx) |
| 239 | { |
| 240 | const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 241 | const char *endtag, *sectname; |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 242 | size_t endtag_len; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 243 | |
| 244 | while (1) { |
| 245 | ign_xmlws(xmlctx); |
| 246 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 247 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 248 | /* EOF */ |
| 249 | if (xmlctx->elements.count) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 250 | LOGVAL(ctx, LY_VCODE_EOF); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 251 | return LY_EVALID; |
| 252 | } |
| 253 | return LY_SUCCESS; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 254 | } else if (xmlctx->in->current[0] != '<') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 255 | LOGVAL(ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 256 | xmlctx->in->current, "element tag start ('<')"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 257 | return LY_EVALID; |
| 258 | } |
| 259 | move_input(xmlctx, 1); |
| 260 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 261 | if (xmlctx->in->current[0] == '!') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 262 | move_input(xmlctx, 1); |
| 263 | /* sections to ignore */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 264 | if (!strncmp(xmlctx->in->current, "--", 2)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 265 | /* comment */ |
| 266 | move_input(xmlctx, 2); |
| 267 | sectname = "Comment"; |
| 268 | endtag = "-->"; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 269 | endtag_len = ly_strlen_const("-->"); |
| 270 | } else if (!strncmp(xmlctx->in->current, "[CDATA[", ly_strlen_const("[CDATA["))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 271 | /* CDATA section */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 272 | move_input(xmlctx, ly_strlen_const("[CDATA[")); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 273 | sectname = "CData"; |
| 274 | endtag = "]]>"; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 275 | endtag_len = ly_strlen_const("]]>"); |
| 276 | } else if (!strncmp(xmlctx->in->current, "DOCTYPE", ly_strlen_const("DOCTYPE"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 277 | /* Document type declaration - not supported */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 278 | LOGVAL(ctx, LY_VCODE_NSUPP, "Document Type Declaration"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 279 | return LY_EVALID; |
| 280 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 281 | LOGVAL(ctx, LYVE_SYNTAX, "Unknown XML section \"%.20s\".", &xmlctx->in->current[-2]); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 282 | return LY_EVALID; |
| 283 | } |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 284 | LY_CHECK_RET(skip_section(xmlctx, endtag, endtag_len, sectname)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 285 | } else if (xmlctx->in->current[0] == '?') { |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 286 | LY_CHECK_RET(skip_section(xmlctx, "?>", 2, "Declaration")); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 287 | } else { |
| 288 | /* other non-WS character */ |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return LY_SUCCESS; |
| 294 | } |
| 295 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 296 | /** |
| 297 | * @brief Parse QName. |
| 298 | * |
| 299 | * @param[in] xmlctx XML context to use. |
| 300 | * @param[out] prefix Parsed prefix, may be NULL. |
| 301 | * @param[out] prefix_len Length of @p prefix. |
| 302 | * @param[out] name Parsed name. |
| 303 | * @param[out] name_len Length of @p name. |
| 304 | * @return LY_ERR value. |
| 305 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 306 | static LY_ERR |
| 307 | lyxml_parse_qname(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 308 | { |
| 309 | const char *start, *end; |
| 310 | |
| 311 | *prefix = NULL; |
| 312 | *prefix_len = 0; |
| 313 | |
| 314 | LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end)); |
| 315 | if (end[0] == ':') { |
| 316 | /* we have prefixed identifier */ |
| 317 | *prefix = start; |
| 318 | *prefix_len = end - start; |
| 319 | |
| 320 | move_input(xmlctx, 1); |
| 321 | LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end)); |
| 322 | } |
| 323 | |
| 324 | *name = start; |
| 325 | *name_len = end - start; |
| 326 | return LY_SUCCESS; |
| 327 | } |
| 328 | |
| 329 | /** |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 330 | * @brief Parse XML text content (value). |
| 331 | * |
| 332 | * @param[in] xmlctx XML context to use. |
| 333 | * @param[in] endchar Expected character to mark value end. |
| 334 | * @param[out] value Parsed value. |
| 335 | * @param[out] length Length of @p value. |
| 336 | * @param[out] ws_only Whether the value is empty/white-spaces only. |
| 337 | * @param[out] dynamic Whether the value was dynamically allocated. |
| 338 | * @return LY_ERR value. |
| 339 | */ |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 340 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 341 | lyxml_parse_value(struct lyxml_ctx *xmlctx, char endchar, char **value, size_t *length, ly_bool *ws_only, ly_bool *dynamic) |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 342 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 343 | #define BUFSIZE 24 |
| 344 | #define BUFSIZE_STEP 128 |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 345 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 346 | const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */ |
aPiecek | b287b21 | 2021-05-04 14:24:25 +0200 | [diff] [blame] | 347 | const char *in = xmlctx->in->current, *start, *in_aux; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 348 | char *buf = NULL; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 349 | size_t offset; /* read offset in input buffer */ |
| 350 | size_t len; /* length of the output string (write offset in output buffer) */ |
| 351 | size_t size = 0; /* size of the output buffer */ |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 352 | void *p; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 353 | uint32_t n; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 354 | size_t u; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 355 | ly_bool ws = 1; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 356 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 357 | assert(xmlctx); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 358 | |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 359 | /* init */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 360 | start = in; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 361 | offset = len = 0; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 362 | |
| 363 | /* parse */ |
| 364 | while (in[offset]) { |
| 365 | if (in[offset] == '&') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 366 | /* non WS */ |
| 367 | ws = 0; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 368 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 369 | if (!buf) { |
| 370 | /* prepare output buffer */ |
| 371 | buf = malloc(BUFSIZE); |
| 372 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM); |
| 373 | size = BUFSIZE; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 374 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 375 | |
| 376 | /* allocate enough for the offset and next character, |
| 377 | * we will need 4 bytes at most since we support only the predefined |
| 378 | * (one-char) entities and character references */ |
Juraj Vijtiuk | cb017cc | 2020-07-08 16:19:58 +0200 | [diff] [blame] | 379 | while (len + offset + 4 >= size) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 380 | buf = ly_realloc(buf, size + BUFSIZE_STEP); |
| 381 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM); |
| 382 | size += BUFSIZE_STEP; |
| 383 | } |
| 384 | |
| 385 | if (offset) { |
| 386 | /* store what we have so far */ |
| 387 | memcpy(&buf[len], in, offset); |
| 388 | len += offset; |
| 389 | in += offset; |
| 390 | offset = 0; |
| 391 | } |
| 392 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 393 | ++offset; |
| 394 | if (in[offset] != '#') { |
| 395 | /* entity reference - only predefined references are supported */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 396 | if (!strncmp(&in[offset], "lt;", ly_strlen_const("lt;"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 397 | buf[len++] = '<'; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 398 | in += ly_strlen_const("<"); |
| 399 | } else if (!strncmp(&in[offset], "gt;", ly_strlen_const("gt;"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 400 | buf[len++] = '>'; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 401 | in += ly_strlen_const(">"); |
| 402 | } else if (!strncmp(&in[offset], "amp;", ly_strlen_const("amp;"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 403 | buf[len++] = '&'; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 404 | in += ly_strlen_const("&"); |
| 405 | } else if (!strncmp(&in[offset], "apos;", ly_strlen_const("apos;"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 406 | buf[len++] = '\''; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 407 | in += ly_strlen_const("'"); |
| 408 | } else if (!strncmp(&in[offset], "quot;", ly_strlen_const("quot;"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 409 | buf[len++] = '\"'; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 410 | in += ly_strlen_const("""); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 411 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 412 | LOGVAL(ctx, LYVE_SYNTAX, "Entity reference \"%.*s\" not supported, only predefined references allowed.", |
| 413 | 10, &in[offset - 1]); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 414 | goto error; |
| 415 | } |
| 416 | offset = 0; |
| 417 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 418 | p = (void *)&in[offset - 1]; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 419 | /* character reference */ |
| 420 | ++offset; |
| 421 | if (isdigit(in[offset])) { |
| 422 | for (n = 0; isdigit(in[offset]); offset++) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 423 | n = (LY_BASE_DEC * n) + (in[offset] - '0'); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 424 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 425 | } else if ((in[offset] == 'x') && isxdigit(in[offset + 1])) { |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 426 | for (n = 0, ++offset; isxdigit(in[offset]); offset++) { |
| 427 | if (isdigit(in[offset])) { |
| 428 | u = (in[offset] - '0'); |
| 429 | } else if (in[offset] > 'F') { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 430 | u = LY_BASE_DEC + (in[offset] - 'a'); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 431 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 432 | u = LY_BASE_DEC + (in[offset] - 'A'); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 433 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 434 | n = (LY_BASE_HEX * n) + u; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 435 | } |
| 436 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 437 | LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 438 | goto error; |
| 439 | |
| 440 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 441 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 442 | LY_CHECK_ERR_GOTO(in[offset] != ';', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 443 | LOGVAL(ctx, LY_VCODE_INSTREXP, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 444 | LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"), |
| 445 | error); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 446 | ++offset; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 447 | LY_CHECK_ERR_GOTO(ly_pututf8(&buf[len], n, &u), |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 448 | LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 449 | error); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 450 | len += u; |
| 451 | in += offset; |
| 452 | offset = 0; |
| 453 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 454 | } else if (in[offset] == endchar) { |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 455 | /* end of string */ |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 456 | if (buf) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 457 | /* realloc exact size string */ |
| 458 | buf = ly_realloc(buf, len + offset + 1); |
| 459 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM); |
| 460 | size = len + offset + 1; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame^] | 461 | if (offset) { |
| 462 | memcpy(&buf[len], in, offset); |
| 463 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 464 | |
| 465 | /* set terminating NULL byte */ |
| 466 | buf[len + offset] = '\0'; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 467 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 468 | len += offset; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 469 | in += offset; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 470 | goto success; |
| 471 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 472 | if (!is_xmlws(in[offset])) { |
| 473 | /* non WS */ |
| 474 | ws = 0; |
| 475 | } |
| 476 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 477 | /* log lines */ |
| 478 | if (in[offset] == '\n') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 479 | LY_IN_NEW_LINE(xmlctx->in); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* continue */ |
aPiecek | b287b21 | 2021-05-04 14:24:25 +0200 | [diff] [blame] | 483 | in_aux = &in[offset]; |
| 484 | LY_CHECK_ERR_GOTO(ly_getutf8(&in_aux, &n, &u), |
| 485 | LOGVAL(ctx, LY_VCODE_INCHAR, in[offset]), error); |
| 486 | offset += u; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 487 | } |
| 488 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 489 | |
| 490 | /* EOF reached before endchar */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 491 | LOGVAL(ctx, LY_VCODE_EOF); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 492 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 493 | error: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 494 | free(buf); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 495 | return LY_EVALID; |
| 496 | |
| 497 | success: |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 498 | if (buf) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 499 | *value = buf; |
| 500 | *dynamic = 1; |
| 501 | } else { |
| 502 | *value = (char *)start; |
| 503 | *dynamic = 0; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 504 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 505 | *length = len; |
| 506 | *ws_only = ws; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 507 | |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 508 | xmlctx->in->current = in; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 509 | return LY_SUCCESS; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 510 | |
| 511 | #undef BUFSIZE |
| 512 | #undef BUFSIZE_STEP |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 513 | } |
| 514 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 515 | /** |
| 516 | * @brief Parse XML closing element and match it to a stored starting element. |
| 517 | * |
| 518 | * @param[in] xmlctx XML context to use. |
| 519 | * @param[in] prefix Expected closing element prefix. |
| 520 | * @param[in] prefix_len Length of @p prefix. |
| 521 | * @param[in] name Expected closing element name. |
| 522 | * @param[in] name_len Length of @p name. |
| 523 | * @param[in] empty Whether we are parsing a special "empty" element (with joined starting and closing tag) with no value. |
| 524 | * @return LY_ERR value. |
| 525 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 526 | static LY_ERR |
| 527 | lyxml_close_element(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, const char *name, size_t name_len, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 528 | ly_bool empty) |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 529 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 530 | struct lyxml_elem *e; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 531 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 532 | /* match opening and closing element tags */ |
| 533 | if (!xmlctx->elements.count) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 534 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 535 | (int)name_len, name); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 536 | return LY_EVALID; |
| 537 | } |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 538 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 539 | e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 540 | if ((e->prefix_len != prefix_len) || (e->name_len != name_len) || |
| 541 | (prefix_len && strncmp(prefix, e->prefix, e->prefix_len)) || strncmp(name, e->name, e->name_len)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 542 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Opening (\"%.*s%s%.*s\") and closing (\"%.*s%s%.*s\") elements tag mismatch.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 543 | (int)e->prefix_len, e->prefix ? e->prefix : "", e->prefix ? ":" : "", (int)e->name_len, e->name, |
| 544 | (int)prefix_len, prefix ? prefix : "", prefix ? ":" : "", (int)name_len, name); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 545 | return LY_EVALID; |
| 546 | } |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 547 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 548 | /* opening and closing element tags matches, remove record from the opening tags list */ |
| 549 | ly_set_rm_index(&xmlctx->elements, xmlctx->elements.count - 1, free); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 550 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 551 | /* remove also the namespaces connected with the element */ |
| 552 | lyxml_ns_rm(xmlctx); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 553 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 554 | /* skip WS */ |
| 555 | ign_xmlws(xmlctx); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 556 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 557 | /* special "<elem/>" element */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 558 | if (empty && (xmlctx->in->current[0] == '/')) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 559 | move_input(xmlctx, 1); |
| 560 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 561 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 562 | /* parse closing tag */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 563 | if (xmlctx->in->current[0] != '>') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 564 | LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 565 | xmlctx->in->current, "element tag termination ('>')"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 566 | return LY_EVALID; |
| 567 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 568 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 569 | /* move after closing tag without checking for EOF */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 570 | ly_in_skip(xmlctx->in, 1); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 571 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 572 | return LY_SUCCESS; |
| 573 | } |
| 574 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 575 | /** |
| 576 | * @brief Store parsed opening element and parse any included namespaces. |
| 577 | * |
| 578 | * @param[in] xmlctx XML context to use. |
| 579 | * @param[in] prefix Parsed starting element prefix. |
| 580 | * @param[in] prefix_len Length of @p prefix. |
| 581 | * @param[in] name Parsed starting element name. |
| 582 | * @param[in] name_len Length of @p name. |
| 583 | * @return LY_ERR value. |
| 584 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 585 | static LY_ERR |
| 586 | lyxml_open_element(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, const char *name, size_t name_len) |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 587 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 588 | LY_ERR ret = LY_SUCCESS; |
| 589 | struct lyxml_elem *e; |
| 590 | const char *prev_input; |
| 591 | char *value; |
| 592 | size_t parsed, value_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 593 | ly_bool ws_only, dynamic, is_ns; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 594 | uint32_t c; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 595 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 596 | /* store element opening tag information */ |
| 597 | e = malloc(sizeof *e); |
| 598 | LY_CHECK_ERR_RET(!e, LOGMEM(xmlctx->ctx), LY_EMEM); |
| 599 | e->name = name; |
| 600 | e->prefix = prefix; |
| 601 | e->name_len = name_len; |
| 602 | e->prefix_len = prefix_len; |
aPiecek | 93582ed | 2021-05-25 14:49:06 +0200 | [diff] [blame] | 603 | |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 604 | LY_CHECK_RET(ly_set_add(&xmlctx->elements, e, 1, NULL)); |
aPiecek | 93582ed | 2021-05-25 14:49:06 +0200 | [diff] [blame] | 605 | if (xmlctx->elements.count > LY_MAX_BLOCK_DEPTH) { |
| 606 | LOGERR(xmlctx->ctx, LY_EINVAL, |
| 607 | "The maximum number of open elements has been exceeded."); |
| 608 | ret = LY_EINVAL; |
| 609 | goto cleanup; |
| 610 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 611 | |
| 612 | /* skip WS */ |
| 613 | ign_xmlws(xmlctx); |
| 614 | |
| 615 | /* parse and store all namespaces */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 616 | prev_input = xmlctx->in->current; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 617 | is_ns = 1; |
aPiecek | 785ad3d | 2021-05-10 15:51:13 +0200 | [diff] [blame] | 618 | while ((xmlctx->in->current[0] != '\0') && !(ret = ly_getutf8(&xmlctx->in->current, &c, &parsed))) { |
| 619 | if (!is_xmlqnamestartchar(c)) { |
| 620 | break; |
| 621 | } |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 622 | xmlctx->in->current -= parsed; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 623 | |
| 624 | /* parse attribute name */ |
| 625 | LY_CHECK_GOTO(ret = lyxml_parse_qname(xmlctx, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 626 | |
| 627 | /* parse the value */ |
| 628 | LY_CHECK_GOTO(ret = lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic), cleanup); |
| 629 | |
| 630 | /* store every namespace */ |
| 631 | if ((prefix && !ly_strncmp("xmlns", prefix, prefix_len)) || (!prefix && !ly_strncmp("xmlns", name, name_len))) { |
Radek IÅ¡a | 017270d | 2021-02-16 10:26:15 +0100 | [diff] [blame] | 632 | ret = lyxml_ns_add(xmlctx, prefix ? name : NULL, prefix ? name_len : 0, |
| 633 | dynamic ? value : strndup(value, value_len)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 634 | dynamic = 0; |
Radek IÅ¡a | 017270d | 2021-02-16 10:26:15 +0100 | [diff] [blame] | 635 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 636 | } else { |
| 637 | /* not a namespace */ |
| 638 | is_ns = 0; |
| 639 | } |
| 640 | if (dynamic) { |
| 641 | free(value); |
| 642 | } |
| 643 | |
| 644 | /* skip WS */ |
| 645 | ign_xmlws(xmlctx); |
| 646 | |
| 647 | if (is_ns) { |
| 648 | /* we can actually skip all the namespaces as there is no reason to parse them again */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 649 | prev_input = xmlctx->in->current; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 650 | } |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 651 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 652 | |
| 653 | cleanup: |
| 654 | if (!ret) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 655 | xmlctx->in->current = prev_input; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 656 | } |
| 657 | return ret; |
| 658 | } |
| 659 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 660 | /** |
| 661 | * @brief Move parser to the attribute content and parse it. |
| 662 | * |
| 663 | * @param[in] xmlctx XML context to use. |
| 664 | * @param[out] value Parsed attribute value. |
| 665 | * @param[out] value_len Length of @p value. |
| 666 | * @param[out] ws_only Whether the value is empty/white-spaces only. |
| 667 | * @param[out] dynamic Whether the value was dynamically allocated. |
| 668 | * @return LY_ERR value. |
| 669 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 670 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 671 | lyxml_next_attr_content(struct lyxml_ctx *xmlctx, const char **value, size_t *value_len, ly_bool *ws_only, ly_bool *dynamic) |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 672 | { |
| 673 | char quot; |
| 674 | |
| 675 | /* skip WS */ |
| 676 | ign_xmlws(xmlctx); |
| 677 | |
| 678 | /* skip '=' */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 679 | if (xmlctx->in->current[0] == '\0') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 680 | LOGVAL(xmlctx->ctx, LY_VCODE_EOF); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 681 | return LY_EVALID; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 682 | } else if (xmlctx->in->current[0] != '=') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 683 | LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 684 | xmlctx->in->current, "'='"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 685 | return LY_EVALID; |
| 686 | } |
| 687 | move_input(xmlctx, 1); |
| 688 | |
| 689 | /* skip WS */ |
| 690 | ign_xmlws(xmlctx); |
| 691 | |
| 692 | /* find quotes */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 693 | if (xmlctx->in->current[0] == '\0') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 694 | LOGVAL(xmlctx->ctx, LY_VCODE_EOF); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 695 | return LY_EVALID; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 696 | } else if ((xmlctx->in->current[0] != '\'') && (xmlctx->in->current[0] != '\"')) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 697 | LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 698 | xmlctx->in->current, "either single or double quotation mark"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 699 | return LY_EVALID; |
| 700 | } |
| 701 | |
| 702 | /* remember quote */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 703 | quot = xmlctx->in->current[0]; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 704 | move_input(xmlctx, 1); |
| 705 | |
| 706 | /* parse attribute value */ |
| 707 | LY_CHECK_RET(lyxml_parse_value(xmlctx, quot, (char **)value, value_len, ws_only, dynamic)); |
| 708 | |
| 709 | /* move after ending quote (without checking for EOF) */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 710 | ly_in_skip(xmlctx->in, 1); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 711 | |
| 712 | return LY_SUCCESS; |
| 713 | } |
| 714 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 715 | /** |
| 716 | * @brief Move parser to the next attribute and parse it. |
| 717 | * |
| 718 | * @param[in] xmlctx XML context to use. |
| 719 | * @param[out] prefix Parsed attribute prefix. |
| 720 | * @param[out] prefix_len Length of @p prefix. |
| 721 | * @param[out] name Parsed attribute name. |
| 722 | * @param[out] name_len Length of @p name. |
| 723 | * @return LY_ERR value. |
| 724 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 725 | static LY_ERR |
| 726 | lyxml_next_attribute(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 727 | { |
| 728 | const char *in; |
| 729 | char *value; |
| 730 | uint32_t c; |
| 731 | size_t parsed, value_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 732 | ly_bool ws_only, dynamic; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 733 | |
| 734 | /* skip WS */ |
| 735 | ign_xmlws(xmlctx); |
| 736 | |
| 737 | /* parse only possible attributes */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 738 | while ((xmlctx->in->current[0] != '>') && (xmlctx->in->current[0] != '/')) { |
| 739 | in = xmlctx->in->current; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 740 | if (in[0] == '\0') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 741 | LOGVAL(xmlctx->ctx, LY_VCODE_EOF); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 742 | return LY_EVALID; |
| 743 | } else if ((ly_getutf8(&in, &c, &parsed) || !is_xmlqnamestartchar(c))) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 744 | LOGVAL(xmlctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in - parsed), in - parsed, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 745 | "element tag end ('>' or '/>') or an attribute"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 746 | return LY_EVALID; |
| 747 | } |
| 748 | |
| 749 | /* parse attribute name */ |
| 750 | LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len)); |
| 751 | |
| 752 | if ((!*prefix || ly_strncmp("xmlns", *prefix, *prefix_len)) && (*prefix || ly_strncmp("xmlns", *name, *name_len))) { |
| 753 | /* standard attribute */ |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | /* namespace, skip it */ |
| 758 | LY_CHECK_RET(lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic)); |
| 759 | if (dynamic) { |
| 760 | free(value); |
| 761 | } |
| 762 | |
| 763 | /* skip WS */ |
| 764 | ign_xmlws(xmlctx); |
| 765 | } |
| 766 | |
| 767 | return LY_SUCCESS; |
| 768 | } |
| 769 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 770 | /** |
| 771 | * @brief Move parser to the next element and parse it. |
| 772 | * |
| 773 | * @param[in] xmlctx XML context to use. |
| 774 | * @param[out] prefix Parsed element prefix. |
| 775 | * @param[out] prefix_len Length of @p prefix. |
| 776 | * @param[out] name Parse element name. |
| 777 | * @param[out] name_len Length of @p name. |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 778 | * @param[out] closing Flag if the element is closing (includes '/'). |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 779 | * @return LY_ERR value. |
| 780 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 781 | static LY_ERR |
| 782 | lyxml_next_element(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 783 | ly_bool *closing) |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 784 | { |
| 785 | /* skip WS until EOF or after opening tag '<' */ |
| 786 | LY_CHECK_RET(lyxml_skip_until_end_or_after_otag(xmlctx)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 787 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 788 | /* set return values */ |
| 789 | *prefix = *name = NULL; |
| 790 | *prefix_len = *name_len = 0; |
| 791 | return LY_SUCCESS; |
| 792 | } |
| 793 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 794 | if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 795 | move_input(xmlctx, 1); |
| 796 | *closing = 1; |
| 797 | } else { |
| 798 | *closing = 0; |
| 799 | } |
| 800 | |
| 801 | /* skip WS */ |
| 802 | ign_xmlws(xmlctx); |
| 803 | |
| 804 | /* parse element name */ |
| 805 | LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len)); |
| 806 | |
| 807 | return LY_SUCCESS; |
| 808 | } |
| 809 | |
| 810 | LY_ERR |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 811 | lyxml_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyxml_ctx **xmlctx_p) |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 812 | { |
| 813 | LY_ERR ret = LY_SUCCESS; |
| 814 | struct lyxml_ctx *xmlctx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 815 | ly_bool closing; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 816 | |
| 817 | /* new context */ |
| 818 | xmlctx = calloc(1, sizeof *xmlctx); |
| 819 | LY_CHECK_ERR_RET(!xmlctx, LOGMEM(ctx), LY_EMEM); |
| 820 | xmlctx->ctx = ctx; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 821 | xmlctx->in = in; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 822 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 823 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 824 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 825 | /* parse next element, if any */ |
| 826 | LY_CHECK_GOTO(ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 827 | &xmlctx->name_len, &closing), cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 828 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 829 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 830 | /* update status */ |
| 831 | xmlctx->status = LYXML_END; |
| 832 | } else if (closing) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 833 | LOGVAL(ctx, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").", (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 834 | ret = LY_EVALID; |
| 835 | goto cleanup; |
| 836 | } else { |
| 837 | /* open an element, also parses all enclosed namespaces */ |
| 838 | LY_CHECK_GOTO(ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len), cleanup); |
| 839 | |
| 840 | /* update status */ |
| 841 | xmlctx->status = LYXML_ELEMENT; |
| 842 | } |
| 843 | |
| 844 | cleanup: |
| 845 | if (ret) { |
| 846 | lyxml_ctx_free(xmlctx); |
| 847 | } else { |
| 848 | *xmlctx_p = xmlctx; |
| 849 | } |
| 850 | return ret; |
| 851 | } |
| 852 | |
| 853 | LY_ERR |
| 854 | lyxml_ctx_next(struct lyxml_ctx *xmlctx) |
| 855 | { |
| 856 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 857 | ly_bool closing; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 858 | struct lyxml_elem *e; |
| 859 | |
| 860 | /* if the value was not used, free it */ |
| 861 | if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) { |
| 862 | free((char *)xmlctx->value); |
| 863 | xmlctx->value = NULL; |
| 864 | xmlctx->dynamic = 0; |
| 865 | } |
| 866 | |
| 867 | switch (xmlctx->status) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 868 | case LYXML_ELEM_CONTENT: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 869 | /* content |</elem> */ |
| 870 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 871 | /* handle special case when empty content for "<elem/>" was returned */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 872 | if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 873 | assert(xmlctx->elements.count); |
| 874 | e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1]; |
| 875 | |
| 876 | /* close the element (parses closing tag) */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 877 | ret = lyxml_close_element(xmlctx, e->prefix, e->prefix_len, e->name, e->name_len, 1); |
| 878 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 879 | |
| 880 | /* update status */ |
| 881 | xmlctx->status = LYXML_ELEM_CLOSE; |
| 882 | break; |
| 883 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 884 | /* fall through */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 885 | case LYXML_ELEM_CLOSE: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 886 | /* </elem>| <elem2>* */ |
| 887 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 888 | /* parse next element, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 889 | ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len, &closing); |
| 890 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 891 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 892 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 893 | /* update status */ |
| 894 | xmlctx->status = LYXML_END; |
| 895 | } else if (closing) { |
| 896 | /* close an element (parses also closing tag) */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 897 | ret = lyxml_close_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len, 0); |
| 898 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 899 | |
| 900 | /* update status */ |
| 901 | xmlctx->status = LYXML_ELEM_CLOSE; |
| 902 | } else { |
| 903 | /* open an element, also parses all enclosed namespaces */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 904 | ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len); |
| 905 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 906 | |
| 907 | /* update status */ |
| 908 | xmlctx->status = LYXML_ELEMENT; |
| 909 | } |
| 910 | break; |
| 911 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 912 | case LYXML_ELEMENT: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 913 | /* <elem| attr='val'* > content */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 914 | case LYXML_ATTR_CONTENT: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 915 | /* attr='val'| attr='val'* > content */ |
| 916 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 917 | /* parse attribute name, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 918 | ret = lyxml_next_attribute(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len); |
| 919 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 920 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 921 | if (xmlctx->in->current[0] == '>') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 922 | /* no attributes but a closing tag */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 923 | ly_in_skip(xmlctx->in, 1); |
| 924 | if (!xmlctx->in->current[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 925 | LOGVAL(xmlctx->ctx, LY_VCODE_EOF); |
Michal Vasko | f55ae20 | 2020-06-30 15:49:36 +0200 | [diff] [blame] | 926 | ret = LY_EVALID; |
| 927 | goto cleanup; |
| 928 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 929 | |
| 930 | /* parse element content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 931 | ret = lyxml_parse_value(xmlctx, '<', (char **)&xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 932 | &xmlctx->dynamic); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 933 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 934 | |
| 935 | if (!xmlctx->value_len) { |
Radek IÅ¡a | 017270d | 2021-02-16 10:26:15 +0100 | [diff] [blame] | 936 | /* empty value should by alocated staticaly, but check for in any case */ |
| 937 | if (xmlctx->dynamic) { |
| 938 | free((char *) xmlctx->value); |
| 939 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 940 | /* use empty value, easier to work with */ |
| 941 | xmlctx->value = ""; |
Radek IÅ¡a | 017270d | 2021-02-16 10:26:15 +0100 | [diff] [blame] | 942 | xmlctx->dynamic = 0; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /* update status */ |
| 946 | xmlctx->status = LYXML_ELEM_CONTENT; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 947 | } else if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 948 | /* no content but we still return it */ |
| 949 | xmlctx->value = ""; |
| 950 | xmlctx->value_len = 0; |
| 951 | xmlctx->ws_only = 1; |
| 952 | xmlctx->dynamic = 0; |
| 953 | |
| 954 | /* update status */ |
| 955 | xmlctx->status = LYXML_ELEM_CONTENT; |
| 956 | } else { |
| 957 | /* update status */ |
| 958 | xmlctx->status = LYXML_ATTRIBUTE; |
| 959 | } |
| 960 | break; |
| 961 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 962 | case LYXML_ATTRIBUTE: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 963 | /* attr|='val' */ |
| 964 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 965 | /* skip formatting and parse value */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 966 | ret = lyxml_next_attr_content(xmlctx, &xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only, &xmlctx->dynamic); |
| 967 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 968 | |
| 969 | /* update status */ |
| 970 | xmlctx->status = LYXML_ATTR_CONTENT; |
| 971 | break; |
| 972 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 973 | case LYXML_END: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 974 | /* </elem> |EOF */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 975 | /* nothing to do */ |
| 976 | break; |
| 977 | } |
| 978 | |
| 979 | cleanup: |
| 980 | if (ret) { |
| 981 | /* invalidate context */ |
| 982 | xmlctx->status = LYXML_END; |
| 983 | } |
| 984 | return ret; |
| 985 | } |
| 986 | |
| 987 | LY_ERR |
| 988 | lyxml_ctx_peek(struct lyxml_ctx *xmlctx, enum LYXML_PARSER_STATUS *next) |
| 989 | { |
| 990 | LY_ERR ret = LY_SUCCESS; |
| 991 | const char *prefix, *name, *prev_input; |
| 992 | size_t prefix_len, name_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 993 | ly_bool closing; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 994 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 995 | prev_input = xmlctx->in->current; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 996 | |
| 997 | switch (xmlctx->status) { |
| 998 | case LYXML_ELEM_CONTENT: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 999 | if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1000 | *next = LYXML_ELEM_CLOSE; |
| 1001 | break; |
| 1002 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1003 | /* fall through */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1004 | case LYXML_ELEM_CLOSE: |
| 1005 | /* parse next element, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1006 | ret = lyxml_next_element(xmlctx, &prefix, &prefix_len, &name, &name_len, &closing); |
| 1007 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1008 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1009 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1010 | *next = LYXML_END; |
| 1011 | } else if (closing) { |
| 1012 | *next = LYXML_ELEM_CLOSE; |
| 1013 | } else { |
| 1014 | *next = LYXML_ELEMENT; |
| 1015 | } |
| 1016 | break; |
| 1017 | case LYXML_ELEMENT: |
| 1018 | case LYXML_ATTR_CONTENT: |
| 1019 | /* parse attribute name, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1020 | ret = lyxml_next_attribute(xmlctx, &prefix, &prefix_len, &name, &name_len); |
| 1021 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1022 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1023 | if ((xmlctx->in->current[0] == '>') || (xmlctx->in->current[0] == '/')) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1024 | *next = LYXML_ELEM_CONTENT; |
| 1025 | } else { |
| 1026 | *next = LYXML_ATTRIBUTE; |
| 1027 | } |
| 1028 | break; |
| 1029 | case LYXML_ATTRIBUTE: |
| 1030 | *next = LYXML_ATTR_CONTENT; |
| 1031 | break; |
| 1032 | case LYXML_END: |
| 1033 | *next = LYXML_END; |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | cleanup: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1038 | xmlctx->in->current = prev_input; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1039 | return ret; |
| 1040 | } |
| 1041 | |
| 1042 | void |
| 1043 | lyxml_ctx_free(struct lyxml_ctx *xmlctx) |
| 1044 | { |
| 1045 | uint32_t u; |
| 1046 | |
| 1047 | if (!xmlctx) { |
| 1048 | return; |
| 1049 | } |
| 1050 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1051 | LOG_LOCBACK(0, 0, 0, 1); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1052 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1053 | if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) { |
| 1054 | free((char *)xmlctx->value); |
| 1055 | } |
| 1056 | ly_set_erase(&xmlctx->elements, free); |
| 1057 | for (u = xmlctx->ns.count - 1; u + 1 > 0; --u) { |
| 1058 | /* remove the ns structure */ |
| 1059 | free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix); |
| 1060 | free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri); |
| 1061 | free(xmlctx->ns.objs[u]); |
| 1062 | } |
| 1063 | ly_set_erase(&xmlctx->ns, NULL); |
| 1064 | free(xmlctx); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 1065 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1066 | |
| 1067 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1068 | lyxml_dump_text(struct ly_out *out, const char *text, ly_bool attribute) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1069 | { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1070 | LY_ERR ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1071 | |
| 1072 | if (!text) { |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1076 | for (uint64_t u = 0; text[u]; u++) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1077 | switch (text[u]) { |
| 1078 | case '&': |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1079 | ret = ly_print_(out, "&"); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1080 | break; |
| 1081 | case '<': |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1082 | ret = ly_print_(out, "<"); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1083 | break; |
| 1084 | case '>': |
| 1085 | /* not needed, just for readability */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1086 | ret = ly_print_(out, ">"); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1087 | break; |
| 1088 | case '"': |
| 1089 | if (attribute) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1090 | ret = ly_print_(out, """); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1091 | break; |
| 1092 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1093 | /* fall through */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1094 | default: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1095 | ret = ly_write_(out, &text[u], 1); |
| 1096 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1097 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1098 | LY_CHECK_RET(ret); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1101 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1102 | } |
| 1103 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1104 | LY_ERR |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1105 | lyxml_value_compare(const struct ly_ctx *ctx1, const char *value1, void *val_prefix_data1, |
| 1106 | const struct ly_ctx *ctx2, const char *value2, void *val_prefix_data2) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1107 | { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1108 | const char *value1_iter, *value2_iter; |
| 1109 | const char *value1_next, *value2_next; |
| 1110 | uint32_t value1_len, value2_len; |
| 1111 | ly_bool is_prefix1, is_prefix2; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1112 | const struct lys_module *mod1, *mod2; |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1113 | LY_ERR ret; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1114 | |
| 1115 | if (!value1 && !value2) { |
| 1116 | return LY_SUCCESS; |
| 1117 | } |
| 1118 | if ((value1 && !value2) || (!value1 && value2)) { |
| 1119 | return LY_ENOT; |
| 1120 | } |
| 1121 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1122 | if (!ctx2) { |
| 1123 | ctx2 = ctx1; |
| 1124 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1125 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1126 | ret = LY_SUCCESS; |
| 1127 | for (value1_iter = value1, value2_iter = value2; |
| 1128 | value1_iter && value2_iter; |
| 1129 | value1_iter = value1_next, value2_iter = value2_next) { |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 1130 | if ((ret = ly_value_prefix_next(value1_iter, NULL, &value1_len, &is_prefix1, &value1_next))) { |
| 1131 | break; |
| 1132 | } |
| 1133 | if ((ret = ly_value_prefix_next(value2_iter, NULL, &value2_len, &is_prefix2, &value2_next))) { |
| 1134 | break; |
| 1135 | } |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1136 | |
| 1137 | if (is_prefix1 != is_prefix2) { |
| 1138 | ret = LY_ENOT; |
| 1139 | break; |
| 1140 | } |
| 1141 | |
| 1142 | if (!is_prefix1) { |
| 1143 | if (value1_len != value2_len) { |
| 1144 | ret = LY_ENOT; |
| 1145 | break; |
| 1146 | } |
| 1147 | if (strncmp(value1_iter, value2_iter, value1_len)) { |
| 1148 | ret = LY_ENOT; |
| 1149 | break; |
| 1150 | } |
| 1151 | continue; |
| 1152 | } |
| 1153 | |
| 1154 | mod1 = mod2 = NULL; |
| 1155 | if (val_prefix_data1) { |
| 1156 | /* find module of the first prefix, if any */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1157 | mod1 = ly_resolve_prefix(ctx1, value1_iter, value1_len, LY_VALUE_XML, val_prefix_data1); |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1158 | } |
| 1159 | if (val_prefix_data2) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1160 | mod2 = ly_resolve_prefix(ctx2, value2_iter, value2_len, LY_VALUE_XML, val_prefix_data2); |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1161 | } |
| 1162 | if (!mod1 || !mod2) { |
| 1163 | /* not a prefix or maps to different namespaces */ |
| 1164 | ret = LY_ENOT; |
| 1165 | break; |
| 1166 | } |
| 1167 | |
| 1168 | if (mod1->ctx == mod2->ctx) { |
| 1169 | /* same contexts */ |
| 1170 | if ((mod1->name != mod2->name) || (mod1->revision != mod2->revision)) { |
| 1171 | ret = LY_ENOT; |
| 1172 | break; |
| 1173 | } |
| 1174 | } else { |
| 1175 | /* different contexts */ |
| 1176 | if (strcmp(mod1->name, mod2->name)) { |
| 1177 | ret = LY_ENOT; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1178 | break; |
| 1179 | } |
| 1180 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1181 | if (mod1->revision || mod2->revision) { |
| 1182 | if (!mod1->revision || !mod2->revision) { |
| 1183 | ret = LY_ENOT; |
| 1184 | break; |
| 1185 | } |
| 1186 | if (strcmp(mod1->revision, mod2->revision)) { |
| 1187 | ret = LY_ENOT; |
| 1188 | break; |
| 1189 | } |
| 1190 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1191 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1192 | } |
| 1193 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1194 | if (value1_iter || value2_iter) { |
| 1195 | ret = LY_ENOT; |
| 1196 | } |
| 1197 | |
| 1198 | return ret; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1199 | } |