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 = "-->"; |
| 258 | endtag_len = 3; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 259 | } else if (!strncmp(xmlctx->in->current, "[CDATA[", 7)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 260 | /* CDATA section */ |
| 261 | move_input(xmlctx, 7); |
| 262 | sectname = "CData"; |
| 263 | endtag = "]]>"; |
| 264 | endtag_len = 3; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 265 | } else if (!strncmp(xmlctx->in->current, "DOCTYPE", 7)) { |
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 */ |
| 392 | if (!strncmp(&in[offset], "lt;", 3)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 393 | buf[len++] = '<'; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 394 | in += 4; /* < */ |
| 395 | } else if (!strncmp(&in[offset], "gt;", 3)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 396 | buf[len++] = '>'; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 397 | in += 4; /* > */ |
| 398 | } else if (!strncmp(&in[offset], "amp;", 4)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 399 | buf[len++] = '&'; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 400 | in += 5; /* & */ |
| 401 | } else if (!strncmp(&in[offset], "apos;", 5)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 402 | buf[len++] = '\''; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 403 | in += 6; /* ' */ |
| 404 | } else if (!strncmp(&in[offset], "quot;", 5)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 405 | buf[len++] = '\"'; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 406 | in += 6; /* " */ |
| 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++) { |
| 419 | n = (10 * n) + (in[offset] - '0'); |
| 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') { |
| 426 | u = 10 + (in[offset] - 'a'); |
| 427 | } else { |
| 428 | u = 10 + (in[offset] - 'A'); |
| 429 | } |
| 430 | n = (16 * n) + u; |
| 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) { |
| 850 | /* content |</elem> */ |
| 851 | case LYXML_ELEM_CONTENT: |
| 852 | /* handle special case when empty content for "<elem/>" was returned */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 853 | if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 854 | assert(xmlctx->elements.count); |
| 855 | e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1]; |
| 856 | |
| 857 | /* close the element (parses closing tag) */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 858 | ret = lyxml_close_element(xmlctx, e->prefix, e->prefix_len, e->name, e->name_len, 1); |
| 859 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 860 | |
| 861 | /* update status */ |
| 862 | xmlctx->status = LYXML_ELEM_CLOSE; |
| 863 | break; |
| 864 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 865 | /* fallthrough */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 866 | |
| 867 | /* </elem>| <elem2>* */ |
| 868 | case LYXML_ELEM_CLOSE: |
| 869 | /* parse next element, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 870 | ret = lyxml_next_element(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len, &closing); |
| 871 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 872 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 873 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 874 | /* update status */ |
| 875 | xmlctx->status = LYXML_END; |
| 876 | } else if (closing) { |
| 877 | /* close an element (parses also closing tag) */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 878 | ret = lyxml_close_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len, 0); |
| 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_ELEM_CLOSE; |
| 883 | } else { |
| 884 | /* open an element, also parses all enclosed namespaces */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 885 | ret = lyxml_open_element(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len); |
| 886 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 887 | |
| 888 | /* update status */ |
| 889 | xmlctx->status = LYXML_ELEMENT; |
| 890 | } |
| 891 | break; |
| 892 | |
| 893 | /* <elem| attr='val'* > content */ |
| 894 | case LYXML_ELEMENT: |
| 895 | |
| 896 | /* attr='val'| attr='val'* > content */ |
| 897 | case LYXML_ATTR_CONTENT: |
| 898 | /* parse attribute name, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 899 | ret = lyxml_next_attribute(xmlctx, &xmlctx->prefix, &xmlctx->prefix_len, &xmlctx->name, &xmlctx->name_len); |
| 900 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 901 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 902 | if (xmlctx->in->current[0] == '>') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 903 | /* no attributes but a closing tag */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 904 | ly_in_skip(xmlctx->in, 1); |
| 905 | if (!xmlctx->in->current[0]) { |
Michal Vasko | f55ae20 | 2020-06-30 15:49:36 +0200 | [diff] [blame] | 906 | LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF); |
| 907 | ret = LY_EVALID; |
| 908 | goto cleanup; |
| 909 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 910 | |
| 911 | /* parse element content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 912 | 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] | 913 | &xmlctx->dynamic); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 914 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 915 | |
| 916 | if (!xmlctx->value_len) { |
| 917 | /* use empty value, easier to work with */ |
| 918 | xmlctx->value = ""; |
| 919 | assert(!xmlctx->dynamic); |
| 920 | } |
| 921 | |
| 922 | /* update status */ |
| 923 | xmlctx->status = LYXML_ELEM_CONTENT; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 924 | } else if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 925 | /* no content but we still return it */ |
| 926 | xmlctx->value = ""; |
| 927 | xmlctx->value_len = 0; |
| 928 | xmlctx->ws_only = 1; |
| 929 | xmlctx->dynamic = 0; |
| 930 | |
| 931 | /* update status */ |
| 932 | xmlctx->status = LYXML_ELEM_CONTENT; |
| 933 | } else { |
| 934 | /* update status */ |
| 935 | xmlctx->status = LYXML_ATTRIBUTE; |
| 936 | } |
| 937 | break; |
| 938 | |
| 939 | /* attr|='val' */ |
| 940 | case LYXML_ATTRIBUTE: |
| 941 | /* skip formatting and parse value */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 942 | ret = lyxml_next_attr_content(xmlctx, &xmlctx->value, &xmlctx->value_len, &xmlctx->ws_only, &xmlctx->dynamic); |
| 943 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 944 | |
| 945 | /* update status */ |
| 946 | xmlctx->status = LYXML_ATTR_CONTENT; |
| 947 | break; |
| 948 | |
| 949 | /* </elem> |EOF */ |
| 950 | case LYXML_END: |
| 951 | /* nothing to do */ |
| 952 | break; |
| 953 | } |
| 954 | |
| 955 | cleanup: |
| 956 | if (ret) { |
| 957 | /* invalidate context */ |
| 958 | xmlctx->status = LYXML_END; |
| 959 | } |
| 960 | return ret; |
| 961 | } |
| 962 | |
| 963 | LY_ERR |
| 964 | lyxml_ctx_peek(struct lyxml_ctx *xmlctx, enum LYXML_PARSER_STATUS *next) |
| 965 | { |
| 966 | LY_ERR ret = LY_SUCCESS; |
| 967 | const char *prefix, *name, *prev_input; |
| 968 | size_t prefix_len, name_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 969 | ly_bool closing; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 970 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 971 | prev_input = xmlctx->in->current; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 972 | |
| 973 | switch (xmlctx->status) { |
| 974 | case LYXML_ELEM_CONTENT: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 975 | if (xmlctx->in->current[0] == '/') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 976 | *next = LYXML_ELEM_CLOSE; |
| 977 | break; |
| 978 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 979 | /* fallthrough */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 980 | case LYXML_ELEM_CLOSE: |
| 981 | /* parse next element, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 982 | ret = lyxml_next_element(xmlctx, &prefix, &prefix_len, &name, &name_len, &closing); |
| 983 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 984 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 985 | if (xmlctx->in->current[0] == '\0') { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 986 | *next = LYXML_END; |
| 987 | } else if (closing) { |
| 988 | *next = LYXML_ELEM_CLOSE; |
| 989 | } else { |
| 990 | *next = LYXML_ELEMENT; |
| 991 | } |
| 992 | break; |
| 993 | case LYXML_ELEMENT: |
| 994 | case LYXML_ATTR_CONTENT: |
| 995 | /* parse attribute name, if any */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 996 | ret = lyxml_next_attribute(xmlctx, &prefix, &prefix_len, &name, &name_len); |
| 997 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 998 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 999 | if ((xmlctx->in->current[0] == '>') || (xmlctx->in->current[0] == '/')) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1000 | *next = LYXML_ELEM_CONTENT; |
| 1001 | } else { |
| 1002 | *next = LYXML_ATTRIBUTE; |
| 1003 | } |
| 1004 | break; |
| 1005 | case LYXML_ATTRIBUTE: |
| 1006 | *next = LYXML_ATTR_CONTENT; |
| 1007 | break; |
| 1008 | case LYXML_END: |
| 1009 | *next = LYXML_END; |
| 1010 | break; |
| 1011 | } |
| 1012 | |
| 1013 | cleanup: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1014 | xmlctx->in->current = prev_input; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 1015 | return ret; |
| 1016 | } |
| 1017 | |
| 1018 | void |
| 1019 | lyxml_ctx_free(struct lyxml_ctx *xmlctx) |
| 1020 | { |
| 1021 | uint32_t u; |
| 1022 | |
| 1023 | if (!xmlctx) { |
| 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | if (((xmlctx->status == LYXML_ELEM_CONTENT) || (xmlctx->status == LYXML_ATTR_CONTENT)) && xmlctx->dynamic) { |
| 1028 | free((char *)xmlctx->value); |
| 1029 | } |
| 1030 | ly_set_erase(&xmlctx->elements, free); |
| 1031 | for (u = xmlctx->ns.count - 1; u + 1 > 0; --u) { |
| 1032 | /* remove the ns structure */ |
| 1033 | free(((struct lyxml_ns *)xmlctx->ns.objs[u])->prefix); |
| 1034 | free(((struct lyxml_ns *)xmlctx->ns.objs[u])->uri); |
| 1035 | free(xmlctx->ns.objs[u]); |
| 1036 | } |
| 1037 | ly_set_erase(&xmlctx->ns, NULL); |
| 1038 | free(xmlctx); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 1039 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1040 | |
| 1041 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1042 | 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] | 1043 | { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1044 | LY_ERR ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1045 | |
| 1046 | if (!text) { |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1050 | for (uint64_t u = 0; text[u]; u++) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1051 | switch (text[u]) { |
| 1052 | case '&': |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1053 | ret = ly_print_(out, "&"); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1054 | break; |
| 1055 | case '<': |
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 | /* not needed, just for readability */ |
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 | case '"': |
| 1063 | if (attribute) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1064 | ret = ly_print_(out, """); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1065 | break; |
| 1066 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1067 | /* falls through */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1068 | default: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1069 | ret = ly_write_(out, &text[u], 1); |
| 1070 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1071 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1072 | LY_CHECK_RET(ret); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 1075 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1078 | LY_ERR |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1079 | lyxml_value_compare(const struct ly_ctx *ctx, const char *value1, void *val_prefix_data1, const char *value2, |
| 1080 | void *val_prefix_data2) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1081 | { |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1082 | const char *ptr1, *ptr2, *end1, *end2; |
| 1083 | const struct lys_module *mod1, *mod2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1084 | |
| 1085 | if (!value1 && !value2) { |
| 1086 | return LY_SUCCESS; |
| 1087 | } |
| 1088 | if ((value1 && !value2) || (!value1 && value2)) { |
| 1089 | return LY_ENOT; |
| 1090 | } |
| 1091 | |
| 1092 | ptr1 = value1; |
| 1093 | ptr2 = value2; |
| 1094 | while (ptr1[0] && ptr2[0]) { |
| 1095 | if (ptr1[0] != ptr2[0]) { |
| 1096 | /* 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] | 1097 | mod1 = mod2 = NULL; |
| 1098 | if (val_prefix_data1 && (end1 = strchr(ptr1, ':'))) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1099 | /* find module of the first prefix, if any */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1100 | 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] | 1101 | } |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1102 | if (val_prefix_data2 && (end2 = strchr(ptr2, ':'))) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1103 | /* find module of the second prefix, if any */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1104 | 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] | 1105 | } |
| 1106 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1107 | if (!mod1 || !mod2 || (mod1 != mod2)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1108 | /* not a prefix or maps to different namespaces */ |
| 1109 | break; |
| 1110 | } |
| 1111 | |
| 1112 | /* skip prefixes in both values (':' is skipped as iter) */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1113 | ptr1 = end1; |
| 1114 | ptr2 = end2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | ++ptr1; |
| 1118 | ++ptr2; |
| 1119 | } |
| 1120 | if (ptr1[0] || ptr2[0]) { |
| 1121 | /* not a match or simply different lengths */ |
| 1122 | return LY_ENOT; |
| 1123 | } |
| 1124 | |
| 1125 | return LY_SUCCESS; |
| 1126 | } |