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