blob: 796fee1248d8bc4611ee2ea76704b04f39d4abdd [file] [log] [blame]
Radek Krejcid91dbaf2018-09-21 15:51:39 +02001/**
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 Krejci7a7fa902018-09-25 17:08:21 +020015#include <ctype.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020016#include <stdbool.h>
17#include <stdint.h>
18
19#include "libyang.h"
20#include "xml.h"
21#include "common.h"
22
23/* Macro to test if character is whitespace */
24#define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd)
25
26/* Macro to test if character is allowed to be a first character of an qualified identifier */
27#define is_xmlqnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \
28 (c >= 'A' && c <= 'Z') || /* c == ':' || */ \
29 (c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \
30 (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
31 c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \
32 (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
33 (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
34 (c >= 0x10000 && c <= 0xeffff))
35
36/* Macro to test if character is allowed to be used in an qualified identifier */
37#define is_xmlqnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \
38 (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || /* c == ':' || */ \
39 c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\
40 (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
41 c == 0x200d || (c >= 0x300 && c <= 0x36f) || \
42 (c >= 0x2070 && c <= 0x218f) || (c >= 0x2030f && c <= 0x2040) || \
43 (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
44 (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
45 (c >= 0x10000 && c <= 0xeffff))
46
47/* Move input p by s characters, if EOF log with lyxml_context c */
48#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)
49
50/* Ignore whitespaces in the input string p, if EOF log with lyxml_context c */
51#define ign_xmlws(c,p) while (is_xmlws(*(p))) {if (*(p) == '\n') {++c->line;} ++p;}
52
53static const char *
54ign_todelim(register const char *input, const char *delim, size_t delim_len, size_t *newlines)
55{
56 size_t i;
57 register const char *a, *b;
58
59 (*newlines) = 0;
60 for ( ; *input; ++input) {
61 if (*input != *delim) {
62 if (*input == '\n') {
63 ++(*newlines);
64 }
65 continue;
66 }
67 a = input;
68 b = delim;
69 for (i = 0; i < delim_len; ++i) {
70 if (*a++ != *b++) {
71 break;
72 }
73 }
74 if (i == delim_len) {
75 return input;
76 }
77 }
78 return NULL;
79}
80
81static LY_ERR
82lyxml_getutf8(const char **input, unsigned int *utf8_char, size_t *bytes_read)
83{
84 unsigned int c, len;
85 int aux;
86 int i;
87
88 c = (*input)[0];
89 LY_CHECK_RET(!c, LY_EINVAL);
90
91 /* process character byte(s) */
92 if ((c & 0xf8) == 0xf0) {
93 /* four bytes character */
94 len = 4;
95
96 c &= 0x07;
97 for (i = 1; i <= 3; i++) {
98 aux = (*input)[i];
99 if ((aux & 0xc0) != 0x80) {
100 return LY_EINVAL;
101 }
102
103 c = (c << 6) | (aux & 0x3f);
104 }
105
106 if (c < 0x1000 || c > 0x10ffff) {
107 return LY_EINVAL;
108 }
109 } else if ((c & 0xf0) == 0xe0) {
110 /* three bytes character */
111 len = 3;
112
113 c &= 0x0f;
114 for (i = 1; i <= 2; i++) {
115 aux = (*input)[i];
116 if ((aux & 0xc0) != 0x80) {
117 return LY_EINVAL;
118 }
119
120 c = (c << 6) | (aux & 0x3f);
121 }
122
123 if (c < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) {
124 return LY_EINVAL;
125 }
126 } else if ((c & 0xe0) == 0xc0) {
127 /* two bytes character */
128 len = 2;
129
130 aux = (*input)[1];
131 if ((aux & 0xc0) != 0x80) {
132 return LY_EINVAL;
133 }
134 c = ((c & 0x1f) << 6) | (aux & 0x3f);
135
136 if (c < 0x80) {
137 return LY_EINVAL;
138 }
139 } else if (!(c & 0x80)) {
140 /* one byte character */
141 len = 1;
142
143 if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) {
144 return LY_EINVAL;
145 }
146 } else {
147 return LY_EINVAL;
148 }
149
150 (*utf8_char) = c;
151 (*input) += len;
152 if (bytes_read) {
153 (*bytes_read) = len;
154 }
155 return LY_SUCCESS;
156}
157
Radek Krejci7a7fa902018-09-25 17:08:21 +0200158/**
159 * Store UTF-8 character specified as 4byte integer into the dst buffer.
160 * Returns number of written bytes (4 max), expects that dst has enough space.
161 *
162 * UTF-8 mapping:
163 * 00000000 -- 0000007F: 0xxxxxxx
164 * 00000080 -- 000007FF: 110xxxxx 10xxxxxx
165 * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
166 * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
167 *
168 * Includes checking for valid characters (following RFC 7950, sec 9.4)
169 */
170static LY_ERR
171lyxml_pututf8(char *dst, int32_t value, size_t *bytes_written)
172{
173 if (value < 0x80) {
174 /* one byte character */
175 if (value < 0x20 &&
176 value != 0x09 &&
177 value != 0x0a &&
178 value != 0x0d) {
179 return LY_EINVAL;
180 }
181
182 dst[0] = value;
183 (*bytes_written) = 1;
184 } else if (value < 0x800) {
185 /* two bytes character */
186 dst[0] = 0xc0 | (value >> 6);
187 dst[1] = 0x80 | (value & 0x3f);
188 (*bytes_written) = 2;
189 } else if (value < 0xfffe) {
190 /* three bytes character */
191 if (((value & 0xf800) == 0xd800) ||
192 (value >= 0xfdd0 && value <= 0xfdef)) {
193 /* exclude surrogate blocks %xD800-DFFF */
194 /* exclude noncharacters %xFDD0-FDEF */
195 return LY_EINVAL;
196 }
197
198 dst[0] = 0xe0 | (value >> 12);
199 dst[1] = 0x80 | ((value >> 6) & 0x3f);
200 dst[2] = 0x80 | (value & 0x3f);
201
202 (*bytes_written) = 3;
203 } else if (value < 0x10fffe) {
204 if ((value & 0xffe) == 0xffe) {
205 /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF,
206 * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF,
207 * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */
208 return LY_EINVAL;
209 }
210 /* four bytes character */
211 dst[0] = 0xf0 | (value >> 18);
212 dst[1] = 0x80 | ((value >> 12) & 0x3f);
213 dst[2] = 0x80 | ((value >> 6) & 0x3f);
214 dst[3] = 0x80 | (value & 0x3f);
215
216 (*bytes_written) = 4;
217 }
218 return LY_SUCCESS;
219}
220
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200221LY_ERR
222lyxml_check_qname(struct lyxml_context *context, const char **input, unsigned int *term_char, size_t *term_char_len)
223{
224 unsigned int c;
225 const char *id = (*input);
226 LY_ERR rc;
227
228 /* check NameStartChar (minus colon) */
229 LY_CHECK_ERR_RET(lyxml_getutf8(input, &c, NULL) != LY_SUCCESS,
230 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID);
231 LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c),
232 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
233 "Identifier \"%s\" starts with invalid character.", id),
234 LY_EVALID);
235
236 /* check rest of the identifier */
237 for (rc = lyxml_getutf8(input, &c, term_char_len);
238 rc == LY_SUCCESS && is_xmlqnamechar(c);
239 rc = lyxml_getutf8(input, &c, term_char_len));
240 LY_CHECK_ERR_RET(rc != LY_SUCCESS, LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID);
241
242 (*term_char) = c;
243 return LY_SUCCESS;
244}
245
Radek Krejcid972c252018-09-25 13:23:39 +0200246/**
Radek Krejci7a7fa902018-09-25 17:08:21 +0200247 * @brief Parse input as XML text (attribute's values and element's content).
248 *
249 * Mixed content of XML elements is not allowed.
250 *
251 * In the case of attribute's values, the input string is expected to start on a quotation mark to
252 * select which delimiter (single or double quote) is used. Otherwise, the element content is beeing
253 * parsed expected to be terminated by '<' character.
254 *
255 * If function succeedes, the string in output buffer is always NULL-terminated.
256 *
257 * @param[in] context XML context to track lines or store errors into libyang context.
258 * @param[in,out] input Input string to process, updated according to the processed/read data.
259 * @param[out] buffer Storage of the output string. If NULL, the buffer is allocated. Otherwise, the buffer
260 * is used and enlarged when necessary.
261 * @param[out] buffer_size Allocated size of the returned buffer. If a buffer is provided by a caller, it
262 * is not being reduced even if the string is shorter. On the other hand, it can be enlarged if needed.
263 * @return LY_ERR value.
264 */
265LY_ERR
266lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size)
267{
268#define BUFSIZE 4096
269#define BUFSIZE_STEP 4096
270#define BUFSIZE_CHECK(CTX, BUF, SIZE, CURR, NEED) \
271 if (CURR+NEED >= SIZE) { \
272 BUF = ly_realloc(BUF, SIZE + BUFSIZE_STEP); \
273 LY_CHECK_ERR_RET(!BUF, LOGMEM(CTX), LY_EMEM); \
274 SIZE += BUFSIZE_STEP; \
275 }
276
277 struct ly_ctx *ctx = context->ctx; /* shortcut */
278 const char *in = (*input);
279 char *buf, delim;
280 size_t offset; /* read offset in input buffer */
281 size_t len; /* write offset in output buffer */
282 size_t size; /* size of the output buffer */
283 void *p;
284 int32_t n;
285 size_t u;
286 bool empty_content = false;
287 LY_ERR rc;
288
289 if (in[0] == '\'') {
290 delim = '\'';
291 ++in;
292 } else if (in[0] == '"') {
293 delim = '"';
294 ++in;
295 } else {
296 delim = '<';
297 empty_content = true;
298 }
299
300 if (empty_content) {
301 /* only when processing element's content - try to ignore whitespaces used to format XML data
302 * before element's child or closing tag */
303 for (offset = 0; in[offset] && is_xmlws(in[offset]); ++offset);
304 LY_CHECK_ERR_RET(!in[offset], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
305 if (in[offset] == '<') {
306 in += offset;
307 return LY_EINVAL;
308 }
309 } else {
310 /* init */
311 offset = 0;
312 }
313
314 /* prepare output buffer */
315 if (*buffer) {
316 buf = *buffer;
317 size = *buffer_size;
318 } else {
319 buf = malloc(BUFSIZE);
320 size = BUFSIZE;
321
322 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
323 }
324 len = 0;
325
326 /* parse */
327 while (in[offset]) {
328 if (in[offset] == '&') {
329 if (offset) {
330 /* store what we have so far */
331 BUFSIZE_CHECK(ctx, buf, size, len, offset);
332 memcpy(&buf[len], in, offset);
333 len += offset;
334 in += offset;
335 offset = 0;
336 }
337 /* process reference */
338 /* we will need 4 bytes at most since we support only the predefined
339 * (one-char) entities and character references */
340 BUFSIZE_CHECK(ctx, buf, size, len, 4);
341 ++offset;
342 if (in[offset] != '#') {
343 /* entity reference - only predefined references are supported */
344 if (!strncmp(&in[offset], "lt;", 3)) {
345 buf[len++] = '<';
346 in += 4; /* &lt; */
347 } else if (!strncmp(&in[offset], "gt;", 3)) {
348 buf[len++] = '>';
349 in += 4; /* &gt; */
350 } else if (!strncmp(&in[offset], "amp;", 4)) {
351 buf[len++] = '&';
352 in += 5; /* &amp; */
353 } else if (!strncmp(&in[offset], "apos;", 5)) {
354 buf[len++] = '\'';
355 in += 6; /* &apos; */
356 } else if (!strncmp(&in[offset], "quot;", 5)) {
357 buf[len++] = '\"';
358 in += 6; /* &quot; */
359 } else {
360 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP,
361 "entity references (except the predefined references)");
362 goto error;
363 }
364 offset = 0;
365 } else {
366 p = (void*)&in[offset - 1];
367 /* character reference */
368 ++offset;
369 if (isdigit(in[offset])) {
370 for (n = 0; isdigit(in[offset]); offset++) {
371 n = (10 * n) + (in[offset] - '0');
372 }
373 } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) {
374 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
375 if (isdigit(in[offset])) {
376 u = (in[offset] - '0');
377 } else if (in[offset] > 'F') {
378 u = 10 + (in[offset] - 'a');
379 } else {
380 u = 10 + (in[offset] - 'A');
381 }
382 n = (16 * n) + u;
383 }
384 } else {
385 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Invalid character reference %.*s", 12, p);
386 goto error;
387
388 }
389 LY_CHECK_ERR_GOTO(in[offset] != ';',
390 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP,
391 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
392 error);
393 ++offset;
394 rc = lyxml_pututf8(&buf[len], n, &u);
395 LY_CHECK_ERR_GOTO(rc, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
396 "Invalid character reference %.*s (0x%08x).", 12, p, n),
397 error);
398 len += u;
399 in += offset;
400 offset = 0;
401 }
402 } else if (in[offset] == delim) {
403 /* end of string */
404 if (len + offset >= size) {
405 buf = ly_realloc(buf, len + offset + 1);
406 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
407 size = len + offset + 1;
408 }
409 memcpy(&buf[len], in, offset);
410 len += offset;
411 /* in case of element content, keep the leading <,
412 * for attribute's value mova after the terminating quotation mark */
413 if (delim == '<') {
414 in += offset;
415 } else {
416 in += offset + 1;
417 }
418 goto success;
419 } else {
420 /* log lines */
421 if (in[offset] == '\n') {
422 ++context->line;
423 }
424
425 /* continue */
426 ++offset;
427 }
428 }
429 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF);
430error:
431 if (!(*buffer)) {
432 free(buf);
433 }
434 return LY_EVALID;
435
436success:
437 if (!(*buffer) && size != len + 1) {
438 /* not using provided buffer, so fit the allocated buffer to what we really have inside */
439 p = realloc(buf, len + 1);
440 /* ignore realloc fail because we are reducing the buffer,
441 * so just return bigger buffer than needed */
442 if (p) {
443 size = len + 1;
444 buf = p;
445 }
446 }
447 /* set terminating NULL byte */
448 buf[len] = '\0';
449
450 (*input) = in;
451 (*buffer) = buf;
452 (*buffer_size) = size;
453 return LY_SUCCESS;
454
455#undef BUFSIZE
456#undef BUFSIZE_STEP
457#undef BUFSIZE_CHECK
458}
459
460/**
Radek Krejcid972c252018-09-25 13:23:39 +0200461 * @brief Parse input expecting an XML attribute (including XML namespace).
462 *
463 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
464 * is returned.
465 *
466 * In case of a namespace definition, prefix just contains xmlns string. In case of the default namespace,
467 * prefix is NULL and the attribute name is xmlns.
468 *
469 * @param[in] context XML context to track lines or store errors into libyang context.
470 * @param[in,out] input Input string to process, updated according to the processed/read data so,
Radek Krejci7a7fa902018-09-25 17:08:21 +0200471 * when succeeded, it points to the opening quote of the attribute's value.
Radek Krejcid972c252018-09-25 13:23:39 +0200472 * @param[out] prefix Pointer to prefix if present in the attribute name, NULL otherwise.
473 * @param[out] prefix_len Length of the prefix if any.
474 * @param[out] name Attribute name. LY_SUCCESS can be returned with NULL name only in case the
475 * end of the element tag was reached.
476 * @param[out] name_len Length of the element name.
477 * @return LY_ERR values.
478 */
479LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200480lyxml_get_attribute(struct lyxml_context *context, const char **input,
Radek Krejcid972c252018-09-25 13:23:39 +0200481 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
482{
483 struct ly_ctx *ctx = context->ctx; /* shortcut */
484 const char *in = (*input);
485 const char *id;
486 const char *endtag;
487 LY_ERR rc;
488 unsigned int c;
489 size_t endtag_len;
490
491 /* initialize output variables */
492 (*prefix) = (*name) = NULL;
493 (*prefix_len) = (*name_len) = 0;
494
495 /* skip initial whitespaces */
496 ign_xmlws(context, in);
497
498 if (in[0] == '\0') {
499 /* EOF - not expected at this place */
500 return LY_EINVAL;
501 } else if (in[0] == '>' || in[0] == '/') {
502 /* element terminated by > or /> */
503 goto success;
504 }
505
506 /* remember the identifier start before checking its format */
507 id = in;
508 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
509 LY_CHECK_RET(rc);
510 if (c == ':') {
511 /* we have prefixed identifier */
512 endtag = in - endtag_len;
513
514 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
515 LY_CHECK_RET(rc);
516
517 (*prefix) = id;
518 (*prefix_len) = endtag - id;
519 id = endtag + 1;
520 }
521 if (!is_xmlws(c) && c != '=') {
522 in = in - endtag_len;
523 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "whitespace or '='");
524 return LY_EVALID;
525 }
526 in = in - endtag_len;
527 (*name) = id;
528 (*name_len) = in - id;
529
530 /* eat '=' and stop at the value beginning */
531 ign_xmlws(context, in);
532 if (in[0] != '=') {
533 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "'='");
534 return LY_EVALID;
535 }
536 ++in;
537 ign_xmlws(context, in);
538 if (in[0] != '\'' && in[0] != '"') {
539 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "either single or double quotation mark");
540 return LY_EVALID;
541 }
542
543success:
544 /* move caller's input */
545 (*input) = in;
546 return LY_SUCCESS;
547}
548
549/**
550 * @brief Parse input expecting an XML element.
551 *
552 * Able to silently skip comments, PIs and CData. DOCTYPE is not parsable, so it is reported as LY_EVALID error.
553 * If '<' is not found in input, LY_EINVAL is returned (but no error is logged), so it is possible to continue
554 * with parsing input as text content.
555 *
556 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
557 * is returned.
558 *
559 * @param[in] context XML context to track lines or store errors into libyang context.
560 * @param[in,out] input Input string to process, updated according to the processed/read data.
561 * @param[in] options Currently unused options to modify input processing.
562 * @param[out] prefix Pointer to prefix if present in the element name, NULL otherwise.
563 * @param[out] prefix_len Length of the prefix if any.
564 * @param[out] name Element name. LY_SUCCESS can be returned with NULL name only in case the
565 * end of the input string was reached (EOF).
566 * @param[out] name_len Length of the element name.
567 * @return LY_ERR values.
568 */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200569LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200570lyxml_get_element(struct lyxml_context *context, const char **input,
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200571 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
572{
573 struct ly_ctx *ctx = context->ctx; /* shortcut */
574 const char *in = (*input);
575 const char *endtag;
576 const char *sectname;
577 const char *id;
578 size_t endtag_len, newlines;
579 bool loop = true;
580 unsigned int c;
581 LY_ERR rc;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200582
583 /* initialize output variables */
584 (*prefix) = (*name) = NULL;
585 (*prefix_len) = (*name_len) = 0;
586
587 while (loop) {
588 ign_xmlws(context, in);
589
590 if (in[0] == '\0') {
591 /* EOF */
592 goto success;
593 } else if (in[0] != '<') {
594 return LY_EINVAL;
595 }
596 move_input(context, in, 1);
597
598 if (in[0] == '!') {
599 move_input(context, in, 1);
600 /* sections to ignore */
601 if (!strncmp(in, "--", 2)) {
602 /* comment */
603 move_input(context, in, 2);
604 sectname = "Comment";
605 endtag = "-->";
606 endtag_len = 3;
607 } else if (!strncmp(in, "[CDATA[", 7)) {
608 /* CDATA section */
609 move_input(context, in, 7);
610 sectname = "CData";
611 endtag = "]]>";
612 endtag_len = 3;
613 } else if (!strncmp(in, "DOCTYPE", 7)) {
614 /* Document type declaration - not supported */
615 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP, "Document Type Declaration");
616 return LY_EVALID;
617 }
618 in = ign_todelim(in, endtag, endtag_len, &newlines);
619 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, sectname), LY_EVALID);
620 context->line += newlines;
621 in += endtag_len;
622 } else if (in[0] == '?') {
623 in = ign_todelim(in, "?>", 2, &newlines);
624 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
625 context->line += newlines;
626 in += 2;
627 } else {
628 /* element */
629 ign_xmlws(context, in);
630 LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
631
632 /* remember the identifier start before checking its format */
633 id = in;
634 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
635 LY_CHECK_RET(rc);
636 if (c == ':') {
637 /* we have prefixed identifier */
638 endtag = in - endtag_len;
639
640 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
641 LY_CHECK_RET(rc);
642
643 (*prefix) = id;
644 (*prefix_len) = endtag - id;
645 id = endtag + 1;
646 }
647 if (!is_xmlws(c) && c != '/' && c != '>') {
648 in = in - endtag_len;
Radek Krejcid972c252018-09-25 13:23:39 +0200649 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in,
650 "whitespace or element tag termination ('>' or '/>'");
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200651 return LY_EVALID;
652 }
653 in = in - endtag_len;
654 (*name) = id;
655 (*name_len) = in - id;
656
657 loop = false;
658 }
659 }
660
661success:
662 /* move caller's input */
663 (*input) = in;
664 return LY_SUCCESS;
665}
666