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