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