Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file xml.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Generic XML parser implementation for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | c1c03d6 | 2018-11-27 10:52:43 +0100 | [diff] [blame] | 15 | #include "common.h" |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 16 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 19 | #include <stdbool.h> |
| 20 | #include <stdint.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 22 | #include <string.h> |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 23 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 24 | #include "xml.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | #include "printer_internal.h" |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 26 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 27 | /* Move input p by s characters, if EOF log with lyxml_context c */ |
| 28 | #define move_input(c,p,s) p += s; LY_CHECK_ERR_RET(!p[0], LOGVAL(c->ctx, LY_VLOG_LINE, &c->line, LY_VCODE_EOF), LY_EVALID) |
| 29 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 30 | /* Ignore whitespaces in the input string p */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 31 | #define ign_xmlws(c,p) while (is_xmlws(*(p))) {if (*(p) == '\n') {++c->line;} ++p;} |
| 32 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 33 | /** |
| 34 | * @brief Ignore any characters until the delim of the size delim_len is read |
| 35 | * |
| 36 | * Detects number of read new lines. |
| 37 | * Returns the pointer to the beginning of the detected delim, or NULL in case the delim not found in |
| 38 | * NULL-terminated input string. |
| 39 | * */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 40 | static const char * |
| 41 | ign_todelim(register const char *input, const char *delim, size_t delim_len, size_t *newlines) |
| 42 | { |
| 43 | size_t i; |
| 44 | register const char *a, *b; |
| 45 | |
| 46 | (*newlines) = 0; |
| 47 | for ( ; *input; ++input) { |
| 48 | if (*input != *delim) { |
| 49 | if (*input == '\n') { |
| 50 | ++(*newlines); |
| 51 | } |
| 52 | continue; |
| 53 | } |
| 54 | a = input; |
| 55 | b = delim; |
| 56 | for (i = 0; i < delim_len; ++i) { |
| 57 | if (*a++ != *b++) { |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | if (i == delim_len) { |
| 62 | return input; |
| 63 | } |
| 64 | } |
| 65 | return NULL; |
| 66 | } |
| 67 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 68 | /** |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 69 | * Store UTF-8 character specified as 4byte integer into the dst buffer. |
| 70 | * Returns number of written bytes (4 max), expects that dst has enough space. |
| 71 | * |
| 72 | * UTF-8 mapping: |
| 73 | * 00000000 -- 0000007F: 0xxxxxxx |
| 74 | * 00000080 -- 000007FF: 110xxxxx 10xxxxxx |
| 75 | * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx |
| 76 | * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 77 | * |
| 78 | * Includes checking for valid characters (following RFC 7950, sec 9.4) |
| 79 | */ |
| 80 | static LY_ERR |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 81 | lyxml_pututf8(char *dst, uint32_t value, size_t *bytes_written) |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 82 | { |
| 83 | if (value < 0x80) { |
| 84 | /* one byte character */ |
| 85 | if (value < 0x20 && |
| 86 | value != 0x09 && |
| 87 | value != 0x0a && |
| 88 | value != 0x0d) { |
| 89 | return LY_EINVAL; |
| 90 | } |
| 91 | |
| 92 | dst[0] = value; |
| 93 | (*bytes_written) = 1; |
| 94 | } else if (value < 0x800) { |
| 95 | /* two bytes character */ |
| 96 | dst[0] = 0xc0 | (value >> 6); |
| 97 | dst[1] = 0x80 | (value & 0x3f); |
| 98 | (*bytes_written) = 2; |
| 99 | } else if (value < 0xfffe) { |
| 100 | /* three bytes character */ |
| 101 | if (((value & 0xf800) == 0xd800) || |
| 102 | (value >= 0xfdd0 && value <= 0xfdef)) { |
| 103 | /* exclude surrogate blocks %xD800-DFFF */ |
| 104 | /* exclude noncharacters %xFDD0-FDEF */ |
| 105 | return LY_EINVAL; |
| 106 | } |
| 107 | |
| 108 | dst[0] = 0xe0 | (value >> 12); |
| 109 | dst[1] = 0x80 | ((value >> 6) & 0x3f); |
| 110 | dst[2] = 0x80 | (value & 0x3f); |
| 111 | |
| 112 | (*bytes_written) = 3; |
| 113 | } else if (value < 0x10fffe) { |
| 114 | if ((value & 0xffe) == 0xffe) { |
| 115 | /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF, |
| 116 | * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF, |
| 117 | * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */ |
| 118 | return LY_EINVAL; |
| 119 | } |
| 120 | /* four bytes character */ |
| 121 | dst[0] = 0xf0 | (value >> 18); |
| 122 | dst[1] = 0x80 | ((value >> 12) & 0x3f); |
| 123 | dst[2] = 0x80 | ((value >> 6) & 0x3f); |
| 124 | dst[3] = 0x80 | (value & 0x3f); |
| 125 | |
| 126 | (*bytes_written) = 4; |
| 127 | } |
| 128 | return LY_SUCCESS; |
| 129 | } |
| 130 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 131 | /** |
| 132 | * @brief Check/Get an XML qualified name from the input string. |
| 133 | * |
| 134 | * The identifier must have at least one valid character complying the name start character constraints. |
| 135 | * The identifier is terminated by the first character, which does not comply to the name character constraints. |
| 136 | * |
| 137 | * See https://www.w3.org/TR/xml-names/#NT-NCName |
| 138 | * |
| 139 | * @param[in] context XML context to track lines or store errors into libyang context. |
| 140 | * @param[in,out] input Input string to process, updated according to the processed/read data. |
| 141 | * Note that the term_char is also read, so input points after the term_char at the end. |
| 142 | * @param[out] term_char The first character in the input string which does not compy to the name constraints. |
| 143 | * @param[out] term_char_len Number of bytes used to encode UTF8 term_char. Serves to be able to go back in input string. |
| 144 | * @return LY_ERR value. |
| 145 | */ |
| 146 | static LY_ERR |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 147 | lyxml_check_qname(struct lyxml_context *context, const char **input, unsigned int *term_char, size_t *term_char_len) |
| 148 | { |
| 149 | unsigned int c; |
| 150 | const char *id = (*input); |
| 151 | LY_ERR rc; |
| 152 | |
| 153 | /* check NameStartChar (minus colon) */ |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 154 | LY_CHECK_ERR_RET(ly_getutf8(input, &c, NULL) != LY_SUCCESS, |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 155 | LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID); |
| 156 | LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c), |
| 157 | LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, |
| 158 | "Identifier \"%s\" starts with invalid character.", id), |
| 159 | LY_EVALID); |
| 160 | |
| 161 | /* check rest of the identifier */ |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 162 | for (rc = ly_getutf8(input, &c, term_char_len); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 163 | rc == LY_SUCCESS && is_xmlqnamechar(c); |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 164 | rc = ly_getutf8(input, &c, term_char_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 165 | LY_CHECK_ERR_RET(rc != LY_SUCCESS, LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID); |
| 166 | |
| 167 | (*term_char) = c; |
| 168 | return LY_SUCCESS; |
| 169 | } |
| 170 | |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 171 | /** |
| 172 | * @brief Add namespace definition into XML context. |
| 173 | * |
| 174 | * Namespaces from a single element are supposed to be added sequentially together (not interleaved by a namespace from other |
| 175 | * element). This mimic namespace visibility, since the namespace defined in element E is not visible from its parents or |
| 176 | * siblings. On the other hand, namespace from a parent element can be redefined in a child element. This is also reflected |
| 177 | * by lyxml_ns_get() which returns the most recent namespace definition for the given prefix. |
| 178 | * |
| 179 | * When leaving processing of a subtree of some element (after it is removed from context->elements), caller is supposed to call |
| 180 | * lyxml_ns_rm() to remove all the namespaces defined in such an element from the context. |
| 181 | * |
| 182 | * @param[in] context XML context to work with. |
| 183 | * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute(). Can be NULL for default namespace. |
| 184 | * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from lyxml_get_attribute()). |
| 185 | * @param[in] uri Namespace URI (value) to store. Value can be obtained via lyxml_get_string() and caller is not supposed to |
| 186 | * work with the pointer when the function succeeds. In case of error the value is freed. |
| 187 | * @return LY_ERR values. |
| 188 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 189 | LY_ERR |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 190 | lyxml_ns_add(struct lyxml_context *context, const char *prefix, size_t prefix_len, char *uri) |
| 191 | { |
| 192 | struct lyxml_ns *ns; |
| 193 | |
| 194 | ns = malloc(sizeof *ns); |
| 195 | LY_CHECK_ERR_RET(!ns, LOGMEM(context->ctx), LY_EMEM); |
| 196 | |
| 197 | /* we need to connect the depth of the element where the namespace is defined with the |
| 198 | * namespace record to be able to maintain (remove) the record when the parser leaves |
| 199 | * (to its sibling or back to the parent) the element where the namespace was defined */ |
| 200 | ns->depth = context->elements.count; |
| 201 | |
| 202 | ns->uri = uri; |
| 203 | if (prefix) { |
| 204 | ns->prefix = strndup(prefix, prefix_len); |
| 205 | LY_CHECK_ERR_RET(!ns->prefix, LOGMEM(context->ctx); free(ns->uri); free(ns), LY_EMEM); |
| 206 | } else { |
| 207 | ns->prefix = NULL; |
| 208 | } |
| 209 | |
| 210 | LY_CHECK_ERR_RET(ly_set_add(&context->ns, ns, LY_SET_OPT_USEASLIST) == -1, |
| 211 | free(ns->prefix); free(ns->uri); free(ns), LY_EMEM); |
| 212 | return LY_SUCCESS; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @brief Remove all the namespaces defined in the element recently closed (removed from the context->elements). |
| 217 | * |
| 218 | * @param[in] context XML context to work with. |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 219 | */ |
Radek Krejci | 17dca99 | 2019-05-17 10:53:27 +0200 | [diff] [blame] | 220 | void |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 221 | lyxml_ns_rm(struct lyxml_context *context) |
| 222 | { |
| 223 | unsigned int u; |
| 224 | |
| 225 | for (u = context->ns.count - 1; u + 1 > 0; --u) { |
| 226 | if (((struct lyxml_ns *)context->ns.objs[u])->depth != context->elements.count + 1) { |
| 227 | /* we are done, the namespaces from a single element are supposed to be together */ |
| 228 | break; |
| 229 | } |
| 230 | /* remove the ns structure */ |
| 231 | free(((struct lyxml_ns *)context->ns.objs[u])->prefix); |
| 232 | free(((struct lyxml_ns *)context->ns.objs[u])->uri); |
| 233 | free(context->ns.objs[u]); |
| 234 | --context->ns.count; |
| 235 | } |
| 236 | |
| 237 | if (!context->ns.count) { |
| 238 | /* cleanup the context's namespaces storage */ |
| 239 | ly_set_erase(&context->ns, NULL); |
| 240 | } |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | const struct lyxml_ns * |
| 244 | lyxml_ns_get(struct lyxml_context *context, const char *prefix, size_t prefix_len) |
| 245 | { |
| 246 | unsigned int u; |
| 247 | struct lyxml_ns *ns; |
| 248 | |
| 249 | for (u = context->ns.count - 1; u + 1 > 0; --u) { |
| 250 | ns = (struct lyxml_ns *)context->ns.objs[u]; |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 251 | if (prefix && prefix_len) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 252 | if (ns->prefix && !ly_strncmp(ns->prefix, prefix, prefix_len)) { |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 253 | return ns; |
| 254 | } |
| 255 | } else if (!ns->prefix) { |
| 256 | /* default namespace */ |
| 257 | return ns; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return NULL; |
| 262 | } |
| 263 | |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 264 | static LY_ERR |
| 265 | lyxml_parse_element_start(struct lyxml_context *context, const char **input, int *closing) |
| 266 | { |
| 267 | struct ly_ctx *ctx = context->ctx; /* shortcut */ |
| 268 | const char *in = (*input); |
| 269 | const char *endtag; |
| 270 | const char *sectname; |
| 271 | size_t endtag_len, newlines; |
| 272 | |
| 273 | while (1) { |
| 274 | ign_xmlws(context, in); |
| 275 | |
| 276 | if (in[0] == '\0') { |
| 277 | /* EOF */ |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 278 | if (context->elements.count) { |
| 279 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF); |
| 280 | return LY_EVALID; |
| 281 | } |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 282 | context->status = LYXML_END; |
| 283 | (*input) = in; |
| 284 | return LY_SUCCESS; |
| 285 | } else if (in[0] != '<') { |
| 286 | return LY_EINVAL; |
| 287 | } |
| 288 | move_input(context, in, 1); |
| 289 | |
| 290 | if (in[0] == '!') { |
| 291 | move_input(context, in, 1); |
| 292 | /* sections to ignore */ |
| 293 | if (!strncmp(in, "--", 2)) { |
| 294 | /* comment */ |
| 295 | move_input(context, in, 2); |
| 296 | sectname = "Comment"; |
| 297 | endtag = "-->"; |
| 298 | endtag_len = 3; |
| 299 | } else if (!strncmp(in, "[CDATA[", 7)) { |
| 300 | /* CDATA section */ |
| 301 | move_input(context, in, 7); |
| 302 | sectname = "CData"; |
| 303 | endtag = "]]>"; |
| 304 | endtag_len = 3; |
| 305 | } else if (!strncmp(in, "DOCTYPE", 7)) { |
| 306 | /* Document type declaration - not supported */ |
| 307 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP, "Document Type Declaration"); |
| 308 | return LY_EVALID; |
| 309 | } else { |
| 310 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Unknown XML section \"%.20s\".", &in[-2]); |
| 311 | return LY_EVALID; |
| 312 | } |
| 313 | in = ign_todelim(in, endtag, endtag_len, &newlines); |
| 314 | LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, sectname), LY_EVALID); |
| 315 | context->line += newlines; |
| 316 | in += endtag_len; |
| 317 | } else if (in[0] == '?') { |
| 318 | in = ign_todelim(in, "?>", 2, &newlines); |
| 319 | LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID); |
| 320 | context->line += newlines; |
| 321 | in += 2; |
| 322 | } else if (in[0] == '/') { |
| 323 | /* closing element tag */ |
| 324 | *closing = 1; |
| 325 | ++in; |
| 326 | goto element; |
| 327 | } else { |
| 328 | /* opening element tag */ |
| 329 | *closing = 0; |
| 330 | element: |
| 331 | ign_xmlws(context, in); |
| 332 | LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID); |
| 333 | |
| 334 | (*input) = in; |
| 335 | return LY_SUCCESS; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | static LY_ERR |
| 341 | lyxml_parse_element_name(struct lyxml_context *context, const char **input, size_t *endtag_len, unsigned int *term_char, |
| 342 | const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 343 | { |
| 344 | LY_ERR rc; |
| 345 | const char *in = (*input); |
| 346 | const char *id; |
| 347 | const char *endtag; |
| 348 | |
| 349 | id = in; |
| 350 | rc = lyxml_check_qname(context, &in, term_char, endtag_len); |
| 351 | LY_CHECK_RET(rc); |
| 352 | if (*term_char == ':') { |
| 353 | /* we have prefixed identifier */ |
| 354 | endtag = in - *endtag_len; |
| 355 | |
| 356 | rc = lyxml_check_qname(context, &in, term_char, endtag_len); |
| 357 | LY_CHECK_RET(rc); |
| 358 | |
| 359 | (*prefix) = id; |
| 360 | (*prefix_len) = endtag - id; |
| 361 | id = endtag + 1; |
| 362 | } |
| 363 | if (!is_xmlws(*term_char) && *term_char != '/' && *term_char != '>') { |
| 364 | (*input) = in - *endtag_len; |
| 365 | LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(*input), *input, |
| 366 | "whitespace or element tag termination ('>' or '/>'"); |
| 367 | return LY_EVALID; |
| 368 | } |
| 369 | (*name) = id; |
| 370 | (*name_len) = in - *endtag_len - id; |
| 371 | |
| 372 | if (is_xmlws(*term_char)) { |
| 373 | /* go to the next meaningful input */ |
| 374 | ign_xmlws(context, in); |
| 375 | LY_CHECK_ERR_RET(!in[0], LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID); |
| 376 | *term_char = in[0]; |
| 377 | ++in; |
| 378 | *endtag_len = 1; |
| 379 | } |
| 380 | |
| 381 | (*input) = in; |
| 382 | return LY_SUCCESS; |
| 383 | } |
| 384 | |
| 385 | LY_ERR |
| 386 | lyxml_get_element(struct lyxml_context *context, const char **input, |
| 387 | const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 388 | { |
| 389 | struct ly_ctx *ctx = context->ctx; /* shortcut */ |
| 390 | const char *in = (*input); |
| 391 | size_t endtag_len; |
| 392 | bool loop = true; |
| 393 | int closing = 0; |
| 394 | unsigned int c; |
| 395 | LY_ERR rc; |
| 396 | struct lyxml_elem *e; |
| 397 | |
| 398 | /* initialize output variables */ |
| 399 | (*prefix) = (*name) = NULL; |
| 400 | (*prefix_len) = (*name_len) = 0; |
| 401 | |
| 402 | while (loop) { |
| 403 | rc = lyxml_parse_element_start(context, &in, &closing); |
| 404 | if (rc) { |
| 405 | return rc; |
| 406 | } else if (context->status == LYXML_END) { |
| 407 | goto success; |
| 408 | } |
| 409 | /* we are at the begining of the element name, remember the identifier start before checking its format */ |
| 410 | LY_CHECK_RET(rc = lyxml_parse_element_name(context, &in, &endtag_len, &c, prefix, prefix_len, name, name_len)); |
| 411 | |
| 412 | if (closing) { |
| 413 | /* match opening and closing element tags */ |
| 414 | LY_CHECK_ERR_RET( |
| 415 | !context->elements.count, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 416 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, |
| 417 | "Opening and closing elements tag missmatch (\"%.*s\").", *name_len, *name), |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 418 | LY_EVALID); |
| 419 | e = (struct lyxml_elem*)context->elements.objs[context->elements.count - 1]; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 420 | if (e->prefix_len != *prefix_len || e->name_len != *name_len |
| 421 | || (*prefix_len && strncmp(*prefix, e->prefix, e->prefix_len)) || strncmp(*name, e->name, e->name_len)) { |
| 422 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, |
| 423 | "Opening and closing elements tag missmatch (\"%.*s\").", *name_len, *name); |
| 424 | return LY_EVALID; |
| 425 | } |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 426 | /* opening and closing element tags matches, remove record from the opening tags list */ |
| 427 | free(e); |
| 428 | --context->elements.count; |
| 429 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 430 | /* remove also the namespaces connected with the element */ |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 431 | lyxml_ns_rm(context); |
| 432 | |
| 433 | /* do not return element information to announce closing element being currently processed */ |
| 434 | *name = *prefix = NULL; |
| 435 | *name_len = *prefix_len = 0; |
| 436 | |
| 437 | if (c == '>') { |
| 438 | /* end of closing element */ |
| 439 | context->status = LYXML_ELEMENT; |
| 440 | } else { |
| 441 | in -= endtag_len; |
| 442 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Unexpected data \"%.*s\" in closing element tag.", |
| 443 | LY_VCODE_INSTREXP_len(in), in); |
| 444 | return LY_EVALID; |
| 445 | } |
| 446 | } else { |
| 447 | if (c == '>') { |
| 448 | /* end of opening element */ |
| 449 | context->status = LYXML_ELEM_CONTENT; |
| 450 | } else if (c == '/' && in[0] == '>') { |
| 451 | /* empty element closing */ |
| 452 | context->status = LYXML_ELEMENT; |
| 453 | ++in; |
| 454 | } else { |
| 455 | /* attribute */ |
| 456 | context->status = LYXML_ATTRIBUTE; |
| 457 | in -= endtag_len; |
| 458 | } |
| 459 | |
| 460 | if (context->status != LYXML_ELEMENT) { |
| 461 | /* store element opening tag information */ |
| 462 | e = malloc(sizeof *e); |
| 463 | LY_CHECK_ERR_RET(!e, LOGMEM(ctx), LY_EMEM); |
| 464 | e->name = *name; |
| 465 | e->prefix = *prefix; |
| 466 | e->name_len = *name_len; |
| 467 | e->prefix_len = *prefix_len; |
| 468 | ly_set_add(&context->elements, e, LY_SET_OPT_USEASLIST); |
| 469 | } |
| 470 | } |
| 471 | loop = false; |
| 472 | } |
| 473 | |
| 474 | success: |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 475 | /* check for end of input */ |
| 476 | if (in[0] == '\0') { |
| 477 | /* EOF */ |
| 478 | if (context->elements.count) { |
| 479 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF); |
| 480 | return LY_EVALID; |
| 481 | } |
| 482 | context->status = LYXML_END; |
| 483 | } |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 484 | /* move caller's input */ |
| 485 | (*input) = in; |
| 486 | return LY_SUCCESS; |
| 487 | } |
| 488 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 489 | LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 490 | lyxml_skip_element(struct lyxml_context *context, const char **input) |
| 491 | { |
| 492 | LY_ERR ret; |
| 493 | unsigned int parents_count = context->elements.count; |
| 494 | |
| 495 | while (context->elements.count >= parents_count) { |
| 496 | /* skip attributes */ |
| 497 | while (context->status == LYXML_ATTRIBUTE) { |
| 498 | LY_CHECK_RET(lyxml_get_attribute(context, input, NULL, NULL, NULL, NULL)); |
| 499 | } |
| 500 | |
| 501 | /* skip content */ |
| 502 | if (context->status == LYXML_ELEM_CONTENT) { |
| 503 | ret = lyxml_get_string(context, input, NULL, NULL, NULL, NULL, NULL); |
| 504 | if (ret && (ret != LY_EINVAL)) { |
| 505 | return ret; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | if (context->status != LYXML_ELEMENT) { |
| 510 | LOGINT(context->ctx); |
| 511 | return LY_EINT; |
| 512 | } |
| 513 | |
| 514 | /* nested element/closing element */ |
| 515 | LY_CHECK_RET(lyxml_get_element(context, input, NULL, NULL, NULL, NULL)); |
| 516 | } |
| 517 | |
| 518 | return LY_SUCCESS; |
| 519 | } |
| 520 | |
| 521 | LY_ERR |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 522 | lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size, char **output, |
| 523 | size_t *length, int *dynamic) |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 524 | { |
| 525 | #define BUFSIZE 4096 |
| 526 | #define BUFSIZE_STEP 4096 |
| 527 | #define BUFSIZE_CHECK(CTX, BUF, SIZE, CURR, NEED) \ |
| 528 | if (CURR+NEED >= SIZE) { \ |
| 529 | BUF = ly_realloc(BUF, SIZE + BUFSIZE_STEP); \ |
| 530 | LY_CHECK_ERR_RET(!BUF, LOGMEM(CTX), LY_EMEM); \ |
| 531 | SIZE += BUFSIZE_STEP; \ |
| 532 | } |
| 533 | |
| 534 | struct ly_ctx *ctx = context->ctx; /* shortcut */ |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 535 | const char *in = (*input), *start; |
| 536 | char *buf = NULL, delim; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 537 | size_t offset; /* read offset in input buffer */ |
| 538 | size_t len; /* length of the output string (write offset in output buffer) */ |
| 539 | size_t size = 0; /* size of the output buffer */ |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 540 | void *p; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 541 | uint32_t n; |
| 542 | size_t u, newlines; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 543 | bool empty_content = false; |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 544 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 545 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 546 | assert(context); |
| 547 | assert(context->status == LYXML_ELEM_CONTENT || context->status == LYXML_ATTR_CONTENT); |
| 548 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 549 | if (in[0] == '\'') { |
| 550 | delim = '\''; |
| 551 | ++in; |
| 552 | } else if (in[0] == '"') { |
| 553 | delim = '"'; |
| 554 | ++in; |
| 555 | } else { |
| 556 | delim = '<'; |
| 557 | empty_content = true; |
| 558 | } |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 559 | start = in; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 560 | |
| 561 | if (empty_content) { |
| 562 | /* only when processing element's content - try to ignore whitespaces used to format XML data |
| 563 | * before element's child or closing tag */ |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 564 | for (offset = newlines = 0; in[offset] && is_xmlws(in[offset]); ++offset) { |
| 565 | if (in[offset] == '\n') { |
| 566 | ++newlines; |
| 567 | } |
| 568 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 569 | LY_CHECK_ERR_RET(!in[offset], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 570 | context->line += newlines; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 571 | if (in[offset] == '<') { |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 572 | (*input) = in + offset; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 573 | |
| 574 | /* get know if it is child element (indentation) or closing element (whitespace-only content) */ |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 575 | len = offset; |
| 576 | offset = 0; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 577 | in = *input; |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 578 | goto element_endtag_check; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 579 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 580 | } |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 581 | /* init */ |
| 582 | offset = len = 0; |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 583 | empty_content = false; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 584 | |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 585 | if (0) { |
| 586 | getbuffer: |
| 587 | /* prepare output buffer */ |
| 588 | if (*buffer) { |
| 589 | buf = *buffer; |
| 590 | size = *buffer_size; |
| 591 | } else { |
| 592 | buf = malloc(BUFSIZE); |
| 593 | size = BUFSIZE; |
| 594 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM); |
| 595 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 596 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 597 | |
| 598 | /* parse */ |
| 599 | while (in[offset]) { |
| 600 | if (in[offset] == '&') { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 601 | if (output) { |
| 602 | if (!buf) { |
| 603 | /* it is necessary to modify the input, so we will need a dynamically allocated buffer */ |
| 604 | goto getbuffer; |
| 605 | } |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 606 | |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 607 | if (offset) { |
| 608 | /* store what we have so far */ |
| 609 | BUFSIZE_CHECK(ctx, buf, size, len, offset); |
| 610 | memcpy(&buf[len], in, offset); |
| 611 | len += offset; |
| 612 | in += offset; |
| 613 | offset = 0; |
| 614 | } |
| 615 | /* process reference */ |
| 616 | /* we will need 4 bytes at most since we support only the predefined |
| 617 | * (one-char) entities and character references */ |
| 618 | BUFSIZE_CHECK(ctx, buf, size, len, 4); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 619 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 620 | ++offset; |
| 621 | if (in[offset] != '#') { |
| 622 | /* entity reference - only predefined references are supported */ |
| 623 | if (!strncmp(&in[offset], "lt;", 3)) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 624 | if (output) { |
| 625 | buf[len++] = '<'; |
| 626 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 627 | in += 4; /* < */ |
| 628 | } else if (!strncmp(&in[offset], "gt;", 3)) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 629 | if (output) { |
| 630 | buf[len++] = '>'; |
| 631 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 632 | in += 4; /* > */ |
| 633 | } else if (!strncmp(&in[offset], "amp;", 4)) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 634 | if (output) { |
| 635 | buf[len++] = '&'; |
| 636 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 637 | in += 5; /* & */ |
| 638 | } else if (!strncmp(&in[offset], "apos;", 5)) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 639 | if (output) { |
| 640 | buf[len++] = '\''; |
| 641 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 642 | in += 6; /* ' */ |
| 643 | } else if (!strncmp(&in[offset], "quot;", 5)) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 644 | if (output) { |
| 645 | buf[len++] = '\"'; |
| 646 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 647 | in += 6; /* " */ |
| 648 | } else { |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 649 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, |
| 650 | "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] | 651 | goto error; |
| 652 | } |
| 653 | offset = 0; |
| 654 | } else { |
| 655 | p = (void*)&in[offset - 1]; |
| 656 | /* character reference */ |
| 657 | ++offset; |
| 658 | if (isdigit(in[offset])) { |
| 659 | for (n = 0; isdigit(in[offset]); offset++) { |
| 660 | n = (10 * n) + (in[offset] - '0'); |
| 661 | } |
| 662 | } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) { |
| 663 | for (n = 0, ++offset; isxdigit(in[offset]); offset++) { |
| 664 | if (isdigit(in[offset])) { |
| 665 | u = (in[offset] - '0'); |
| 666 | } else if (in[offset] > 'F') { |
| 667 | u = 10 + (in[offset] - 'a'); |
| 668 | } else { |
| 669 | u = 10 + (in[offset] - 'A'); |
| 670 | } |
| 671 | n = (16 * n) + u; |
| 672 | } |
| 673 | } else { |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 674 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 675 | goto error; |
| 676 | |
| 677 | } |
| 678 | LY_CHECK_ERR_GOTO(in[offset] != ';', |
| 679 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, |
| 680 | LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"), |
| 681 | error); |
| 682 | ++offset; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 683 | if (output) { |
| 684 | rc = lyxml_pututf8(&buf[len], n, &u); |
| 685 | } else { |
| 686 | char utf8[4]; |
| 687 | rc = lyxml_pututf8(&utf8[0], n, &u); |
| 688 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 689 | LY_CHECK_ERR_GOTO(rc, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 690 | "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n), |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 691 | error); |
| 692 | len += u; |
| 693 | in += offset; |
| 694 | offset = 0; |
| 695 | } |
| 696 | } else if (in[offset] == delim) { |
| 697 | /* end of string */ |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 698 | if (buf) { |
| 699 | if (len + offset >= size) { |
| 700 | buf = ly_realloc(buf, len + offset + 1); |
| 701 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM); |
| 702 | size = len + offset + 1; |
| 703 | } |
| 704 | memcpy(&buf[len], in, offset); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 705 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 706 | len += offset; |
| 707 | /* in case of element content, keep the leading <, |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 708 | * for attribute's value move after the terminating quotation mark */ |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 709 | element_endtag_check: |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 710 | if (context->status == LYXML_ELEM_CONTENT) { |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 711 | const char *name = NULL, *prefix = NULL; |
| 712 | size_t name_len = 0, prefix_len = 0; |
| 713 | int closing = 0; |
| 714 | /* use fake context to preserve real context (lines, status) since we don't want really parse the element tag here */ |
| 715 | struct lyxml_context fakecontext = {.ctx = context->ctx, .line = context->line, .status = context->status}; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 716 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 717 | in += offset; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 718 | |
| 719 | /* get know if it is child element (mixed content) or closing element (regular content) */ |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 720 | /* We don't want actually to parse the closing element, we just need to check mixed content. |
| 721 | * The closing element tag is preserved to keep the context for the data (returned string), |
| 722 | * since it can contain data using XML prefixes defined in this element and the caller can |
| 723 | * want to work with it */ |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 724 | (*input) = in; |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 725 | rc = lyxml_parse_element_start(&fakecontext, &in, &closing); |
| 726 | if (rc) { |
Radek Krejci | 8ced2f7 | 2019-05-20 12:33:49 +0200 | [diff] [blame] | 727 | /* some parsing error */ |
| 728 | goto error; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 729 | } else { |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 730 | size_t endtag_len; |
| 731 | unsigned int c; |
| 732 | struct lyxml_elem *e; |
| 733 | |
| 734 | LY_CHECK_GOTO(lyxml_parse_element_name(&fakecontext, &in, &endtag_len, &c, &prefix, &prefix_len, &name, &name_len), error); |
| 735 | |
| 736 | if (!closing) { |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 737 | if (empty_content) { |
| 738 | /* the element here is not closing element, so we have the just indentation formatting before the child */ |
| 739 | context->status = LYXML_ELEMENT; |
| 740 | return LY_EINVAL; |
| 741 | } else { |
| 742 | /* the element here is not closing element, so we have not allowed mixed content */ |
| 743 | struct lyxml_elem *e = (struct lyxml_elem*)context->elements.objs[--context->elements.count]; |
| 744 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Mixed XML content is not allowed (%.*s).", |
| 745 | offset + (in - (*input)), &(*input)[-offset]); |
| 746 | free(e); |
| 747 | goto error; |
| 748 | } |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /* closing element start - check the name if it matches the opening element tag */ |
| 752 | LY_CHECK_ERR_GOTO(!context->elements.count, |
| 753 | LOGVAL(ctx, LY_VLOG_LINE, &fakecontext.line, LYVE_SYNTAX, "Opening and closing elements tag missmatch (\"%.*s\").", |
| 754 | name_len, name), |
| 755 | error); |
| 756 | e = (struct lyxml_elem*)context->elements.objs[context->elements.count - 1]; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 757 | if (e->prefix_len != prefix_len || e->name_len != name_len |
| 758 | || (prefix_len && strncmp(prefix, e->prefix, e->prefix_len)) || strncmp(name, e->name, e->name_len)) { |
| 759 | LOGVAL(ctx, LY_VLOG_LINE, &fakecontext.line, LYVE_SYNTAX, |
| 760 | "Opening and closing elements tag missmatch (\"%.*s\", expected \"%.*s\").", |
| 761 | name_len, name, e->name_len, e->name); |
| 762 | free(e); |
| 763 | --context->elements.count; |
| 764 | goto error; |
| 765 | } |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 766 | /* opening and closing element tags matches */ |
| 767 | /* return input back */ |
| 768 | in = (*input); |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 769 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 770 | } else { |
| 771 | in += offset + 1; |
| 772 | } |
| 773 | goto success; |
| 774 | } else { |
| 775 | /* log lines */ |
| 776 | if (in[offset] == '\n') { |
| 777 | ++context->line; |
| 778 | } |
| 779 | |
| 780 | /* continue */ |
| 781 | ++offset; |
| 782 | } |
| 783 | } |
| 784 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF); |
| 785 | error: |
| 786 | if (!(*buffer)) { |
Radek Krejci | bb9b198 | 2019-04-08 14:24:59 +0200 | [diff] [blame] | 787 | /* buffer not provided, buf is local */ |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 788 | free(buf); |
Radek Krejci | bb9b198 | 2019-04-08 14:24:59 +0200 | [diff] [blame] | 789 | } else if (buf) { |
| 790 | /* buf is shared with caller via buffer, but buf could be reallocated, so update the provided buffer */ |
| 791 | (*buffer) = buf; |
| 792 | (*buffer_size) = size; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 793 | } |
| 794 | return LY_EVALID; |
| 795 | |
| 796 | success: |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 797 | if (buf) { |
| 798 | if (!(*buffer) && size != len + 1) { |
| 799 | /* not using provided buffer, so fit the allocated buffer to what we really have inside */ |
| 800 | p = realloc(buf, len + 1); |
| 801 | /* ignore realloc fail because we are reducing the buffer, |
| 802 | * so just return bigger buffer than needed */ |
| 803 | if (p) { |
| 804 | size = len + 1; |
| 805 | buf = p; |
| 806 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 807 | } |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 808 | /* set terminating NULL byte */ |
| 809 | buf[len] = '\0'; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 810 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 811 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 812 | context->status -= 1; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 813 | if (buf) { |
| 814 | (*buffer) = buf; |
| 815 | (*buffer_size) = size; |
| 816 | (*output) = buf; |
| 817 | (*dynamic) = 1; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 818 | (*length) = len; |
| 819 | } else if (output) { |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 820 | (*output) = (char*)start; |
| 821 | (*dynamic) = 0; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 822 | (*length) = len; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 823 | } |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 824 | |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 825 | if (context->status == LYXML_ATTRIBUTE) { |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 826 | /* skip whitespaces after the value */ |
| 827 | ign_xmlws(context, in); |
| 828 | |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 829 | if (in[0] == '>') { |
| 830 | /* element terminated by > - termination of the opening tag */ |
| 831 | context->status = LYXML_ELEM_CONTENT; |
| 832 | ++in; |
| 833 | } else if (in[0] == '/' && in[1] == '>') { |
| 834 | /* element terminated by /> - termination of an empty element */ |
| 835 | context->status = LYXML_ELEMENT; |
| 836 | in += 2; |
| 837 | |
| 838 | /* remove the closed element record from the tags list */ |
| 839 | free(context->elements.objs[context->elements.count - 1]); |
| 840 | --context->elements.count; |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 841 | |
| 842 | /* remove also the namespaces conneted with the element */ |
Radek Krejci | 17dca99 | 2019-05-17 10:53:27 +0200 | [diff] [blame] | 843 | lyxml_ns_rm(context); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 844 | |
| 845 | if (!context->elements.count && in[0] == '\0') { |
| 846 | /* EOF */ |
| 847 | context->status = LYXML_END; |
| 848 | } |
| 849 | } /* else another attribute */ |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | (*input) = in; |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 853 | return rc; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 854 | |
| 855 | #undef BUFSIZE |
| 856 | #undef BUFSIZE_STEP |
| 857 | #undef BUFSIZE_CHECK |
| 858 | } |
| 859 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 860 | LY_ERR |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 861 | lyxml_get_attribute(struct lyxml_context *context, const char **input, |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 862 | const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 863 | { |
| 864 | struct ly_ctx *ctx = context->ctx; /* shortcut */ |
| 865 | const char *in = (*input); |
| 866 | const char *id; |
| 867 | const char *endtag; |
| 868 | LY_ERR rc; |
| 869 | unsigned int c; |
| 870 | size_t endtag_len; |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 871 | int is_ns = 0; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 872 | const char *ns_prefix; |
| 873 | size_t ns_prefix_len; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 874 | |
| 875 | /* initialize output variables */ |
| 876 | (*prefix) = (*name) = NULL; |
| 877 | (*prefix_len) = (*name_len) = 0; |
| 878 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 879 | do { |
| 880 | /* skip initial whitespaces */ |
| 881 | ign_xmlws(context, in); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 882 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 883 | if (in[0] == '\0') { |
| 884 | /* EOF - not expected at this place */ |
| 885 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF); |
| 886 | return LY_EVALID; |
| 887 | } |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 888 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 889 | /* remember the identifier start before checking its format */ |
| 890 | id = in; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 891 | rc = lyxml_check_qname(context, &in, &c, &endtag_len); |
| 892 | LY_CHECK_RET(rc); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 893 | if (c == ':') { |
| 894 | /* we have prefixed identifier */ |
| 895 | endtag = in - endtag_len; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 896 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 897 | rc = lyxml_check_qname(context, &in, &c, &endtag_len); |
| 898 | LY_CHECK_RET(rc); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 899 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 900 | (*prefix) = id; |
| 901 | (*prefix_len) = endtag - id; |
| 902 | id = endtag + 1; |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 903 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 904 | if (!is_xmlws(c) && c != '=') { |
| 905 | in = in - endtag_len; |
| 906 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "whitespace or '='"); |
| 907 | return LY_EVALID; |
| 908 | } |
| 909 | in = in - endtag_len; |
| 910 | (*name) = id; |
| 911 | (*name_len) = in - id; |
| 912 | |
| 913 | /* eat '=' and stop at the value beginning */ |
| 914 | ign_xmlws(context, in); |
| 915 | if (in[0] != '=') { |
| 916 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "'='"); |
| 917 | return LY_EVALID; |
| 918 | } |
| 919 | ++in; |
| 920 | ign_xmlws(context, in); |
| 921 | if (in[0] != '\'' && in[0] != '"') { |
| 922 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, |
| 923 | LY_VCODE_INSTREXP_len(in), in, "either single or double quotation mark"); |
| 924 | return LY_EVALID; |
| 925 | } |
| 926 | context->status = LYXML_ATTR_CONTENT; |
| 927 | |
| 928 | is_ns = 0; |
| 929 | if (*prefix && *prefix_len == 5 && !strncmp(*prefix, "xmlns", 5)) { |
| 930 | is_ns = 1; |
| 931 | ns_prefix = *name; |
| 932 | ns_prefix_len = *name_len; |
| 933 | } else if (*name_len == 5 && !strncmp(*name, "xmlns", 5)) { |
| 934 | is_ns = 1; |
| 935 | ns_prefix = NULL; |
| 936 | ns_prefix_len = 0; |
| 937 | } |
| 938 | if (is_ns) { |
| 939 | /* instead of attribute, we have namespace specification, |
| 940 | * so process it automatically and then move to another attribute (if any) */ |
| 941 | char *value = NULL; |
| 942 | size_t value_len = 0; |
| 943 | int dynamic = 0; |
| 944 | |
| 945 | LY_CHECK_RET(lyxml_get_string(context, &in, &value, &value_len, &value, &value_len, &dynamic)); |
| 946 | if ((rc = lyxml_ns_add(context, ns_prefix, ns_prefix_len, dynamic ? value : strndup(value, value_len)))) { |
| 947 | if (dynamic) { |
| 948 | free(value); |
| 949 | return rc; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /* do not return ns */ |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 954 | (*prefix) = (*name) = NULL; |
| 955 | (*prefix_len) = (*name_len) = 0; |
| 956 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 957 | } while (is_ns && (context->status == LYXML_ATTRIBUTE)); |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 958 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 959 | /* move caller's input */ |
| 960 | (*input) = in; |
| 961 | return LY_SUCCESS; |
| 962 | } |
| 963 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 964 | void |
| 965 | lyxml_context_clear(struct lyxml_context *context) |
| 966 | { |
| 967 | unsigned int u; |
| 968 | |
| 969 | ly_set_erase(&context->elements, free); |
| 970 | for (u = context->ns.count - 1; u + 1 > 0; --u) { |
| 971 | /* remove the ns structure */ |
| 972 | free(((struct lyxml_ns *)context->ns.objs[u])->prefix); |
| 973 | free(((struct lyxml_ns *)context->ns.objs[u])->uri); |
| 974 | free(context->ns.objs[u]); |
| 975 | } |
| 976 | ly_set_erase(&context->ns, NULL); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 977 | context->status = 0; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 978 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 979 | |
| 980 | LY_ERR |
| 981 | lyxml_dump_text(struct lyout *out, const char *text, int attribute) |
| 982 | { |
| 983 | LY_ERR ret = LY_SUCCESS; |
| 984 | unsigned int u; |
| 985 | |
| 986 | if (!text) { |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | for (u = 0; text[u]; u++) { |
| 991 | switch (text[u]) { |
| 992 | case '&': |
| 993 | ret = ly_print(out, "&"); |
| 994 | break; |
| 995 | case '<': |
| 996 | ret = ly_print(out, "<"); |
| 997 | break; |
| 998 | case '>': |
| 999 | /* not needed, just for readability */ |
| 1000 | ret = ly_print(out, ">"); |
| 1001 | break; |
| 1002 | case '"': |
| 1003 | if (attribute) { |
| 1004 | ret = ly_print(out, """); |
| 1005 | break; |
| 1006 | } |
| 1007 | /* falls through */ |
| 1008 | default: |
| 1009 | ly_write(out, &text[u], 1); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1016 | LY_ERR |
| 1017 | lyxml_get_prefixes(struct lyxml_context *ctx, const char *value, size_t value_len, struct ly_prefix **val_prefs) |
| 1018 | { |
| 1019 | LY_ERR ret; |
| 1020 | uint32_t u, c; |
| 1021 | const struct lyxml_ns *ns; |
| 1022 | const char *start, *stop; |
| 1023 | struct ly_prefix *prefixes = NULL; |
| 1024 | size_t len; |
| 1025 | |
| 1026 | for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) { |
| 1027 | size_t bytes; |
| 1028 | ly_getutf8(&stop, &c, &bytes); |
| 1029 | if (is_xmlqnamestartchar(c)) { |
| 1030 | for (ly_getutf8(&stop, &c, &bytes); |
| 1031 | is_xmlqnamechar(c) && (size_t)(stop - value) < value_len; |
| 1032 | ly_getutf8(&stop, &c, &bytes)); |
| 1033 | stop = stop - bytes; |
| 1034 | if (*stop == ':') { |
| 1035 | /* we have a possible prefix */ |
| 1036 | len = stop - start; |
| 1037 | ns = lyxml_ns_get(ctx, start, len); |
| 1038 | if (ns) { |
| 1039 | struct ly_prefix *p = NULL; |
| 1040 | |
| 1041 | /* check whether we do not already have this prefix stored */ |
| 1042 | LY_ARRAY_FOR(prefixes, u) { |
| 1043 | if (!ly_strncmp(prefixes[u].pref, start, len)) { |
| 1044 | p = &prefixes[u]; |
| 1045 | break; |
| 1046 | } |
| 1047 | } |
| 1048 | if (!p) { |
| 1049 | LY_ARRAY_NEW_GOTO(ctx->ctx, prefixes, p, ret, error); |
| 1050 | p->pref = lydict_insert(ctx->ctx, start, len); |
| 1051 | p->ns = lydict_insert(ctx->ctx, ns->uri, 0); |
| 1052 | } /* else the prefix already present */ |
| 1053 | } |
| 1054 | } |
| 1055 | stop = stop + bytes; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | *val_prefs = prefixes; |
| 1060 | return LY_SUCCESS; |
| 1061 | |
| 1062 | error: |
| 1063 | LY_ARRAY_FOR(prefixes, u) { |
| 1064 | lydict_remove(ctx->ctx, prefixes[u].pref); |
| 1065 | } |
| 1066 | LY_ARRAY_FREE(prefixes); |
| 1067 | return ret; |
| 1068 | } |
| 1069 | |
| 1070 | LY_ERR |
| 1071 | lyxml_value_compare(const char *value1, const struct ly_prefix *prefs1, const char *value2, const struct ly_prefix *prefs2) |
| 1072 | { |
| 1073 | const char *ptr1, *ptr2, *ns1, *ns2; |
| 1074 | uint32_t u1, u2; |
| 1075 | int len; |
| 1076 | |
| 1077 | if (!value1 && !value2) { |
| 1078 | return LY_SUCCESS; |
| 1079 | } |
| 1080 | if ((value1 && !value2) || (!value1 && value2)) { |
| 1081 | return LY_ENOT; |
| 1082 | } |
| 1083 | |
| 1084 | ptr1 = value1; |
| 1085 | ptr2 = value2; |
| 1086 | while (ptr1[0] && ptr2[0]) { |
| 1087 | if (ptr1[0] != ptr2[0]) { |
| 1088 | /* it can be a start of prefix that maps to the same module */ |
| 1089 | ns1 = ns2 = NULL; |
| 1090 | if (prefs1) { |
| 1091 | /* find module of the first prefix, if any */ |
| 1092 | LY_ARRAY_FOR(prefs1, u1) { |
| 1093 | len = strlen(prefs1[u1].pref); |
| 1094 | if (!strncmp(ptr1, prefs1[u1].pref, len) && (ptr1[len] == ':')) { |
| 1095 | ns1 = prefs1[u1].ns; |
| 1096 | break; |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | if (prefs2) { |
| 1101 | /* find module of the second prefix, if any */ |
| 1102 | LY_ARRAY_FOR(prefs2, u2) { |
| 1103 | len = strlen(prefs2[u2].pref); |
| 1104 | if (!strncmp(ptr2, prefs2[u2].pref, len) && (ptr2[len] == ':')) { |
| 1105 | ns2 = prefs2[u2].ns; |
| 1106 | break; |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | if (!ns1 || !ns2 || (ns1 != ns2)) { |
| 1112 | /* not a prefix or maps to different namespaces */ |
| 1113 | break; |
| 1114 | } |
| 1115 | |
| 1116 | /* skip prefixes in both values (':' is skipped as iter) */ |
| 1117 | ptr1 += strlen(prefs1[u1].pref); |
| 1118 | ptr2 += strlen(prefs2[u2].pref); |
| 1119 | } |
| 1120 | |
| 1121 | ++ptr1; |
| 1122 | ++ptr2; |
| 1123 | } |
| 1124 | if (ptr1[0] || ptr2[0]) { |
| 1125 | /* not a match or simply different lengths */ |
| 1126 | return LY_ENOT; |
| 1127 | } |
| 1128 | |
| 1129 | return LY_SUCCESS; |
| 1130 | } |