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, |
| 416 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Opening and closing elements tag missmatch (\"%.*s\").", *name_len, *name), |
| 417 | LY_EVALID); |
| 418 | e = (struct lyxml_elem*)context->elements.objs[context->elements.count - 1]; |
| 419 | LY_CHECK_ERR_RET(e->prefix_len != *prefix_len || e->name_len != *name_len |
| 420 | || (*prefix_len && strncmp(*prefix, e->prefix, e->prefix_len)) || strncmp(*name, e->name, e->name_len), |
| 421 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Opening and closing elements tag missmatch (\"%.*s\").", *name_len, *name), |
| 422 | LY_EVALID); |
| 423 | /* opening and closing element tags matches, remove record from the opening tags list */ |
| 424 | free(e); |
| 425 | --context->elements.count; |
| 426 | |
| 427 | /* remove also the namespaces conneted with the element */ |
| 428 | lyxml_ns_rm(context); |
| 429 | |
| 430 | /* do not return element information to announce closing element being currently processed */ |
| 431 | *name = *prefix = NULL; |
| 432 | *name_len = *prefix_len = 0; |
| 433 | |
| 434 | if (c == '>') { |
| 435 | /* end of closing element */ |
| 436 | context->status = LYXML_ELEMENT; |
| 437 | } else { |
| 438 | in -= endtag_len; |
| 439 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Unexpected data \"%.*s\" in closing element tag.", |
| 440 | LY_VCODE_INSTREXP_len(in), in); |
| 441 | return LY_EVALID; |
| 442 | } |
| 443 | } else { |
| 444 | if (c == '>') { |
| 445 | /* end of opening element */ |
| 446 | context->status = LYXML_ELEM_CONTENT; |
| 447 | } else if (c == '/' && in[0] == '>') { |
| 448 | /* empty element closing */ |
| 449 | context->status = LYXML_ELEMENT; |
| 450 | ++in; |
| 451 | } else { |
| 452 | /* attribute */ |
| 453 | context->status = LYXML_ATTRIBUTE; |
| 454 | in -= endtag_len; |
| 455 | } |
| 456 | |
| 457 | if (context->status != LYXML_ELEMENT) { |
| 458 | /* store element opening tag information */ |
| 459 | e = malloc(sizeof *e); |
| 460 | LY_CHECK_ERR_RET(!e, LOGMEM(ctx), LY_EMEM); |
| 461 | e->name = *name; |
| 462 | e->prefix = *prefix; |
| 463 | e->name_len = *name_len; |
| 464 | e->prefix_len = *prefix_len; |
| 465 | ly_set_add(&context->elements, e, LY_SET_OPT_USEASLIST); |
| 466 | } |
| 467 | } |
| 468 | loop = false; |
| 469 | } |
| 470 | |
| 471 | success: |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 472 | /* check for end of input */ |
| 473 | if (in[0] == '\0') { |
| 474 | /* EOF */ |
| 475 | if (context->elements.count) { |
| 476 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF); |
| 477 | return LY_EVALID; |
| 478 | } |
| 479 | context->status = LYXML_END; |
| 480 | } |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 481 | /* move caller's input */ |
| 482 | (*input) = in; |
| 483 | return LY_SUCCESS; |
| 484 | } |
| 485 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 486 | LY_ERR |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 487 | lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size, char **output, size_t *length, int *dynamic) |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 488 | { |
| 489 | #define BUFSIZE 4096 |
| 490 | #define BUFSIZE_STEP 4096 |
| 491 | #define BUFSIZE_CHECK(CTX, BUF, SIZE, CURR, NEED) \ |
| 492 | if (CURR+NEED >= SIZE) { \ |
| 493 | BUF = ly_realloc(BUF, SIZE + BUFSIZE_STEP); \ |
| 494 | LY_CHECK_ERR_RET(!BUF, LOGMEM(CTX), LY_EMEM); \ |
| 495 | SIZE += BUFSIZE_STEP; \ |
| 496 | } |
| 497 | |
| 498 | struct ly_ctx *ctx = context->ctx; /* shortcut */ |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 499 | const char *in = (*input), *start; |
| 500 | char *buf = NULL, delim; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 501 | size_t offset; /* read offset in input buffer */ |
| 502 | size_t len; /* length of the output string (write offset in output buffer) */ |
| 503 | size_t size = 0; /* size of the output buffer */ |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 504 | void *p; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 505 | uint32_t n; |
| 506 | size_t u, newlines; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 507 | bool empty_content = false; |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 508 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 509 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 510 | assert(context); |
| 511 | assert(context->status == LYXML_ELEM_CONTENT || context->status == LYXML_ATTR_CONTENT); |
| 512 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 513 | if (in[0] == '\'') { |
| 514 | delim = '\''; |
| 515 | ++in; |
| 516 | } else if (in[0] == '"') { |
| 517 | delim = '"'; |
| 518 | ++in; |
| 519 | } else { |
| 520 | delim = '<'; |
| 521 | empty_content = true; |
| 522 | } |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 523 | start = in; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 524 | |
| 525 | if (empty_content) { |
| 526 | /* only when processing element's content - try to ignore whitespaces used to format XML data |
| 527 | * before element's child or closing tag */ |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 528 | for (offset = newlines = 0; in[offset] && is_xmlws(in[offset]); ++offset) { |
| 529 | if (in[offset] == '\n') { |
| 530 | ++newlines; |
| 531 | } |
| 532 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 533 | 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] | 534 | context->line += newlines; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 535 | if (in[offset] == '<') { |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 536 | (*input) = in + offset; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 537 | |
| 538 | /* 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] | 539 | len = offset; |
| 540 | offset = 0; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 541 | in = *input; |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 542 | goto element_endtag_check; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 543 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 544 | } |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 545 | /* init */ |
| 546 | offset = len = 0; |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 547 | empty_content = false; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 548 | |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 549 | if (0) { |
| 550 | getbuffer: |
| 551 | /* prepare output buffer */ |
| 552 | if (*buffer) { |
| 553 | buf = *buffer; |
| 554 | size = *buffer_size; |
| 555 | } else { |
| 556 | buf = malloc(BUFSIZE); |
| 557 | size = BUFSIZE; |
| 558 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM); |
| 559 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 560 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 561 | |
| 562 | /* parse */ |
| 563 | while (in[offset]) { |
| 564 | if (in[offset] == '&') { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 565 | if (output) { |
| 566 | if (!buf) { |
| 567 | /* it is necessary to modify the input, so we will need a dynamically allocated buffer */ |
| 568 | goto getbuffer; |
| 569 | } |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 570 | |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 571 | if (offset) { |
| 572 | /* store what we have so far */ |
| 573 | BUFSIZE_CHECK(ctx, buf, size, len, offset); |
| 574 | memcpy(&buf[len], in, offset); |
| 575 | len += offset; |
| 576 | in += offset; |
| 577 | offset = 0; |
| 578 | } |
| 579 | /* process reference */ |
| 580 | /* we will need 4 bytes at most since we support only the predefined |
| 581 | * (one-char) entities and character references */ |
| 582 | BUFSIZE_CHECK(ctx, buf, size, len, 4); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 583 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 584 | ++offset; |
| 585 | if (in[offset] != '#') { |
| 586 | /* entity reference - only predefined references are supported */ |
| 587 | if (!strncmp(&in[offset], "lt;", 3)) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 588 | if (output) { |
| 589 | buf[len++] = '<'; |
| 590 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 591 | in += 4; /* < */ |
| 592 | } else if (!strncmp(&in[offset], "gt;", 3)) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 593 | if (output) { |
| 594 | buf[len++] = '>'; |
| 595 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 596 | in += 4; /* > */ |
| 597 | } else if (!strncmp(&in[offset], "amp;", 4)) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 598 | if (output) { |
| 599 | buf[len++] = '&'; |
| 600 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 601 | in += 5; /* & */ |
| 602 | } else if (!strncmp(&in[offset], "apos;", 5)) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 603 | if (output) { |
| 604 | buf[len++] = '\''; |
| 605 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 606 | in += 6; /* ' */ |
| 607 | } else if (!strncmp(&in[offset], "quot;", 5)) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 608 | if (output) { |
| 609 | buf[len++] = '\"'; |
| 610 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 611 | in += 6; /* " */ |
| 612 | } else { |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 613 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, |
| 614 | "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] | 615 | goto error; |
| 616 | } |
| 617 | offset = 0; |
| 618 | } else { |
| 619 | p = (void*)&in[offset - 1]; |
| 620 | /* character reference */ |
| 621 | ++offset; |
| 622 | if (isdigit(in[offset])) { |
| 623 | for (n = 0; isdigit(in[offset]); offset++) { |
| 624 | n = (10 * n) + (in[offset] - '0'); |
| 625 | } |
| 626 | } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) { |
| 627 | for (n = 0, ++offset; isxdigit(in[offset]); offset++) { |
| 628 | if (isdigit(in[offset])) { |
| 629 | u = (in[offset] - '0'); |
| 630 | } else if (in[offset] > 'F') { |
| 631 | u = 10 + (in[offset] - 'a'); |
| 632 | } else { |
| 633 | u = 10 + (in[offset] - 'A'); |
| 634 | } |
| 635 | n = (16 * n) + u; |
| 636 | } |
| 637 | } else { |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 638 | 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] | 639 | goto error; |
| 640 | |
| 641 | } |
| 642 | LY_CHECK_ERR_GOTO(in[offset] != ';', |
| 643 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, |
| 644 | LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"), |
| 645 | error); |
| 646 | ++offset; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 647 | if (output) { |
| 648 | rc = lyxml_pututf8(&buf[len], n, &u); |
| 649 | } else { |
| 650 | char utf8[4]; |
| 651 | rc = lyxml_pututf8(&utf8[0], n, &u); |
| 652 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 653 | 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] | 654 | "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n), |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 655 | error); |
| 656 | len += u; |
| 657 | in += offset; |
| 658 | offset = 0; |
| 659 | } |
| 660 | } else if (in[offset] == delim) { |
| 661 | /* end of string */ |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 662 | if (buf) { |
| 663 | if (len + offset >= size) { |
| 664 | buf = ly_realloc(buf, len + offset + 1); |
| 665 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM); |
| 666 | size = len + offset + 1; |
| 667 | } |
| 668 | memcpy(&buf[len], in, offset); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 669 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 670 | len += offset; |
| 671 | /* in case of element content, keep the leading <, |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 672 | * for attribute's value move after the terminating quotation mark */ |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 673 | element_endtag_check: |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 674 | if (context->status == LYXML_ELEM_CONTENT) { |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 675 | const char *name = NULL, *prefix = NULL; |
| 676 | size_t name_len = 0, prefix_len = 0; |
| 677 | int closing = 0; |
| 678 | /* use fake context to preserve real context (lines, status) since we don't want really parse the element tag here */ |
| 679 | 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] | 680 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 681 | in += offset; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 682 | |
| 683 | /* 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] | 684 | /* We don't want actually to parse the closing element, we just need to check mixed content. |
| 685 | * The closing element tag is preserved to keep the context for the data (returned string), |
| 686 | * since it can contain data using XML prefixes defined in this element and the caller can |
| 687 | * want to work with it */ |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 688 | (*input) = in; |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 689 | rc = lyxml_parse_element_start(&fakecontext, &in, &closing); |
| 690 | if (rc) { |
Radek Krejci | 8ced2f7 | 2019-05-20 12:33:49 +0200 | [diff] [blame] | 691 | /* some parsing error */ |
| 692 | goto error; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 693 | } else { |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 694 | size_t endtag_len; |
| 695 | unsigned int c; |
| 696 | struct lyxml_elem *e; |
| 697 | |
| 698 | LY_CHECK_GOTO(lyxml_parse_element_name(&fakecontext, &in, &endtag_len, &c, &prefix, &prefix_len, &name, &name_len), error); |
| 699 | |
| 700 | if (!closing) { |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 701 | if (empty_content) { |
| 702 | /* the element here is not closing element, so we have the just indentation formatting before the child */ |
| 703 | context->status = LYXML_ELEMENT; |
| 704 | return LY_EINVAL; |
| 705 | } else { |
| 706 | /* the element here is not closing element, so we have not allowed mixed content */ |
| 707 | struct lyxml_elem *e = (struct lyxml_elem*)context->elements.objs[--context->elements.count]; |
| 708 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Mixed XML content is not allowed (%.*s).", |
| 709 | offset + (in - (*input)), &(*input)[-offset]); |
| 710 | free(e); |
| 711 | goto error; |
| 712 | } |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* closing element start - check the name if it matches the opening element tag */ |
| 716 | LY_CHECK_ERR_GOTO(!context->elements.count, |
| 717 | LOGVAL(ctx, LY_VLOG_LINE, &fakecontext.line, LYVE_SYNTAX, "Opening and closing elements tag missmatch (\"%.*s\").", |
| 718 | name_len, name), |
| 719 | error); |
| 720 | e = (struct lyxml_elem*)context->elements.objs[context->elements.count - 1]; |
| 721 | LY_CHECK_ERR_GOTO(e->prefix_len != prefix_len || e->name_len != name_len |
| 722 | || (prefix_len && strncmp(prefix, e->prefix, e->prefix_len)) || strncmp(name, e->name, e->name_len), |
David Sedlák | 54a6f13 | 2019-07-16 15:14:18 +0200 | [diff] [blame] | 723 | free(e); --context->elements.count; LOGVAL(ctx, LY_VLOG_LINE, &fakecontext.line, LYVE_SYNTAX, |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 724 | "Opening and closing elements tag missmatch (\"%.*s\").", name_len, name), |
| 725 | error); |
| 726 | /* opening and closing element tags matches */ |
| 727 | /* return input back */ |
| 728 | in = (*input); |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 729 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 730 | } else { |
| 731 | in += offset + 1; |
| 732 | } |
| 733 | goto success; |
| 734 | } else { |
| 735 | /* log lines */ |
| 736 | if (in[offset] == '\n') { |
| 737 | ++context->line; |
| 738 | } |
| 739 | |
| 740 | /* continue */ |
| 741 | ++offset; |
| 742 | } |
| 743 | } |
| 744 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF); |
| 745 | error: |
| 746 | if (!(*buffer)) { |
Radek Krejci | bb9b198 | 2019-04-08 14:24:59 +0200 | [diff] [blame] | 747 | /* buffer not provided, buf is local */ |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 748 | free(buf); |
Radek Krejci | bb9b198 | 2019-04-08 14:24:59 +0200 | [diff] [blame] | 749 | } else if (buf) { |
| 750 | /* buf is shared with caller via buffer, but buf could be reallocated, so update the provided buffer */ |
| 751 | (*buffer) = buf; |
| 752 | (*buffer_size) = size; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 753 | } |
| 754 | return LY_EVALID; |
| 755 | |
| 756 | success: |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 757 | if (buf) { |
| 758 | if (!(*buffer) && size != len + 1) { |
| 759 | /* not using provided buffer, so fit the allocated buffer to what we really have inside */ |
| 760 | p = realloc(buf, len + 1); |
| 761 | /* ignore realloc fail because we are reducing the buffer, |
| 762 | * so just return bigger buffer than needed */ |
| 763 | if (p) { |
| 764 | size = len + 1; |
| 765 | buf = p; |
| 766 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 767 | } |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 768 | /* set terminating NULL byte */ |
| 769 | buf[len] = '\0'; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 770 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 771 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 772 | context->status -= 1; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 773 | if (buf) { |
| 774 | (*buffer) = buf; |
| 775 | (*buffer_size) = size; |
| 776 | (*output) = buf; |
| 777 | (*dynamic) = 1; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 778 | (*length) = len; |
| 779 | } else if (output) { |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 780 | (*output) = (char*)start; |
| 781 | (*dynamic) = 0; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 782 | (*length) = len; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 783 | } |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 784 | |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 785 | if (context->status == LYXML_ATTRIBUTE) { |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 786 | /* skip whitespaces after the value */ |
| 787 | ign_xmlws(context, in); |
| 788 | |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 789 | if (in[0] == '>') { |
| 790 | /* element terminated by > - termination of the opening tag */ |
| 791 | context->status = LYXML_ELEM_CONTENT; |
| 792 | ++in; |
| 793 | } else if (in[0] == '/' && in[1] == '>') { |
| 794 | /* element terminated by /> - termination of an empty element */ |
| 795 | context->status = LYXML_ELEMENT; |
| 796 | in += 2; |
| 797 | |
| 798 | /* remove the closed element record from the tags list */ |
| 799 | free(context->elements.objs[context->elements.count - 1]); |
| 800 | --context->elements.count; |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 801 | |
| 802 | /* remove also the namespaces conneted with the element */ |
Radek Krejci | 17dca99 | 2019-05-17 10:53:27 +0200 | [diff] [blame] | 803 | lyxml_ns_rm(context); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 804 | |
| 805 | if (!context->elements.count && in[0] == '\0') { |
| 806 | /* EOF */ |
| 807 | context->status = LYXML_END; |
| 808 | } |
| 809 | } /* else another attribute */ |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | (*input) = in; |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 813 | return rc; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 814 | |
| 815 | #undef BUFSIZE |
| 816 | #undef BUFSIZE_STEP |
| 817 | #undef BUFSIZE_CHECK |
| 818 | } |
| 819 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 820 | LY_ERR |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 821 | lyxml_get_attribute(struct lyxml_context *context, const char **input, |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 822 | const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 823 | { |
| 824 | struct ly_ctx *ctx = context->ctx; /* shortcut */ |
| 825 | const char *in = (*input); |
| 826 | const char *id; |
| 827 | const char *endtag; |
| 828 | LY_ERR rc; |
| 829 | unsigned int c; |
| 830 | size_t endtag_len; |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 831 | int is_ns = 0; |
| 832 | const char *ns_prefix = NULL; |
| 833 | size_t ns_prefix_len = 0; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 834 | |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 835 | start: |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 836 | /* initialize output variables */ |
| 837 | (*prefix) = (*name) = NULL; |
| 838 | (*prefix_len) = (*name_len) = 0; |
| 839 | |
| 840 | /* skip initial whitespaces */ |
| 841 | ign_xmlws(context, in); |
| 842 | |
| 843 | if (in[0] == '\0') { |
| 844 | /* EOF - not expected at this place */ |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 845 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF); |
| 846 | return LY_EVALID; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /* remember the identifier start before checking its format */ |
| 850 | id = in; |
| 851 | rc = lyxml_check_qname(context, &in, &c, &endtag_len); |
| 852 | LY_CHECK_RET(rc); |
| 853 | if (c == ':') { |
| 854 | /* we have prefixed identifier */ |
| 855 | endtag = in - endtag_len; |
| 856 | |
| 857 | rc = lyxml_check_qname(context, &in, &c, &endtag_len); |
| 858 | LY_CHECK_RET(rc); |
| 859 | |
| 860 | (*prefix) = id; |
| 861 | (*prefix_len) = endtag - id; |
| 862 | id = endtag + 1; |
| 863 | } |
| 864 | if (!is_xmlws(c) && c != '=') { |
| 865 | in = in - endtag_len; |
| 866 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "whitespace or '='"); |
| 867 | return LY_EVALID; |
| 868 | } |
| 869 | in = in - endtag_len; |
| 870 | (*name) = id; |
| 871 | (*name_len) = in - id; |
| 872 | |
| 873 | /* eat '=' and stop at the value beginning */ |
| 874 | ign_xmlws(context, in); |
| 875 | if (in[0] != '=') { |
| 876 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "'='"); |
| 877 | return LY_EVALID; |
| 878 | } |
| 879 | ++in; |
| 880 | ign_xmlws(context, in); |
| 881 | if (in[0] != '\'' && in[0] != '"') { |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 882 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, |
| 883 | LY_VCODE_INSTREXP_len(in), in, "either single or double quotation mark"); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 884 | return LY_EVALID; |
| 885 | } |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 886 | context->status = LYXML_ATTR_CONTENT; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 887 | |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 888 | is_ns = 0; |
| 889 | if (*prefix && *prefix_len == 5 && !strncmp(*prefix, "xmlns", 5)) { |
| 890 | is_ns = 1; |
| 891 | ns_prefix = *name; |
| 892 | ns_prefix_len = *name_len; |
| 893 | } else if (*name_len == 5 && !strncmp(*name, "xmlns", 5)) { |
| 894 | is_ns = 1; |
| 895 | } |
| 896 | if (is_ns) { |
| 897 | /* instead of attribute, we have namespace specification, |
| 898 | * so process it automatically and then move to another attribute (if any) */ |
| 899 | char *value = NULL; |
| 900 | size_t value_len = 0; |
| 901 | int dynamic = 0; |
| 902 | |
| 903 | LY_CHECK_RET(lyxml_get_string(context, &in, &value, &value_len, &value, &value_len, &dynamic)); |
| 904 | if ((rc = lyxml_ns_add(context, ns_prefix, ns_prefix_len, dynamic ? value : strndup(value, value_len)))) { |
| 905 | if (dynamic) { |
| 906 | free(value); |
| 907 | return rc; |
| 908 | } |
| 909 | } |
| 910 | if (context->status == LYXML_ATTRIBUTE) { |
| 911 | goto start; |
| 912 | } else { |
| 913 | (*prefix) = (*name) = NULL; |
| 914 | (*prefix_len) = (*name_len) = 0; |
| 915 | } |
| 916 | } |
| 917 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 918 | /* move caller's input */ |
| 919 | (*input) = in; |
| 920 | return LY_SUCCESS; |
| 921 | } |
| 922 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 923 | void |
| 924 | lyxml_context_clear(struct lyxml_context *context) |
| 925 | { |
| 926 | unsigned int u; |
| 927 | |
| 928 | ly_set_erase(&context->elements, free); |
| 929 | for (u = context->ns.count - 1; u + 1 > 0; --u) { |
| 930 | /* remove the ns structure */ |
| 931 | free(((struct lyxml_ns *)context->ns.objs[u])->prefix); |
| 932 | free(((struct lyxml_ns *)context->ns.objs[u])->uri); |
| 933 | free(context->ns.objs[u]); |
| 934 | } |
| 935 | ly_set_erase(&context->ns, NULL); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 936 | context->status = 0; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 937 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 938 | |
| 939 | LY_ERR |
| 940 | lyxml_dump_text(struct lyout *out, const char *text, int attribute) |
| 941 | { |
| 942 | LY_ERR ret = LY_SUCCESS; |
| 943 | unsigned int u; |
| 944 | |
| 945 | if (!text) { |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | for (u = 0; text[u]; u++) { |
| 950 | switch (text[u]) { |
| 951 | case '&': |
| 952 | ret = ly_print(out, "&"); |
| 953 | break; |
| 954 | case '<': |
| 955 | ret = ly_print(out, "<"); |
| 956 | break; |
| 957 | case '>': |
| 958 | /* not needed, just for readability */ |
| 959 | ret = ly_print(out, ">"); |
| 960 | break; |
| 961 | case '"': |
| 962 | if (attribute) { |
| 963 | ret = ly_print(out, """); |
| 964 | break; |
| 965 | } |
| 966 | /* falls through */ |
| 967 | default: |
| 968 | ly_write(out, &text[u], 1); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | return ret; |
| 973 | } |
| 974 | |