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