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 | * |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 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 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 184 | void |
| 185 | lyxml_ns_rm(struct lyxml_ctx *xmlctx) |
| 186 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 187 | for (uint32_t u = xmlctx->ns.count - 1; u + 1 > 0; --u) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 188 | if (((struct lyxml_ns *)xmlctx->ns.objs[u])->depth != xmlctx->elements.count + 1) { |
| 189 | /* we are done, the namespaces from a single element are supposed to be together */ |
| 190 | break; |
| 191 | } |
| 192 | /* remove the ns structure */ |
| 193 | free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix); |
| 194 | free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri); |
| 195 | free(xmlctx->ns.objs[u]); |
| 196 | --xmlctx->ns.count; |
| 197 | } |
| 198 | |
| 199 | if (!xmlctx->ns.count) { |
| 200 | /* cleanup the xmlctx's namespaces storage */ |
| 201 | ly_set_erase(&xmlctx->ns, NULL); |
| 202 | } |
| 203 | } |
| 204 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 205 | const struct lyxml_ns * |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 206 | 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] | 207 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 208 | struct lyxml_ns *ns; |
| 209 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 210 | for (uint32_t u = ns_set->count - 1; u + 1 > 0; --u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 211 | ns = (struct lyxml_ns *)ns_set->objs[u]; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 212 | if (prefix && prefix_len) { |
| 213 | if (ns->prefix && !ly_strncmp(ns->prefix, prefix, prefix_len)) { |
| 214 | return ns; |
| 215 | } |
| 216 | } else if (!ns->prefix) { |
| 217 | /* default namespace */ |
| 218 | return ns; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return NULL; |
| 223 | } |
| 224 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 225 | /** |
| 226 | * @brief Skip in the input until EOF or just after the opening tag. |
| 227 | * Handles special XML constructs (comment, cdata, doctype). |
| 228 | * |
| 229 | * @param[in] xmlctx XML context to use. |
| 230 | * @return LY_ERR value. |
| 231 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 232 | static LY_ERR |
| 233 | lyxml_skip_until_end_or_after_otag(struct lyxml_ctx *xmlctx) |
| 234 | { |
| 235 | const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 236 | const char *endtag, *sectname; |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 237 | size_t endtag_len; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 238 | |
| 239 | while (1) { |
| 240 | ign_xmlws(xmlctx); |
| 241 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 242 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 243 | /* EOF */ |
| 244 | if (xmlctx->elements.count) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 245 | LOGVAL(ctx, LY_VCODE_EOF); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 246 | return LY_EVALID; |
| 247 | } |
| 248 | return LY_SUCCESS; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 249 | } else if (xmlctx->in->current[0] != '<') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 250 | LOGVAL(ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 251 | xmlctx->in->current, "element tag start ('<')"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 252 | return LY_EVALID; |
| 253 | } |
| 254 | move_input(xmlctx, 1); |
| 255 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 256 | if (xmlctx->in->current[0] == '!') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 257 | move_input(xmlctx, 1); |
| 258 | /* sections to ignore */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 259 | if (!strncmp(xmlctx->in->current, "--", 2)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 260 | /* comment */ |
| 261 | move_input(xmlctx, 2); |
| 262 | sectname = "Comment"; |
| 263 | endtag = "-->"; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 264 | endtag_len = ly_strlen_const("-->"); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 265 | } else if (!strncmp(xmlctx->in->current, "DOCTYPE", ly_strlen_const("DOCTYPE"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 266 | /* Document type declaration - not supported */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 267 | LOGVAL(ctx, LY_VCODE_NSUPP, "Document Type Declaration"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 268 | return LY_EVALID; |
| 269 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 270 | LOGVAL(ctx, LYVE_SYNTAX, "Unknown XML section \"%.20s\".", &xmlctx->in->current[-2]); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 271 | return LY_EVALID; |
| 272 | } |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 273 | LY_CHECK_RET(skip_section(xmlctx, endtag, endtag_len, sectname)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 274 | } else if (xmlctx->in->current[0] == '?') { |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 275 | LY_CHECK_RET(skip_section(xmlctx, "?>", 2, "Declaration")); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 276 | } else { |
| 277 | /* other non-WS character */ |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return LY_SUCCESS; |
| 283 | } |
| 284 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 285 | /** |
| 286 | * @brief Parse QName. |
| 287 | * |
| 288 | * @param[in] xmlctx XML context to use. |
| 289 | * @param[out] prefix Parsed prefix, may be NULL. |
| 290 | * @param[out] prefix_len Length of @p prefix. |
| 291 | * @param[out] name Parsed name. |
| 292 | * @param[out] name_len Length of @p name. |
| 293 | * @return LY_ERR value. |
| 294 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 295 | static LY_ERR |
| 296 | lyxml_parse_qname(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 297 | { |
| 298 | const char *start, *end; |
| 299 | |
| 300 | *prefix = NULL; |
| 301 | *prefix_len = 0; |
| 302 | |
| 303 | LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end)); |
| 304 | if (end[0] == ':') { |
| 305 | /* we have prefixed identifier */ |
| 306 | *prefix = start; |
| 307 | *prefix_len = end - start; |
| 308 | |
| 309 | move_input(xmlctx, 1); |
| 310 | LY_CHECK_RET(lyxml_parse_identifier(xmlctx, &start, &end)); |
| 311 | } |
| 312 | |
| 313 | *name = start; |
| 314 | *name_len = end - start; |
| 315 | return LY_SUCCESS; |
| 316 | } |
| 317 | |
| 318 | /** |
Michal Vasko | 8cf6f72 | 2022-02-18 13:08:23 +0100 | [diff] [blame^] | 319 | * @brief Prepare buffer for new data. |
| 320 | * |
| 321 | * @param[in] ctx Context for logging. |
| 322 | * @param[in,out] in XML input data. |
| 323 | * @param[in,out] offset Current offset in @p in. |
| 324 | * @param[in] need_space Needed additional free space that is allocated. |
| 325 | * @param[in,out] buf Dynamic buffer. |
| 326 | * @param[in,out] len Current @p buf length (used characters). |
| 327 | * @param[in,out] size Current @p buf size (allocated characters). |
| 328 | * @return LY_ERR value. |
| 329 | */ |
| 330 | static LY_ERR |
| 331 | lyxml_parse_value_use_buf(const struct ly_ctx *ctx, const char **in, size_t *offset, size_t need_space, char **buf, |
| 332 | size_t *len, size_t *size) |
| 333 | { |
| 334 | #define BUFSIZE 24 |
| 335 | #define BUFSIZE_STEP 128 |
| 336 | |
| 337 | if (!*buf) { |
| 338 | /* prepare output buffer */ |
| 339 | *buf = malloc(BUFSIZE); |
| 340 | LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM); |
| 341 | *size = BUFSIZE; |
| 342 | } |
| 343 | |
| 344 | /* allocate needed space */ |
| 345 | while (*len + *offset + need_space >= *size) { |
| 346 | *buf = ly_realloc(*buf, *size + BUFSIZE_STEP); |
| 347 | LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM); |
| 348 | *size += BUFSIZE_STEP; |
| 349 | } |
| 350 | |
| 351 | if (*offset) { |
| 352 | /* store what we have so far */ |
| 353 | memcpy(&(*buf)[*len], *in, *offset); |
| 354 | *len += *offset; |
| 355 | *in += *offset; |
| 356 | *offset = 0; |
| 357 | } |
| 358 | |
| 359 | return LY_SUCCESS; |
| 360 | |
| 361 | #undef BUFSIZE |
| 362 | #undef BUFSIZE_STEP |
| 363 | } |
| 364 | |
| 365 | /** |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 366 | * @brief Parse XML text content (value). |
| 367 | * |
| 368 | * @param[in] xmlctx XML context to use. |
| 369 | * @param[in] endchar Expected character to mark value end. |
| 370 | * @param[out] value Parsed value. |
| 371 | * @param[out] length Length of @p value. |
| 372 | * @param[out] ws_only Whether the value is empty/white-spaces only. |
| 373 | * @param[out] dynamic Whether the value was dynamically allocated. |
| 374 | * @return LY_ERR value. |
| 375 | */ |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 376 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 377 | 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] | 378 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 379 | const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */ |
aPiecek | b287b21 | 2021-05-04 14:24:25 +0200 | [diff] [blame] | 380 | const char *in = xmlctx->in->current, *start, *in_aux; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 381 | char *buf = NULL; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 382 | size_t offset; /* read offset in input buffer */ |
| 383 | size_t len; /* length of the output string (write offset in output buffer) */ |
| 384 | size_t size = 0; /* size of the output buffer */ |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 385 | void *p; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 386 | uint32_t n; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 387 | size_t u; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 388 | ly_bool ws = 1; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 389 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 390 | assert(xmlctx); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 391 | |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 392 | /* init */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 393 | start = in; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 394 | offset = len = 0; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 395 | |
| 396 | /* parse */ |
| 397 | while (in[offset]) { |
| 398 | if (in[offset] == '&') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 399 | /* non WS */ |
| 400 | ws = 0; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 401 | |
Michal Vasko | 8cf6f72 | 2022-02-18 13:08:23 +0100 | [diff] [blame^] | 402 | /* use buffer and allocate enough for the offset and next character, |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 403 | * we will need 4 bytes at most since we support only the predefined |
| 404 | * (one-char) entities and character references */ |
Michal Vasko | 8cf6f72 | 2022-02-18 13:08:23 +0100 | [diff] [blame^] | 405 | LY_CHECK_RET(lyxml_parse_value_use_buf(ctx, &in, &offset, 4, &buf, &len, &size)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 406 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 407 | ++offset; |
| 408 | if (in[offset] != '#') { |
| 409 | /* entity reference - only predefined references are supported */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 410 | if (!strncmp(&in[offset], "lt;", ly_strlen_const("lt;"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 411 | buf[len++] = '<'; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 412 | in += ly_strlen_const("<"); |
| 413 | } else if (!strncmp(&in[offset], "gt;", ly_strlen_const("gt;"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 414 | buf[len++] = '>'; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 415 | in += ly_strlen_const(">"); |
| 416 | } else if (!strncmp(&in[offset], "amp;", ly_strlen_const("amp;"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 417 | buf[len++] = '&'; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 418 | in += ly_strlen_const("&"); |
| 419 | } else if (!strncmp(&in[offset], "apos;", ly_strlen_const("apos;"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 420 | buf[len++] = '\''; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 421 | in += ly_strlen_const("'"); |
| 422 | } else if (!strncmp(&in[offset], "quot;", ly_strlen_const("quot;"))) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 423 | buf[len++] = '\"'; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 424 | in += ly_strlen_const("""); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 425 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 426 | LOGVAL(ctx, LYVE_SYNTAX, "Entity reference \"%.*s\" not supported, only predefined references allowed.", |
| 427 | 10, &in[offset - 1]); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 428 | goto error; |
| 429 | } |
| 430 | offset = 0; |
| 431 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 432 | p = (void *)&in[offset - 1]; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 433 | /* character reference */ |
| 434 | ++offset; |
| 435 | if (isdigit(in[offset])) { |
| 436 | for (n = 0; isdigit(in[offset]); offset++) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 437 | n = (LY_BASE_DEC * n) + (in[offset] - '0'); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 438 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 439 | } else if ((in[offset] == 'x') && isxdigit(in[offset + 1])) { |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 440 | for (n = 0, ++offset; isxdigit(in[offset]); offset++) { |
| 441 | if (isdigit(in[offset])) { |
| 442 | u = (in[offset] - '0'); |
| 443 | } else if (in[offset] > 'F') { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 444 | u = LY_BASE_DEC + (in[offset] - 'a'); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 445 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 446 | u = LY_BASE_DEC + (in[offset] - 'A'); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 447 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 448 | n = (LY_BASE_HEX * n) + u; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 449 | } |
| 450 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 451 | LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 452 | goto error; |
| 453 | |
| 454 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 455 | |
Michal Vasko | 8cf6f72 | 2022-02-18 13:08:23 +0100 | [diff] [blame^] | 456 | if (in[offset] != ';') { |
| 457 | LOGVAL(ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"); |
| 458 | goto error; |
| 459 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 460 | ++offset; |
Michal Vasko | 8cf6f72 | 2022-02-18 13:08:23 +0100 | [diff] [blame^] | 461 | if (ly_pututf8(&buf[len], n, &u)) { |
| 462 | LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n); |
| 463 | goto error; |
| 464 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 465 | len += u; |
| 466 | in += offset; |
| 467 | offset = 0; |
| 468 | } |
Michal Vasko | 8cf6f72 | 2022-02-18 13:08:23 +0100 | [diff] [blame^] | 469 | } else if (!strncmp(in + offset, "<![CDATA[", ly_strlen_const("<![CDATA["))) { |
| 470 | /* CDATA, find the end */ |
| 471 | in_aux = strstr(in + offset + ly_strlen_const("<![CDATA["), "]]>"); |
| 472 | if (!in_aux) { |
| 473 | LOGVAL(xmlctx->ctx, LY_VCODE_NTERM, "CDATA"); |
| 474 | goto error; |
| 475 | } |
| 476 | u = in_aux - (in + offset + ly_strlen_const("<![CDATA[")); |
| 477 | |
| 478 | /* use buffer, allocate enough for the whole CDATA */ |
| 479 | LY_CHECK_RET(lyxml_parse_value_use_buf(ctx, &in, &offset, u, &buf, &len, &size)); |
| 480 | |
| 481 | /* skip CDATA tag */ |
| 482 | in += ly_strlen_const("<![CDATA["); |
| 483 | assert(!offset); |
| 484 | |
| 485 | /* analyze CDATA for non WS and newline chars */ |
| 486 | for (n = 0; n < u; ++n) { |
| 487 | if (in[n] == '\n') { |
| 488 | LY_IN_NEW_LINE(xmlctx->in); |
| 489 | } else if (!is_xmlws(in[n])) { |
| 490 | ws = 0; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* copy CDATA */ |
| 495 | memcpy(buf + len, in, u); |
| 496 | len += u; |
| 497 | |
| 498 | /* move input skipping the end tag */ |
| 499 | in += u + ly_strlen_const("]]>"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 500 | } else if (in[offset] == endchar) { |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 501 | /* end of string */ |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 502 | if (buf) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 503 | /* realloc exact size string */ |
| 504 | buf = ly_realloc(buf, len + offset + 1); |
| 505 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM); |
| 506 | size = len + offset + 1; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 507 | if (offset) { |
| 508 | memcpy(&buf[len], in, offset); |
| 509 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 510 | |
| 511 | /* set terminating NULL byte */ |
| 512 | buf[len + offset] = '\0'; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 513 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 514 | len += offset; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 515 | in += offset; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 516 | goto success; |
| 517 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 518 | if (!is_xmlws(in[offset])) { |
| 519 | /* non WS */ |
| 520 | ws = 0; |
| 521 | } |
| 522 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 523 | /* log lines */ |
| 524 | if (in[offset] == '\n') { |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 525 | LY_IN_NEW_LINE(xmlctx->in); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* continue */ |
aPiecek | b287b21 | 2021-05-04 14:24:25 +0200 | [diff] [blame] | 529 | in_aux = &in[offset]; |
| 530 | LY_CHECK_ERR_GOTO(ly_getutf8(&in_aux, &n, &u), |
| 531 | LOGVAL(ctx, LY_VCODE_INCHAR, in[offset]), error); |
| 532 | offset += u; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 533 | } |
| 534 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 535 | |
| 536 | /* EOF reached before endchar */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 537 | LOGVAL(ctx, LY_VCODE_EOF); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 538 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 539 | error: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 540 | free(buf); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 541 | return LY_EVALID; |
| 542 | |
| 543 | success: |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 544 | if (buf) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 545 | *value = buf; |
| 546 | *dynamic = 1; |
| 547 | } else { |
| 548 | *value = (char *)start; |
| 549 | *dynamic = 0; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 550 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 551 | *length = len; |
| 552 | *ws_only = ws; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 553 | |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 554 | xmlctx->in->current = in; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 555 | return LY_SUCCESS; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 556 | } |
| 557 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 558 | /** |
| 559 | * @brief Parse XML closing element and match it to a stored starting element. |
| 560 | * |
| 561 | * @param[in] xmlctx XML context to use. |
| 562 | * @param[in] prefix Expected closing element prefix. |
| 563 | * @param[in] prefix_len Length of @p prefix. |
| 564 | * @param[in] name Expected closing element name. |
| 565 | * @param[in] name_len Length of @p name. |
| 566 | * @param[in] empty Whether we are parsing a special "empty" element (with joined starting and closing tag) with no value. |
| 567 | * @return LY_ERR value. |
| 568 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 569 | static LY_ERR |
| 570 | 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] | 571 | ly_bool empty) |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 572 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 573 | struct lyxml_elem *e; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 574 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 575 | /* match opening and closing element tags */ |
| 576 | if (!xmlctx->elements.count) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 577 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 578 | (int)name_len, name); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 579 | return LY_EVALID; |
| 580 | } |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 581 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 582 | e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 583 | if ((e->prefix_len != prefix_len) || (e->name_len != name_len) || |
| 584 | (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] | 585 | 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] | 586 | (int)e->prefix_len, e->prefix ? e->prefix : "", e->prefix ? ":" : "", (int)e->name_len, e->name, |
| 587 | (int)prefix_len, prefix ? prefix : "", prefix ? ":" : "", (int)name_len, name); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 588 | return LY_EVALID; |
| 589 | } |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 590 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 591 | /* opening and closing element tags matches, remove record from the opening tags list */ |
| 592 | ly_set_rm_index(&xmlctx->elements, xmlctx->elements.count - 1, free); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 593 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 594 | /* remove also the namespaces connected with the element */ |
| 595 | lyxml_ns_rm(xmlctx); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 596 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 597 | /* skip WS */ |
| 598 | ign_xmlws(xmlctx); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 599 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 600 | /* special "<elem/>" element */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 601 | if (empty && (xmlctx->in->current[0] == '/')) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 602 | move_input(xmlctx, 1); |
| 603 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 604 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 605 | /* parse closing tag */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 606 | if (xmlctx->in->current[0] != '>') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 607 | 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] | 608 | xmlctx->in->current, "element tag termination ('>')"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 609 | return LY_EVALID; |
| 610 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 611 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 612 | /* move after closing tag without checking for EOF */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 613 | ly_in_skip(xmlctx->in, 1); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 614 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 615 | return LY_SUCCESS; |
| 616 | } |
| 617 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 618 | /** |
| 619 | * @brief Store parsed opening element and parse any included namespaces. |
| 620 | * |
| 621 | * @param[in] xmlctx XML context to use. |
| 622 | * @param[in] prefix Parsed starting element prefix. |
| 623 | * @param[in] prefix_len Length of @p prefix. |
| 624 | * @param[in] name Parsed starting element name. |
| 625 | * @param[in] name_len Length of @p name. |
| 626 | * @return LY_ERR value. |
| 627 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 628 | static LY_ERR |
| 629 | 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] | 630 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 631 | LY_ERR ret = LY_SUCCESS; |
| 632 | struct lyxml_elem *e; |
| 633 | const char *prev_input; |
Michal Vasko | 4fd9192 | 2021-09-15 08:51:06 +0200 | [diff] [blame] | 634 | uint64_t prev_line; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 635 | char *value; |
| 636 | size_t parsed, value_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 637 | ly_bool ws_only, dynamic, is_ns; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 638 | uint32_t c; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 639 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 640 | /* store element opening tag information */ |
| 641 | e = malloc(sizeof *e); |
| 642 | LY_CHECK_ERR_RET(!e, LOGMEM(xmlctx->ctx), LY_EMEM); |
| 643 | e->name = name; |
| 644 | e->prefix = prefix; |
| 645 | e->name_len = name_len; |
| 646 | e->prefix_len = prefix_len; |
aPiecek | 93582ed | 2021-05-25 14:49:06 +0200 | [diff] [blame] | 647 | |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 648 | LY_CHECK_RET(ly_set_add(&xmlctx->elements, e, 1, NULL)); |
aPiecek | 93582ed | 2021-05-25 14:49:06 +0200 | [diff] [blame] | 649 | if (xmlctx->elements.count > LY_MAX_BLOCK_DEPTH) { |
Michal Vasko | 4fd9192 | 2021-09-15 08:51:06 +0200 | [diff] [blame] | 650 | LOGERR(xmlctx->ctx, LY_EINVAL, "The maximum number of open elements has been exceeded."); |
aPiecek | 93582ed | 2021-05-25 14:49:06 +0200 | [diff] [blame] | 651 | ret = LY_EINVAL; |
| 652 | goto cleanup; |
| 653 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 654 | |
| 655 | /* skip WS */ |
| 656 | ign_xmlws(xmlctx); |
| 657 | |
| 658 | /* parse and store all namespaces */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 659 | prev_input = xmlctx->in->current; |
Michal Vasko | 4fd9192 | 2021-09-15 08:51:06 +0200 | [diff] [blame] | 660 | prev_line = xmlctx->in->line; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 661 | is_ns = 1; |
aPiecek | 785ad3d | 2021-05-10 15:51:13 +0200 | [diff] [blame] | 662 | while ((xmlctx->in->current[0] != '\0') && !(ret = ly_getutf8(&xmlctx->in->current, &c, &parsed))) { |
| 663 | if (!is_xmlqnamestartchar(c)) { |
| 664 | break; |
| 665 | } |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 666 | xmlctx->in->current -= parsed; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 667 | |
| 668 | /* parse attribute name */ |
| 669 | LY_CHECK_GOTO(ret = lyxml_parse_qname(xmlctx, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 670 | |
| 671 | /* parse the value */ |
| 672 | LY_CHECK_GOTO(ret = lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic), cleanup); |
| 673 | |
| 674 | /* store every namespace */ |
| 675 | 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] | 676 | ret = lyxml_ns_add(xmlctx, prefix ? name : NULL, prefix ? name_len : 0, |
| 677 | dynamic ? value : strndup(value, value_len)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 678 | dynamic = 0; |
Radek IÅ¡a | 017270d | 2021-02-16 10:26:15 +0100 | [diff] [blame] | 679 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 680 | } else { |
| 681 | /* not a namespace */ |
| 682 | is_ns = 0; |
| 683 | } |
| 684 | if (dynamic) { |
| 685 | free(value); |
| 686 | } |
| 687 | |
| 688 | /* skip WS */ |
| 689 | ign_xmlws(xmlctx); |
| 690 | |
| 691 | if (is_ns) { |
| 692 | /* 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] | 693 | prev_input = xmlctx->in->current; |
Michal Vasko | 4fd9192 | 2021-09-15 08:51:06 +0200 | [diff] [blame] | 694 | prev_line = xmlctx->in->line; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 695 | } |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 696 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 697 | |
| 698 | cleanup: |
| 699 | if (!ret) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 700 | xmlctx->in->current = prev_input; |
Michal Vasko | 4fd9192 | 2021-09-15 08:51:06 +0200 | [diff] [blame] | 701 | xmlctx->in->line = prev_line; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 702 | } |
| 703 | return ret; |
| 704 | } |
| 705 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 706 | /** |
| 707 | * @brief Move parser to the attribute content and parse it. |
| 708 | * |
| 709 | * @param[in] xmlctx XML context to use. |
| 710 | * @param[out] value Parsed attribute value. |
| 711 | * @param[out] value_len Length of @p value. |
| 712 | * @param[out] ws_only Whether the value is empty/white-spaces only. |
| 713 | * @param[out] dynamic Whether the value was dynamically allocated. |
| 714 | * @return LY_ERR value. |
| 715 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 716 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 717 | 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] | 718 | { |
| 719 | char quot; |
| 720 | |
| 721 | /* skip WS */ |
| 722 | ign_xmlws(xmlctx); |
| 723 | |
| 724 | /* skip '=' */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 725 | if (xmlctx->in->current[0] == '\0') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 726 | LOGVAL(xmlctx->ctx, LY_VCODE_EOF); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 727 | return LY_EVALID; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 728 | } else if (xmlctx->in->current[0] != '=') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 729 | 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] | 730 | xmlctx->in->current, "'='"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 731 | return LY_EVALID; |
| 732 | } |
| 733 | move_input(xmlctx, 1); |
| 734 | |
| 735 | /* skip WS */ |
| 736 | ign_xmlws(xmlctx); |
| 737 | |
| 738 | /* find quotes */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 739 | if (xmlctx->in->current[0] == '\0') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 740 | LOGVAL(xmlctx->ctx, LY_VCODE_EOF); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 741 | return LY_EVALID; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 742 | } else if ((xmlctx->in->current[0] != '\'') && (xmlctx->in->current[0] != '\"')) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 743 | 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] | 744 | xmlctx->in->current, "either single or double quotation mark"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 745 | return LY_EVALID; |
| 746 | } |
| 747 | |
| 748 | /* remember quote */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 749 | quot = xmlctx->in->current[0]; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 750 | move_input(xmlctx, 1); |
| 751 | |
| 752 | /* parse attribute value */ |
| 753 | LY_CHECK_RET(lyxml_parse_value(xmlctx, quot, (char **)value, value_len, ws_only, dynamic)); |
| 754 | |
| 755 | /* move after ending quote (without checking for EOF) */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 756 | ly_in_skip(xmlctx->in, 1); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 757 | |
| 758 | return LY_SUCCESS; |
| 759 | } |
| 760 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 761 | /** |
| 762 | * @brief Move parser to the next attribute and parse it. |
| 763 | * |
| 764 | * @param[in] xmlctx XML context to use. |
| 765 | * @param[out] prefix Parsed attribute prefix. |
| 766 | * @param[out] prefix_len Length of @p prefix. |
| 767 | * @param[out] name Parsed attribute name. |
| 768 | * @param[out] name_len Length of @p name. |
| 769 | * @return LY_ERR value. |
| 770 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 771 | static LY_ERR |
| 772 | lyxml_next_attribute(struct lyxml_ctx *xmlctx, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 773 | { |
| 774 | const char *in; |
| 775 | char *value; |
| 776 | uint32_t c; |
| 777 | size_t parsed, value_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 778 | ly_bool ws_only, dynamic; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 779 | |
| 780 | /* skip WS */ |
| 781 | ign_xmlws(xmlctx); |
| 782 | |
| 783 | /* parse only possible attributes */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 784 | while ((xmlctx->in->current[0] != '>') && (xmlctx->in->current[0] != '/')) { |
| 785 | in = xmlctx->in->current; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 786 | if (in[0] == '\0') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 787 | LOGVAL(xmlctx->ctx, LY_VCODE_EOF); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 788 | return LY_EVALID; |
| 789 | } else if ((ly_getutf8(&in, &c, &parsed) || !is_xmlqnamestartchar(c))) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 790 | 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] | 791 | "element tag end ('>' or '/>') or an attribute"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 792 | return LY_EVALID; |
| 793 | } |
| 794 | |
| 795 | /* parse attribute name */ |
| 796 | LY_CHECK_RET(lyxml_parse_qname(xmlctx, prefix, prefix_len, name, name_len)); |
| 797 | |
| 798 | if ((!*prefix || ly_strncmp("xmlns", *prefix, *prefix_len)) && (*prefix || ly_strncmp("xmlns", *name, *name_len))) { |
| 799 | /* standard attribute */ |
| 800 | break; |
| 801 | } |
| 802 | |
| 803 | /* namespace, skip it */ |
| 804 | LY_CHECK_RET(lyxml_next_attr_content(xmlctx, (const char **)&value, &value_len, &ws_only, &dynamic)); |
| 805 | if (dynamic) { |
| 806 | free(value); |
| 807 | } |
| 808 | |
| 809 | /* skip WS */ |
| 810 | ign_xmlws(xmlctx); |
| 811 | } |
| 812 | |
| 813 | return LY_SUCCESS; |
| 814 | } |
| 815 | |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 816 | /** |
| 817 | * @brief Move parser to the next element and parse it. |
| 818 | * |
| 819 | * @param[in] xmlctx XML context to use. |
| 820 | * @param[out] prefix Parsed element prefix. |
| 821 | * @param[out] prefix_len Length of @p prefix. |
| 822 | * @param[out] name Parse element name. |
| 823 | * @param[out] name_len Length of @p name. |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 824 | * @param[out] closing Flag if the element is closing (includes '/'). |
Michal Vasko | 8cef523 | 2020-06-15 17:59:47 +0200 | [diff] [blame] | 825 | * @return LY_ERR value. |
| 826 | */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 827 | static LY_ERR |
| 828 | 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] | 829 | ly_bool *closing) |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 830 | { |
| 831 | /* skip WS until EOF or after opening tag '<' */ |
| 832 | LY_CHECK_RET(lyxml_skip_until_end_or_after_otag(xmlctx)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 833 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 834 | /* set return values */ |
| 835 | *prefix = *name = NULL; |
| 836 | *prefix_len = *name_len = 0; |
| 837 | return LY_SUCCESS; |
| 838 | } |
| 839 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 840 | if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 841 | 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 | |
| 856 | LY_ERR |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 857 | 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] | 858 | { |
| 859 | LY_ERR ret = LY_SUCCESS; |
| 860 | struct lyxml_ctx *xmlctx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 861 | ly_bool closing; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 862 | |
| 863 | /* new context */ |
| 864 | xmlctx = calloc(1, sizeof *xmlctx); |
| 865 | LY_CHECK_ERR_RET(!xmlctx, LOGMEM(ctx), LY_EMEM); |
| 866 | xmlctx->ctx = ctx; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 867 | xmlctx->in = in; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 868 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 869 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 870 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 871 | /* parse next element, if any */ |
| 872 | 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] | 873 | &xmlctx->name_len, &closing), cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 874 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 875 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 876 | /* update status */ |
| 877 | xmlctx->status = LYXML_END; |
| 878 | } else if (closing) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 879 | 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] | 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 | |
| 890 | cleanup: |
| 891 | if (ret) { |
| 892 | lyxml_ctx_free(xmlctx); |
| 893 | } else { |
| 894 | *xmlctx_p = xmlctx; |
| 895 | } |
| 896 | return ret; |
| 897 | } |
| 898 | |
| 899 | LY_ERR |
| 900 | lyxml_ctx_next(struct lyxml_ctx *xmlctx) |
| 901 | { |
| 902 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 903 | ly_bool closing; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 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) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 914 | case LYXML_ELEM_CONTENT: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 915 | /* content |</elem> */ |
| 916 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 917 | /* handle special case when empty content for "<elem/>" was returned */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 918 | if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 919 | assert(xmlctx->elements.count); |
| 920 | e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1]; |
| 921 | |
| 922 | /* close the element (parses closing tag) */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 923 | ret = lyxml_close_element(xmlctx, e->prefix, e->prefix_len, e->name, e->name_len, 1); |
| 924 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 925 | |
| 926 | /* update status */ |
| 927 | xmlctx->status = LYXML_ELEM_CLOSE; |
| 928 | break; |
| 929 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 930 | /* fall through */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 931 | case LYXML_ELEM_CLOSE: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 932 | /* </elem>| <elem2>* */ |
| 933 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 934 | /* parse next element, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 935 | ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len, &closing); |
| 936 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 937 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 938 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 939 | /* update status */ |
| 940 | xmlctx->status = LYXML_END; |
| 941 | } else if (closing) { |
| 942 | /* close an element (parses also closing tag) */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 943 | ret = lyxml_close_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len, 0); |
| 944 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 945 | |
| 946 | /* update status */ |
| 947 | xmlctx->status = LYXML_ELEM_CLOSE; |
| 948 | } else { |
| 949 | /* open an element, also parses all enclosed namespaces */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 950 | ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len); |
| 951 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 952 | |
| 953 | /* update status */ |
| 954 | xmlctx->status = LYXML_ELEMENT; |
| 955 | } |
| 956 | break; |
| 957 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 958 | case LYXML_ELEMENT: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 959 | /* <elem| attr='val'* > content */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 960 | case LYXML_ATTR_CONTENT: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 961 | /* attr='val'| attr='val'* > content */ |
| 962 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 963 | /* parse attribute name, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 964 | ret = lyxml_next_attribute(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len); |
| 965 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 966 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 967 | if (xmlctx->in->current[0] == '>') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 968 | /* no attributes but a closing tag */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 969 | ly_in_skip(xmlctx->in, 1); |
| 970 | if (!xmlctx->in->current[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 971 | LOGVAL(xmlctx->ctx, LY_VCODE_EOF); |
Michal Vasko | f55ae20 | 2020-06-30 15:49:36 +0200 | [diff] [blame] | 972 | ret = LY_EVALID; |
| 973 | goto cleanup; |
| 974 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 975 | |
| 976 | /* parse element content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 977 | 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] | 978 | &xmlctx->dynamic); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 979 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 980 | |
| 981 | if (!xmlctx->value_len) { |
Radek IÅ¡a | 017270d | 2021-02-16 10:26:15 +0100 | [diff] [blame] | 982 | /* empty value should by alocated staticaly, but check for in any case */ |
| 983 | if (xmlctx->dynamic) { |
| 984 | free((char *) xmlctx->value); |
| 985 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 986 | /* use empty value, easier to work with */ |
| 987 | xmlctx->value = ""; |
Radek IÅ¡a | 017270d | 2021-02-16 10:26:15 +0100 | [diff] [blame] | 988 | xmlctx->dynamic = 0; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /* update status */ |
| 992 | xmlctx->status = LYXML_ELEM_CONTENT; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 993 | } else if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 994 | /* no content but we still return it */ |
| 995 | xmlctx->value = ""; |
| 996 | xmlctx->value_len = 0; |
| 997 | xmlctx->ws_only = 1; |
| 998 | xmlctx->dynamic = 0; |
| 999 | |
| 1000 | /* update status */ |
| 1001 | xmlctx->status = LYXML_ELEM_CONTENT; |
| 1002 | } else { |
| 1003 | /* update status */ |
| 1004 | xmlctx->status = LYXML_ATTRIBUTE; |
| 1005 | } |
| 1006 | break; |
| 1007 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1008 | case LYXML_ATTRIBUTE: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1009 | /* attr|='val' */ |
| 1010 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1011 | /* skip formatting and parse value */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1012 | ret = lyxml_next_attr_content(xmlctx, &xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only, &xmlctx->dynamic); |
| 1013 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1014 | |
| 1015 | /* update status */ |
| 1016 | xmlctx->status = LYXML_ATTR_CONTENT; |
| 1017 | break; |
| 1018 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1019 | case LYXML_END: |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1020 | /* </elem> |EOF */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1021 | /* nothing to do */ |
| 1022 | break; |
| 1023 | } |
| 1024 | |
| 1025 | cleanup: |
| 1026 | if (ret) { |
| 1027 | /* invalidate context */ |
| 1028 | xmlctx->status = LYXML_END; |
| 1029 | } |
| 1030 | return ret; |
| 1031 | } |
| 1032 | |
| 1033 | LY_ERR |
| 1034 | lyxml_ctx_peek(struct lyxml_ctx *xmlctx, enum LYXML_PARSER_STATUS *next) |
| 1035 | { |
| 1036 | LY_ERR ret = LY_SUCCESS; |
| 1037 | const char *prefix, *name, *prev_input; |
| 1038 | size_t prefix_len, name_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1039 | ly_bool closing; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1040 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1041 | prev_input = xmlctx->in->current; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1042 | |
| 1043 | switch (xmlctx->status) { |
| 1044 | case LYXML_ELEM_CONTENT: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1045 | if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1046 | *next = LYXML_ELEM_CLOSE; |
| 1047 | break; |
| 1048 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1049 | /* fall through */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1050 | case LYXML_ELEM_CLOSE: |
| 1051 | /* parse next element, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1052 | ret = lyxml_next_element(xmlctx, &prefix, &prefix_len, &name, &name_len, &closing); |
| 1053 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1054 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1055 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1056 | *next = LYXML_END; |
| 1057 | } else if (closing) { |
| 1058 | *next = LYXML_ELEM_CLOSE; |
| 1059 | } else { |
| 1060 | *next = LYXML_ELEMENT; |
| 1061 | } |
| 1062 | break; |
| 1063 | case LYXML_ELEMENT: |
| 1064 | case LYXML_ATTR_CONTENT: |
| 1065 | /* parse attribute name, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1066 | ret = lyxml_next_attribute(xmlctx, &prefix, &prefix_len, &name, &name_len); |
| 1067 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1068 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1069 | if ((xmlctx->in->current[0] == '>') || (xmlctx->in->current[0] == '/')) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1070 | *next = LYXML_ELEM_CONTENT; |
| 1071 | } else { |
| 1072 | *next = LYXML_ATTRIBUTE; |
| 1073 | } |
| 1074 | break; |
| 1075 | case LYXML_ATTRIBUTE: |
| 1076 | *next = LYXML_ATTR_CONTENT; |
| 1077 | break; |
| 1078 | case LYXML_END: |
| 1079 | *next = LYXML_END; |
| 1080 | break; |
| 1081 | } |
| 1082 | |
| 1083 | cleanup: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1084 | xmlctx->in->current = prev_input; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1085 | return ret; |
| 1086 | } |
| 1087 | |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 1088 | /** |
| 1089 | * @brief Free all namespaces in XML context. |
| 1090 | * |
| 1091 | * @param[in] xmlctx XML context to use. |
| 1092 | */ |
| 1093 | static void |
| 1094 | lyxml_ns_rm_all(struct lyxml_ctx *xmlctx) |
| 1095 | { |
| 1096 | struct lyxml_ns *ns; |
| 1097 | uint32_t i; |
| 1098 | |
| 1099 | for (i = 0; i < xmlctx->ns.count; ++i) { |
| 1100 | ns = xmlctx->ns.objs[i]; |
| 1101 | |
| 1102 | free(ns->prefix); |
| 1103 | free(ns->uri); |
| 1104 | free(ns); |
| 1105 | } |
| 1106 | ly_set_erase(&xmlctx->ns, NULL); |
| 1107 | } |
| 1108 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1109 | void |
| 1110 | lyxml_ctx_free(struct lyxml_ctx *xmlctx) |
| 1111 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1112 | if (!xmlctx) { |
| 1113 | return; |
| 1114 | } |
| 1115 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1116 | LOG_LOCBACK(0, 0, 0, 1); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1117 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1118 | if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) { |
| 1119 | free((char *)xmlctx->value); |
| 1120 | } |
| 1121 | ly_set_erase(&xmlctx->elements, free); |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 1122 | lyxml_ns_rm_all(xmlctx); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1123 | free(xmlctx); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 1124 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1125 | |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 1126 | /** |
| 1127 | * @brief Duplicate an XML element. |
| 1128 | * |
| 1129 | * @param[in] elem Element to duplicate. |
| 1130 | * @return Element duplicate. |
| 1131 | * @return NULL on error. |
| 1132 | */ |
| 1133 | static struct lyxml_elem * |
| 1134 | lyxml_elem_dup(const struct lyxml_elem *elem) |
| 1135 | { |
| 1136 | struct lyxml_elem *dup; |
| 1137 | |
| 1138 | dup = malloc(sizeof *dup); |
| 1139 | LY_CHECK_ERR_RET(!dup, LOGMEM(NULL), NULL); |
| 1140 | |
| 1141 | memcpy(dup, elem, sizeof *dup); |
| 1142 | |
| 1143 | return dup; |
| 1144 | } |
| 1145 | |
| 1146 | /** |
| 1147 | * @brief Duplicate an XML namespace. |
| 1148 | * |
| 1149 | * @param[in] ns Namespace to duplicate. |
| 1150 | * @return Namespace duplicate. |
| 1151 | * @return NULL on error. |
| 1152 | */ |
| 1153 | static struct lyxml_ns * |
| 1154 | lyxml_ns_dup(const struct lyxml_ns *ns) |
| 1155 | { |
| 1156 | struct lyxml_ns *dup; |
| 1157 | |
| 1158 | dup = malloc(sizeof *dup); |
| 1159 | LY_CHECK_ERR_RET(!dup, LOGMEM(NULL), NULL); |
| 1160 | |
| 1161 | if (ns->prefix) { |
| 1162 | dup->prefix = strdup(ns->prefix); |
| 1163 | LY_CHECK_ERR_RET(!dup->prefix, LOGMEM(NULL); free(dup), NULL); |
| 1164 | } else { |
| 1165 | dup->prefix = NULL; |
| 1166 | } |
| 1167 | dup->uri = strdup(ns->uri); |
| 1168 | LY_CHECK_ERR_RET(!dup->uri, LOGMEM(NULL); free(dup->prefix); free(dup), NULL); |
| 1169 | dup->depth = ns->depth; |
| 1170 | |
| 1171 | return dup; |
| 1172 | } |
| 1173 | |
| 1174 | LY_ERR |
| 1175 | lyxml_ctx_backup(struct lyxml_ctx *xmlctx, struct lyxml_ctx *backup) |
| 1176 | { |
| 1177 | uint32_t i; |
| 1178 | |
| 1179 | /* first make shallow copy */ |
| 1180 | memcpy(backup, xmlctx, sizeof *backup); |
| 1181 | |
| 1182 | if ((xmlctx->status == LYXML_ELEM_CONTENT) && xmlctx->dynamic) { |
| 1183 | /* it was backed up, do not free */ |
| 1184 | xmlctx->dynamic = 0; |
| 1185 | } |
| 1186 | |
| 1187 | /* backup in current pointer only */ |
| 1188 | backup->in = (void *)xmlctx->in->current; |
| 1189 | |
| 1190 | /* duplicate elements */ |
| 1191 | backup->elements.objs = malloc(xmlctx->elements.size * sizeof(struct lyxml_elem)); |
Michal Vasko | 0c2403d | 2021-09-15 08:52:18 +0200 | [diff] [blame] | 1192 | LY_CHECK_ERR_RET(!backup->elements.objs, LOGMEM(xmlctx->ctx), LY_EMEM); |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 1193 | for (i = 0; i < xmlctx->elements.count; ++i) { |
| 1194 | backup->elements.objs[i] = lyxml_elem_dup(xmlctx->elements.objs[i]); |
Michal Vasko | 0c2403d | 2021-09-15 08:52:18 +0200 | [diff] [blame] | 1195 | LY_CHECK_RET(!backup->elements.objs[i], LY_EMEM); |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /* duplicate ns */ |
| 1199 | backup->ns.objs = malloc(xmlctx->ns.size * sizeof(struct lyxml_ns)); |
Michal Vasko | 0c2403d | 2021-09-15 08:52:18 +0200 | [diff] [blame] | 1200 | LY_CHECK_ERR_RET(!backup->ns.objs, LOGMEM(xmlctx->ctx), LY_EMEM); |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 1201 | for (i = 0; i < xmlctx->ns.count; ++i) { |
| 1202 | backup->ns.objs[i] = lyxml_ns_dup(xmlctx->ns.objs[i]); |
Michal Vasko | 0c2403d | 2021-09-15 08:52:18 +0200 | [diff] [blame] | 1203 | LY_CHECK_RET(!backup->ns.objs[i], LY_EMEM); |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | return LY_SUCCESS; |
| 1207 | } |
| 1208 | |
| 1209 | void |
| 1210 | lyxml_ctx_restore(struct lyxml_ctx *xmlctx, struct lyxml_ctx *backup) |
| 1211 | { |
| 1212 | if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) { |
| 1213 | /* free dynamic value */ |
| 1214 | free((char *)xmlctx->value); |
| 1215 | } |
| 1216 | |
| 1217 | /* free elements */ |
| 1218 | ly_set_erase(&xmlctx->elements, free); |
| 1219 | |
| 1220 | /* free ns */ |
| 1221 | lyxml_ns_rm_all(xmlctx); |
| 1222 | |
| 1223 | /* restore in current pointer */ |
| 1224 | xmlctx->in->current = (void *)backup->in; |
| 1225 | backup->in = xmlctx->in; |
| 1226 | |
| 1227 | /* restore backup */ |
| 1228 | memcpy(xmlctx, backup, sizeof *xmlctx); |
| 1229 | } |
| 1230 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1231 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1232 | 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] | 1233 | { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1234 | LY_ERR ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1235 | |
| 1236 | if (!text) { |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1240 | for (uint64_t u = 0; text[u]; u++) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1241 | switch (text[u]) { |
| 1242 | case '&': |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1243 | ret = ly_print_(out, "&"); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1244 | break; |
| 1245 | case '<': |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1246 | ret = ly_print_(out, "<"); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1247 | break; |
| 1248 | case '>': |
| 1249 | /* not needed, just for readability */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1250 | ret = ly_print_(out, ">"); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1251 | break; |
| 1252 | case '"': |
| 1253 | if (attribute) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1254 | ret = ly_print_(out, """); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1255 | break; |
| 1256 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1257 | /* fall through */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1258 | default: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1259 | ret = ly_write_(out, &text[u], 1); |
| 1260 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1261 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1262 | LY_CHECK_RET(ret); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1265 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1268 | LY_ERR |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1269 | lyxml_value_compare(const struct ly_ctx *ctx1, const char *value1, void *val_prefix_data1, |
| 1270 | const struct ly_ctx *ctx2, const char *value2, void *val_prefix_data2) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1271 | { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1272 | const char *value1_iter, *value2_iter; |
| 1273 | const char *value1_next, *value2_next; |
| 1274 | uint32_t value1_len, value2_len; |
| 1275 | ly_bool is_prefix1, is_prefix2; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1276 | const struct lys_module *mod1, *mod2; |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1277 | LY_ERR ret; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1278 | |
| 1279 | if (!value1 && !value2) { |
| 1280 | return LY_SUCCESS; |
| 1281 | } |
| 1282 | if ((value1 && !value2) || (!value1 && value2)) { |
| 1283 | return LY_ENOT; |
| 1284 | } |
| 1285 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1286 | if (!ctx2) { |
| 1287 | ctx2 = ctx1; |
| 1288 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1289 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1290 | ret = LY_SUCCESS; |
| 1291 | for (value1_iter = value1, value2_iter = value2; |
| 1292 | value1_iter && value2_iter; |
| 1293 | value1_iter = value1_next, value2_iter = value2_next) { |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 1294 | if ((ret = ly_value_prefix_next(value1_iter, NULL, &value1_len, &is_prefix1, &value1_next))) { |
| 1295 | break; |
| 1296 | } |
| 1297 | if ((ret = ly_value_prefix_next(value2_iter, NULL, &value2_len, &is_prefix2, &value2_next))) { |
| 1298 | break; |
| 1299 | } |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1300 | |
| 1301 | if (is_prefix1 != is_prefix2) { |
| 1302 | ret = LY_ENOT; |
| 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | if (!is_prefix1) { |
| 1307 | if (value1_len != value2_len) { |
| 1308 | ret = LY_ENOT; |
| 1309 | break; |
| 1310 | } |
| 1311 | if (strncmp(value1_iter, value2_iter, value1_len)) { |
| 1312 | ret = LY_ENOT; |
| 1313 | break; |
| 1314 | } |
| 1315 | continue; |
| 1316 | } |
| 1317 | |
| 1318 | mod1 = mod2 = NULL; |
| 1319 | if (val_prefix_data1) { |
| 1320 | /* find module of the first prefix, if any */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1321 | 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] | 1322 | } |
| 1323 | if (val_prefix_data2) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1324 | 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] | 1325 | } |
| 1326 | if (!mod1 || !mod2) { |
| 1327 | /* not a prefix or maps to different namespaces */ |
| 1328 | ret = LY_ENOT; |
| 1329 | break; |
| 1330 | } |
| 1331 | |
| 1332 | if (mod1->ctx == mod2->ctx) { |
| 1333 | /* same contexts */ |
| 1334 | if ((mod1->name != mod2->name) || (mod1->revision != mod2->revision)) { |
| 1335 | ret = LY_ENOT; |
| 1336 | break; |
| 1337 | } |
| 1338 | } else { |
| 1339 | /* different contexts */ |
| 1340 | if (strcmp(mod1->name, mod2->name)) { |
| 1341 | ret = LY_ENOT; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1342 | break; |
| 1343 | } |
| 1344 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1345 | if (mod1->revision || mod2->revision) { |
| 1346 | if (!mod1->revision || !mod2->revision) { |
| 1347 | ret = LY_ENOT; |
| 1348 | break; |
| 1349 | } |
| 1350 | if (strcmp(mod1->revision, mod2->revision)) { |
| 1351 | ret = LY_ENOT; |
| 1352 | break; |
| 1353 | } |
| 1354 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1355 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1356 | } |
| 1357 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1358 | if (value1_iter || value2_iter) { |
| 1359 | ret = LY_ENOT; |
| 1360 | } |
| 1361 | |
| 1362 | return ret; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1363 | } |