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