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