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