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 | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 15 | #define _POSIX_C_SOURCE 200809L /* strndup() */ |
| 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 | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 21 | #include <string.h> |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 22 | |
| 23 | #include "libyang.h" |
| 24 | #include "xml.h" |
| 25 | #include "common.h" |
| 26 | |
| 27 | /* Macro to test if character is whitespace */ |
| 28 | #define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd) |
| 29 | |
| 30 | /* Macro to test if character is allowed to be a first character of an qualified identifier */ |
| 31 | #define is_xmlqnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \ |
| 32 | (c >= 'A' && c <= 'Z') || /* c == ':' || */ \ |
| 33 | (c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \ |
| 34 | (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \ |
| 35 | c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \ |
| 36 | (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \ |
| 37 | (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \ |
| 38 | (c >= 0x10000 && c <= 0xeffff)) |
| 39 | |
| 40 | /* Macro to test if character is allowed to be used in an qualified identifier */ |
| 41 | #define is_xmlqnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \ |
| 42 | (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || /* c == ':' || */ \ |
| 43 | c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\ |
| 44 | (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \ |
| 45 | c == 0x200d || (c >= 0x300 && c <= 0x36f) || \ |
| 46 | (c >= 0x2070 && c <= 0x218f) || (c >= 0x2030f && c <= 0x2040) || \ |
| 47 | (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \ |
| 48 | (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \ |
| 49 | (c >= 0x10000 && c <= 0xeffff)) |
| 50 | |
| 51 | /* Move input p by s characters, if EOF log with lyxml_context c */ |
| 52 | #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) |
| 53 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 54 | /* Ignore whitespaces in the input string p */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 55 | #define ign_xmlws(c,p) while (is_xmlws(*(p))) {if (*(p) == '\n') {++c->line;} ++p;} |
| 56 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 57 | /** |
| 58 | * @brief Ignore any characters until the delim of the size delim_len is read |
| 59 | * |
| 60 | * Detects number of read new lines. |
| 61 | * Returns the pointer to the beginning of the detected delim, or NULL in case the delim not found in |
| 62 | * NULL-terminated input string. |
| 63 | * */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 64 | static const char * |
| 65 | ign_todelim(register const char *input, const char *delim, size_t delim_len, size_t *newlines) |
| 66 | { |
| 67 | size_t i; |
| 68 | register const char *a, *b; |
| 69 | |
| 70 | (*newlines) = 0; |
| 71 | for ( ; *input; ++input) { |
| 72 | if (*input != *delim) { |
| 73 | if (*input == '\n') { |
| 74 | ++(*newlines); |
| 75 | } |
| 76 | continue; |
| 77 | } |
| 78 | a = input; |
| 79 | b = delim; |
| 80 | for (i = 0; i < delim_len; ++i) { |
| 81 | if (*a++ != *b++) { |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | if (i == delim_len) { |
| 86 | return input; |
| 87 | } |
| 88 | } |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 92 | /** |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 93 | * Store UTF-8 character specified as 4byte integer into the dst buffer. |
| 94 | * Returns number of written bytes (4 max), expects that dst has enough space. |
| 95 | * |
| 96 | * UTF-8 mapping: |
| 97 | * 00000000 -- 0000007F: 0xxxxxxx |
| 98 | * 00000080 -- 000007FF: 110xxxxx 10xxxxxx |
| 99 | * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx |
| 100 | * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 101 | * |
| 102 | * Includes checking for valid characters (following RFC 7950, sec 9.4) |
| 103 | */ |
| 104 | static LY_ERR |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 105 | lyxml_pututf8(char *dst, uint32_t value, size_t *bytes_written) |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 106 | { |
| 107 | if (value < 0x80) { |
| 108 | /* one byte character */ |
| 109 | if (value < 0x20 && |
| 110 | value != 0x09 && |
| 111 | value != 0x0a && |
| 112 | value != 0x0d) { |
| 113 | return LY_EINVAL; |
| 114 | } |
| 115 | |
| 116 | dst[0] = value; |
| 117 | (*bytes_written) = 1; |
| 118 | } else if (value < 0x800) { |
| 119 | /* two bytes character */ |
| 120 | dst[0] = 0xc0 | (value >> 6); |
| 121 | dst[1] = 0x80 | (value & 0x3f); |
| 122 | (*bytes_written) = 2; |
| 123 | } else if (value < 0xfffe) { |
| 124 | /* three bytes character */ |
| 125 | if (((value & 0xf800) == 0xd800) || |
| 126 | (value >= 0xfdd0 && value <= 0xfdef)) { |
| 127 | /* exclude surrogate blocks %xD800-DFFF */ |
| 128 | /* exclude noncharacters %xFDD0-FDEF */ |
| 129 | return LY_EINVAL; |
| 130 | } |
| 131 | |
| 132 | dst[0] = 0xe0 | (value >> 12); |
| 133 | dst[1] = 0x80 | ((value >> 6) & 0x3f); |
| 134 | dst[2] = 0x80 | (value & 0x3f); |
| 135 | |
| 136 | (*bytes_written) = 3; |
| 137 | } else if (value < 0x10fffe) { |
| 138 | if ((value & 0xffe) == 0xffe) { |
| 139 | /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF, |
| 140 | * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF, |
| 141 | * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */ |
| 142 | return LY_EINVAL; |
| 143 | } |
| 144 | /* four bytes character */ |
| 145 | dst[0] = 0xf0 | (value >> 18); |
| 146 | dst[1] = 0x80 | ((value >> 12) & 0x3f); |
| 147 | dst[2] = 0x80 | ((value >> 6) & 0x3f); |
| 148 | dst[3] = 0x80 | (value & 0x3f); |
| 149 | |
| 150 | (*bytes_written) = 4; |
| 151 | } |
| 152 | return LY_SUCCESS; |
| 153 | } |
| 154 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 155 | /** |
| 156 | * @brief Check/Get an XML qualified name from the input string. |
| 157 | * |
| 158 | * The identifier must have at least one valid character complying the name start character constraints. |
| 159 | * The identifier is terminated by the first character, which does not comply to the name character constraints. |
| 160 | * |
| 161 | * See https://www.w3.org/TR/xml-names/#NT-NCName |
| 162 | * |
| 163 | * @param[in] context XML context to track lines or store errors into libyang context. |
| 164 | * @param[in,out] input Input string to process, updated according to the processed/read data. |
| 165 | * Note that the term_char is also read, so input points after the term_char at the end. |
| 166 | * @param[out] term_char The first character in the input string which does not compy to the name constraints. |
| 167 | * @param[out] term_char_len Number of bytes used to encode UTF8 term_char. Serves to be able to go back in input string. |
| 168 | * @return LY_ERR value. |
| 169 | */ |
| 170 | static LY_ERR |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 171 | lyxml_check_qname(struct lyxml_context *context, const char **input, unsigned int *term_char, size_t *term_char_len) |
| 172 | { |
| 173 | unsigned int c; |
| 174 | const char *id = (*input); |
| 175 | LY_ERR rc; |
| 176 | |
| 177 | /* check NameStartChar (minus colon) */ |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 178 | LY_CHECK_ERR_RET(ly_getutf8(input, &c, NULL) != LY_SUCCESS, |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 179 | LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID); |
| 180 | LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c), |
| 181 | LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, |
| 182 | "Identifier \"%s\" starts with invalid character.", id), |
| 183 | LY_EVALID); |
| 184 | |
| 185 | /* check rest of the identifier */ |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 186 | for (rc = ly_getutf8(input, &c, term_char_len); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 187 | rc == LY_SUCCESS && is_xmlqnamechar(c); |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 188 | rc = ly_getutf8(input, &c, term_char_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 189 | LY_CHECK_ERR_RET(rc != LY_SUCCESS, LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID); |
| 190 | |
| 191 | (*term_char) = c; |
| 192 | return LY_SUCCESS; |
| 193 | } |
| 194 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 195 | LY_ERR |
| 196 | lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size) |
| 197 | { |
| 198 | #define BUFSIZE 4096 |
| 199 | #define BUFSIZE_STEP 4096 |
| 200 | #define BUFSIZE_CHECK(CTX, BUF, SIZE, CURR, NEED) \ |
| 201 | if (CURR+NEED >= SIZE) { \ |
| 202 | BUF = ly_realloc(BUF, SIZE + BUFSIZE_STEP); \ |
| 203 | LY_CHECK_ERR_RET(!BUF, LOGMEM(CTX), LY_EMEM); \ |
| 204 | SIZE += BUFSIZE_STEP; \ |
| 205 | } |
| 206 | |
| 207 | struct ly_ctx *ctx = context->ctx; /* shortcut */ |
| 208 | const char *in = (*input); |
| 209 | char *buf, delim; |
| 210 | size_t offset; /* read offset in input buffer */ |
| 211 | size_t len; /* write offset in output buffer */ |
| 212 | size_t size; /* size of the output buffer */ |
| 213 | void *p; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 214 | uint32_t n; |
| 215 | size_t u, newlines; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 216 | bool empty_content = false; |
| 217 | LY_ERR rc; |
| 218 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 219 | assert(context); |
| 220 | assert(context->status == LYXML_ELEM_CONTENT || context->status == LYXML_ATTR_CONTENT); |
| 221 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 222 | if (in[0] == '\'') { |
| 223 | delim = '\''; |
| 224 | ++in; |
| 225 | } else if (in[0] == '"') { |
| 226 | delim = '"'; |
| 227 | ++in; |
| 228 | } else { |
| 229 | delim = '<'; |
| 230 | empty_content = true; |
| 231 | } |
| 232 | |
| 233 | if (empty_content) { |
| 234 | /* only when processing element's content - try to ignore whitespaces used to format XML data |
| 235 | * before element's child or closing tag */ |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 236 | for (offset = newlines = 0; in[offset] && is_xmlws(in[offset]); ++offset) { |
| 237 | if (in[offset] == '\n') { |
| 238 | ++newlines; |
| 239 | } |
| 240 | } |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 241 | 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] | 242 | context->line += newlines; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 243 | if (in[offset] == '<') { |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 244 | (*input) = in + offset; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 245 | return LY_EINVAL; |
| 246 | } |
| 247 | } else { |
| 248 | /* init */ |
| 249 | offset = 0; |
| 250 | } |
| 251 | |
| 252 | /* prepare output buffer */ |
| 253 | if (*buffer) { |
| 254 | buf = *buffer; |
| 255 | size = *buffer_size; |
| 256 | } else { |
| 257 | buf = malloc(BUFSIZE); |
| 258 | size = BUFSIZE; |
| 259 | |
| 260 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM); |
| 261 | } |
| 262 | len = 0; |
| 263 | |
| 264 | /* parse */ |
| 265 | while (in[offset]) { |
| 266 | if (in[offset] == '&') { |
| 267 | if (offset) { |
| 268 | /* store what we have so far */ |
| 269 | BUFSIZE_CHECK(ctx, buf, size, len, offset); |
| 270 | memcpy(&buf[len], in, offset); |
| 271 | len += offset; |
| 272 | in += offset; |
| 273 | offset = 0; |
| 274 | } |
| 275 | /* process reference */ |
| 276 | /* we will need 4 bytes at most since we support only the predefined |
| 277 | * (one-char) entities and character references */ |
| 278 | BUFSIZE_CHECK(ctx, buf, size, len, 4); |
| 279 | ++offset; |
| 280 | if (in[offset] != '#') { |
| 281 | /* entity reference - only predefined references are supported */ |
| 282 | if (!strncmp(&in[offset], "lt;", 3)) { |
| 283 | buf[len++] = '<'; |
| 284 | in += 4; /* < */ |
| 285 | } else if (!strncmp(&in[offset], "gt;", 3)) { |
| 286 | buf[len++] = '>'; |
| 287 | in += 4; /* > */ |
| 288 | } else if (!strncmp(&in[offset], "amp;", 4)) { |
| 289 | buf[len++] = '&'; |
| 290 | in += 5; /* & */ |
| 291 | } else if (!strncmp(&in[offset], "apos;", 5)) { |
| 292 | buf[len++] = '\''; |
| 293 | in += 6; /* ' */ |
| 294 | } else if (!strncmp(&in[offset], "quot;", 5)) { |
| 295 | buf[len++] = '\"'; |
| 296 | in += 6; /* " */ |
| 297 | } else { |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 298 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, |
| 299 | "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] | 300 | goto error; |
| 301 | } |
| 302 | offset = 0; |
| 303 | } else { |
| 304 | p = (void*)&in[offset - 1]; |
| 305 | /* character reference */ |
| 306 | ++offset; |
| 307 | if (isdigit(in[offset])) { |
| 308 | for (n = 0; isdigit(in[offset]); offset++) { |
| 309 | n = (10 * n) + (in[offset] - '0'); |
| 310 | } |
| 311 | } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) { |
| 312 | for (n = 0, ++offset; isxdigit(in[offset]); offset++) { |
| 313 | if (isdigit(in[offset])) { |
| 314 | u = (in[offset] - '0'); |
| 315 | } else if (in[offset] > 'F') { |
| 316 | u = 10 + (in[offset] - 'a'); |
| 317 | } else { |
| 318 | u = 10 + (in[offset] - 'A'); |
| 319 | } |
| 320 | n = (16 * n) + u; |
| 321 | } |
| 322 | } else { |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 323 | 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] | 324 | goto error; |
| 325 | |
| 326 | } |
| 327 | LY_CHECK_ERR_GOTO(in[offset] != ';', |
| 328 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, |
| 329 | LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"), |
| 330 | error); |
| 331 | ++offset; |
| 332 | rc = lyxml_pututf8(&buf[len], n, &u); |
| 333 | 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] | 334 | "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n), |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 335 | error); |
| 336 | len += u; |
| 337 | in += offset; |
| 338 | offset = 0; |
| 339 | } |
| 340 | } else if (in[offset] == delim) { |
| 341 | /* end of string */ |
| 342 | if (len + offset >= size) { |
| 343 | buf = ly_realloc(buf, len + offset + 1); |
| 344 | LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM); |
| 345 | size = len + offset + 1; |
| 346 | } |
| 347 | memcpy(&buf[len], in, offset); |
| 348 | len += offset; |
| 349 | /* in case of element content, keep the leading <, |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 350 | * for attribute's value move after the terminating quotation mark */ |
| 351 | if (context->status == LYXML_ELEM_CONTENT) { |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 352 | in += offset; |
| 353 | } else { |
| 354 | in += offset + 1; |
| 355 | } |
| 356 | goto success; |
| 357 | } else { |
| 358 | /* log lines */ |
| 359 | if (in[offset] == '\n') { |
| 360 | ++context->line; |
| 361 | } |
| 362 | |
| 363 | /* continue */ |
| 364 | ++offset; |
| 365 | } |
| 366 | } |
| 367 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF); |
| 368 | error: |
| 369 | if (!(*buffer)) { |
| 370 | free(buf); |
| 371 | } |
| 372 | return LY_EVALID; |
| 373 | |
| 374 | success: |
| 375 | if (!(*buffer) && size != len + 1) { |
| 376 | /* not using provided buffer, so fit the allocated buffer to what we really have inside */ |
| 377 | p = realloc(buf, len + 1); |
| 378 | /* ignore realloc fail because we are reducing the buffer, |
| 379 | * so just return bigger buffer than needed */ |
| 380 | if (p) { |
| 381 | size = len + 1; |
| 382 | buf = p; |
| 383 | } |
| 384 | } |
| 385 | /* set terminating NULL byte */ |
| 386 | buf[len] = '\0'; |
| 387 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 388 | context->status -= 1; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 389 | (*input) = in; |
| 390 | (*buffer) = buf; |
| 391 | (*buffer_size) = size; |
| 392 | return LY_SUCCESS; |
| 393 | |
| 394 | #undef BUFSIZE |
| 395 | #undef BUFSIZE_STEP |
| 396 | #undef BUFSIZE_CHECK |
| 397 | } |
| 398 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 399 | LY_ERR |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 400 | lyxml_get_attribute(struct lyxml_context *context, const char **input, |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 401 | const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 402 | { |
| 403 | struct ly_ctx *ctx = context->ctx; /* shortcut */ |
| 404 | const char *in = (*input); |
| 405 | const char *id; |
| 406 | const char *endtag; |
| 407 | LY_ERR rc; |
| 408 | unsigned int c; |
| 409 | size_t endtag_len; |
| 410 | |
| 411 | /* initialize output variables */ |
| 412 | (*prefix) = (*name) = NULL; |
| 413 | (*prefix_len) = (*name_len) = 0; |
| 414 | |
| 415 | /* skip initial whitespaces */ |
| 416 | ign_xmlws(context, in); |
| 417 | |
| 418 | if (in[0] == '\0') { |
| 419 | /* EOF - not expected at this place */ |
| 420 | return LY_EINVAL; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 421 | } else if (in[0] == '>') { |
| 422 | /* element terminated by > - termination of the opening tag */ |
| 423 | context->status = LYXML_ELEM_CONTENT; |
| 424 | ++in; |
| 425 | goto success; |
| 426 | } else if (in[0] == '/' && in[1] == '>') { |
| 427 | /* element terminated by /> - termination of an empty element */ |
| 428 | context->status = LYXML_ELEMENT; |
| 429 | in += 2; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 430 | goto success; |
| 431 | } |
| 432 | |
| 433 | /* remember the identifier start before checking its format */ |
| 434 | id = in; |
| 435 | rc = lyxml_check_qname(context, &in, &c, &endtag_len); |
| 436 | LY_CHECK_RET(rc); |
| 437 | if (c == ':') { |
| 438 | /* we have prefixed identifier */ |
| 439 | endtag = in - endtag_len; |
| 440 | |
| 441 | rc = lyxml_check_qname(context, &in, &c, &endtag_len); |
| 442 | LY_CHECK_RET(rc); |
| 443 | |
| 444 | (*prefix) = id; |
| 445 | (*prefix_len) = endtag - id; |
| 446 | id = endtag + 1; |
| 447 | } |
| 448 | if (!is_xmlws(c) && c != '=') { |
| 449 | in = in - endtag_len; |
| 450 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "whitespace or '='"); |
| 451 | return LY_EVALID; |
| 452 | } |
| 453 | in = in - endtag_len; |
| 454 | (*name) = id; |
| 455 | (*name_len) = in - id; |
| 456 | |
| 457 | /* eat '=' and stop at the value beginning */ |
| 458 | ign_xmlws(context, in); |
| 459 | if (in[0] != '=') { |
| 460 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "'='"); |
| 461 | return LY_EVALID; |
| 462 | } |
| 463 | ++in; |
| 464 | ign_xmlws(context, in); |
| 465 | if (in[0] != '\'' && in[0] != '"') { |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 466 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, |
| 467 | LY_VCODE_INSTREXP_len(in), in, "either single or double quotation mark"); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 468 | return LY_EVALID; |
| 469 | } |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 470 | context->status = LYXML_ATTR_CONTENT; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 471 | |
| 472 | success: |
| 473 | /* move caller's input */ |
| 474 | (*input) = in; |
| 475 | return LY_SUCCESS; |
| 476 | } |
| 477 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 478 | LY_ERR |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 479 | lyxml_get_element(struct lyxml_context *context, const char **input, |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 480 | const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 481 | { |
| 482 | struct ly_ctx *ctx = context->ctx; /* shortcut */ |
| 483 | const char *in = (*input); |
| 484 | const char *endtag; |
| 485 | const char *sectname; |
| 486 | const char *id; |
| 487 | size_t endtag_len, newlines; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 488 | bool loop = true, closing = false; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 489 | unsigned int c; |
| 490 | LY_ERR rc; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 491 | struct lyxml_elem *e; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 492 | |
| 493 | /* initialize output variables */ |
| 494 | (*prefix) = (*name) = NULL; |
| 495 | (*prefix_len) = (*name_len) = 0; |
| 496 | |
| 497 | while (loop) { |
| 498 | ign_xmlws(context, in); |
| 499 | |
| 500 | if (in[0] == '\0') { |
| 501 | /* EOF */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 502 | context->status = LYXML_END; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 503 | goto success; |
| 504 | } else if (in[0] != '<') { |
| 505 | return LY_EINVAL; |
| 506 | } |
| 507 | move_input(context, in, 1); |
| 508 | |
| 509 | if (in[0] == '!') { |
| 510 | move_input(context, in, 1); |
| 511 | /* sections to ignore */ |
| 512 | if (!strncmp(in, "--", 2)) { |
| 513 | /* comment */ |
| 514 | move_input(context, in, 2); |
| 515 | sectname = "Comment"; |
| 516 | endtag = "-->"; |
| 517 | endtag_len = 3; |
| 518 | } else if (!strncmp(in, "[CDATA[", 7)) { |
| 519 | /* CDATA section */ |
| 520 | move_input(context, in, 7); |
| 521 | sectname = "CData"; |
| 522 | endtag = "]]>"; |
| 523 | endtag_len = 3; |
| 524 | } else if (!strncmp(in, "DOCTYPE", 7)) { |
| 525 | /* Document type declaration - not supported */ |
| 526 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP, "Document Type Declaration"); |
| 527 | return LY_EVALID; |
| 528 | } |
| 529 | in = ign_todelim(in, endtag, endtag_len, &newlines); |
| 530 | LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, sectname), LY_EVALID); |
| 531 | context->line += newlines; |
| 532 | in += endtag_len; |
| 533 | } else if (in[0] == '?') { |
| 534 | in = ign_todelim(in, "?>", 2, &newlines); |
| 535 | LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID); |
| 536 | context->line += newlines; |
| 537 | in += 2; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 538 | } else if (in[0] == '/') { |
| 539 | /* closing element */ |
| 540 | closing = true; |
| 541 | ++in; |
| 542 | goto element; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 543 | } else { |
| 544 | /* element */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 545 | element: |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 546 | ign_xmlws(context, in); |
| 547 | LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID); |
| 548 | |
| 549 | /* remember the identifier start before checking its format */ |
| 550 | id = in; |
| 551 | rc = lyxml_check_qname(context, &in, &c, &endtag_len); |
| 552 | LY_CHECK_RET(rc); |
| 553 | if (c == ':') { |
| 554 | /* we have prefixed identifier */ |
| 555 | endtag = in - endtag_len; |
| 556 | |
| 557 | rc = lyxml_check_qname(context, &in, &c, &endtag_len); |
| 558 | LY_CHECK_RET(rc); |
| 559 | |
| 560 | (*prefix) = id; |
| 561 | (*prefix_len) = endtag - id; |
| 562 | id = endtag + 1; |
| 563 | } |
| 564 | if (!is_xmlws(c) && c != '/' && c != '>') { |
| 565 | in = in - endtag_len; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 566 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, |
| 567 | "whitespace or element tag termination ('>' or '/>'"); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 568 | return LY_EVALID; |
| 569 | } |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 570 | (*name) = id; |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 571 | (*name_len) = in - endtag_len - id; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 572 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 573 | if (is_xmlws(c)) { |
| 574 | /* go to the next meaningful input */ |
| 575 | ign_xmlws(context, in); |
| 576 | LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID); |
| 577 | c = in[0]; |
| 578 | ++in; |
| 579 | endtag_len = 1; |
| 580 | } |
| 581 | |
| 582 | if (closing) { |
| 583 | /* match opening and closing element tags */ |
| 584 | LY_CHECK_ERR_RET( |
| 585 | !context->elements.count, |
| 586 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Opening and closing elements tag missmatch (\"%.*s\").", name_len, *name), |
| 587 | LY_EVALID); |
| 588 | e = (struct lyxml_elem*)context->elements.objs[context->elements.count - 1]; |
| 589 | LY_CHECK_ERR_RET(e->prefix_len != *prefix_len || e->name_len != *name_len |
| 590 | || (*prefix_len && strncmp(*prefix, e->prefix, e->prefix_len)) || strncmp(*name, e->name, e->name_len), |
| 591 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Opening and closing elements tag missmatch (\"%.*s\").", name_len, *name), |
| 592 | LY_EVALID); |
| 593 | /* opening and closing element tags matches, remove record from the opening tags list */ |
| 594 | free(e); |
| 595 | --context->elements.count; |
| 596 | /* do not return element information to announce closing element being currently processed */ |
| 597 | *name = *prefix = NULL; |
| 598 | *name_len = *prefix_len = 0; |
| 599 | |
| 600 | if (c == '>') { |
| 601 | /* end of closing element */ |
| 602 | context->status = LYXML_ELEMENT; |
| 603 | } else { |
| 604 | in -= endtag_len; |
| 605 | LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Unexpected data \"%.*s\" in closing element tag.", |
| 606 | LY_VCODE_INSTREXP_len(in), in); |
| 607 | return LY_EVALID; |
| 608 | } |
| 609 | } else { |
| 610 | if (c == '>') { |
| 611 | /* end of opening element */ |
| 612 | context->status = LYXML_ELEM_CONTENT; |
| 613 | } else if (c == '/' && in[0] == '>') { |
| 614 | /* empty element closing */ |
| 615 | context->status = LYXML_ELEMENT; |
| 616 | ++in; |
| 617 | } else { |
| 618 | /* attribute */ |
| 619 | context->status = LYXML_ATTRIBUTE; |
| 620 | in -= endtag_len; |
| 621 | } |
| 622 | |
| 623 | if (context->status != LYXML_ELEMENT) { |
| 624 | /* store element opening tag information */ |
| 625 | e = malloc(sizeof *e); |
| 626 | LY_CHECK_ERR_RET(!e, LOGMEM(ctx), LY_EMEM); |
| 627 | e->name = *name; |
| 628 | e->prefix = *prefix; |
| 629 | e->name_len = *name_len; |
| 630 | e->prefix_len = *prefix_len; |
| 631 | ly_set_add(&context->elements, e, LY_SET_OPT_USEASLIST); |
| 632 | } |
| 633 | } |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 634 | loop = false; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | success: |
| 639 | /* move caller's input */ |
| 640 | (*input) = in; |
| 641 | return LY_SUCCESS; |
| 642 | } |
| 643 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 644 | LY_ERR |
| 645 | lyxml_ns_add(struct lyxml_context *context, const char *element_name, const char *prefix, size_t prefix_len, char *uri) |
| 646 | { |
| 647 | struct lyxml_ns *ns; |
| 648 | |
| 649 | ns = malloc(sizeof *ns); |
| 650 | LY_CHECK_ERR_RET(!ns, LOGMEM(context->ctx), LY_EMEM); |
| 651 | |
| 652 | ns->element = element_name; |
| 653 | ns->uri = uri; |
| 654 | if (prefix) { |
| 655 | ns->prefix = strndup(prefix, prefix_len); |
| 656 | LY_CHECK_ERR_RET(!ns->prefix, LOGMEM(context->ctx); free(ns), LY_EMEM); |
| 657 | } else { |
| 658 | ns->prefix = NULL; |
| 659 | } |
| 660 | |
| 661 | LY_CHECK_ERR_RET(ly_set_add(&context->ns, ns, LY_SET_OPT_USEASLIST) == -1, free(ns->prefix), LY_EMEM); |
| 662 | return LY_SUCCESS; |
| 663 | } |
| 664 | |
| 665 | const struct lyxml_ns * |
| 666 | lyxml_ns_get(struct lyxml_context *context, const char *prefix, size_t prefix_len) |
| 667 | { |
| 668 | unsigned int u; |
| 669 | struct lyxml_ns *ns; |
| 670 | |
| 671 | for (u = context->ns.count - 1; u + 1 > 0; --u) { |
| 672 | ns = (struct lyxml_ns *)context->ns.objs[u]; |
| 673 | if (prefix) { |
| 674 | if (!strncmp(prefix, ns->prefix, prefix_len) && ns->prefix[prefix_len] == '\0') { |
| 675 | return ns; |
| 676 | } |
| 677 | } else if (!ns->prefix) { |
| 678 | /* default namespace */ |
| 679 | return ns; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return NULL; |
| 684 | } |
| 685 | |
| 686 | LY_ERR |
| 687 | lyxml_ns_rm(struct lyxml_context *context, const char *element_name) |
| 688 | { |
| 689 | unsigned int u; |
| 690 | |
| 691 | for (u = context->ns.count - 1; u + 1 > 0; --u) { |
| 692 | if (((struct lyxml_ns *)context->ns.objs[u])->element != element_name) { |
| 693 | /* we are done, the namespaces from a single element are supposed to be together */ |
| 694 | break; |
| 695 | } |
| 696 | /* remove the ns structure */ |
| 697 | free(((struct lyxml_ns *)context->ns.objs[u])->prefix); |
| 698 | free(((struct lyxml_ns *)context->ns.objs[u])->uri); |
| 699 | free(context->ns.objs[u]); |
| 700 | --context->ns.count; |
| 701 | } |
| 702 | |
| 703 | if (!context->ns.count) { |
| 704 | /* cleanup the context's namespaces storage */ |
| 705 | ly_set_erase(&context->ns, NULL); |
| 706 | } |
| 707 | |
| 708 | return LY_SUCCESS; |
| 709 | } |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame^] | 710 | |
| 711 | void |
| 712 | lyxml_context_clear(struct lyxml_context *context) |
| 713 | { |
| 714 | unsigned int u; |
| 715 | |
| 716 | ly_set_erase(&context->elements, free); |
| 717 | for (u = context->ns.count - 1; u + 1 > 0; --u) { |
| 718 | /* remove the ns structure */ |
| 719 | free(((struct lyxml_ns *)context->ns.objs[u])->prefix); |
| 720 | free(((struct lyxml_ns *)context->ns.objs[u])->uri); |
| 721 | free(context->ns.objs[u]); |
| 722 | } |
| 723 | ly_set_erase(&context->ns, NULL); |
| 724 | } |