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