blob: 78b259f33493c5d15a6a9d8f4b464fd76717a10d [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 *
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200249 * Mixed content of XML elements is not allowed. Formating whitespaces before child element are ignored,
250 * LY_EINVAL is returned in such a case (buffer is not filled, no error is printed) and input is moved
251 * to the beginning of a child definition.
Radek Krejci7a7fa902018-09-25 17:08:21 +0200252 *
253 * In the case of attribute's values, the input string is expected to start on a quotation mark to
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200254 * select which delimiter (single or double quote) is used. Otherwise, the element content is being
Radek Krejci7a7fa902018-09-25 17:08:21 +0200255 * parsed expected to be terminated by '<' character.
256 *
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200257 * If function succeeds, the string in output buffer is always NULL-terminated.
Radek Krejci7a7fa902018-09-25 17:08:21 +0200258 *
259 * @param[in] context XML context to track lines or store errors into libyang context.
260 * @param[in,out] input Input string to process, updated according to the processed/read data.
261 * @param[out] buffer Storage of the output string. If NULL, the buffer is allocated. Otherwise, the buffer
262 * is used and enlarged when necessary.
263 * @param[out] buffer_size Allocated size of the returned buffer. If a buffer is provided by a caller, it
264 * is not being reduced even if the string is shorter. On the other hand, it can be enlarged if needed.
265 * @return LY_ERR value.
266 */
267LY_ERR
268lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size)
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 */
280 const char *in = (*input);
281 char *buf, delim;
282 size_t offset; /* read offset in input buffer */
283 size_t len; /* write offset in output buffer */
284 size_t size; /* size of the output buffer */
285 void *p;
286 int32_t n;
287 size_t u;
288 bool empty_content = false;
289 LY_ERR rc;
290
291 if (in[0] == '\'') {
292 delim = '\'';
293 ++in;
294 } else if (in[0] == '"') {
295 delim = '"';
296 ++in;
297 } else {
298 delim = '<';
299 empty_content = true;
300 }
301
302 if (empty_content) {
303 /* only when processing element's content - try to ignore whitespaces used to format XML data
304 * before element's child or closing tag */
305 for (offset = 0; in[offset] && is_xmlws(in[offset]); ++offset);
306 LY_CHECK_ERR_RET(!in[offset], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
307 if (in[offset] == '<') {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200308 (*input) = in + offset;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200309 return LY_EINVAL;
310 }
311 } else {
312 /* init */
313 offset = 0;
314 }
315
316 /* prepare output buffer */
317 if (*buffer) {
318 buf = *buffer;
319 size = *buffer_size;
320 } else {
321 buf = malloc(BUFSIZE);
322 size = BUFSIZE;
323
324 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
325 }
326 len = 0;
327
328 /* parse */
329 while (in[offset]) {
330 if (in[offset] == '&') {
331 if (offset) {
332 /* store what we have so far */
333 BUFSIZE_CHECK(ctx, buf, size, len, offset);
334 memcpy(&buf[len], in, offset);
335 len += offset;
336 in += offset;
337 offset = 0;
338 }
339 /* process reference */
340 /* we will need 4 bytes at most since we support only the predefined
341 * (one-char) entities and character references */
342 BUFSIZE_CHECK(ctx, buf, size, len, 4);
343 ++offset;
344 if (in[offset] != '#') {
345 /* entity reference - only predefined references are supported */
346 if (!strncmp(&in[offset], "lt;", 3)) {
347 buf[len++] = '<';
348 in += 4; /* &lt; */
349 } else if (!strncmp(&in[offset], "gt;", 3)) {
350 buf[len++] = '>';
351 in += 4; /* &gt; */
352 } else if (!strncmp(&in[offset], "amp;", 4)) {
353 buf[len++] = '&';
354 in += 5; /* &amp; */
355 } else if (!strncmp(&in[offset], "apos;", 5)) {
356 buf[len++] = '\'';
357 in += 6; /* &apos; */
358 } else if (!strncmp(&in[offset], "quot;", 5)) {
359 buf[len++] = '\"';
360 in += 6; /* &quot; */
361 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200362 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
363 "Entity reference \"%.*s\" not supported, only predefined references allowed.", 10, &in[offset-1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200364 goto error;
365 }
366 offset = 0;
367 } else {
368 p = (void*)&in[offset - 1];
369 /* character reference */
370 ++offset;
371 if (isdigit(in[offset])) {
372 for (n = 0; isdigit(in[offset]); offset++) {
373 n = (10 * n) + (in[offset] - '0');
374 }
375 } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) {
376 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
377 if (isdigit(in[offset])) {
378 u = (in[offset] - '0');
379 } else if (in[offset] > 'F') {
380 u = 10 + (in[offset] - 'a');
381 } else {
382 u = 10 + (in[offset] - 'A');
383 }
384 n = (16 * n) + u;
385 }
386 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200387 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200388 goto error;
389
390 }
391 LY_CHECK_ERR_GOTO(in[offset] != ';',
392 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP,
393 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
394 error);
395 ++offset;
396 rc = lyxml_pututf8(&buf[len], n, &u);
397 LY_CHECK_ERR_GOTO(rc, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
398 "Invalid character reference %.*s (0x%08x).", 12, p, n),
399 error);
400 len += u;
401 in += offset;
402 offset = 0;
403 }
404 } else if (in[offset] == delim) {
405 /* end of string */
406 if (len + offset >= size) {
407 buf = ly_realloc(buf, len + offset + 1);
408 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
409 size = len + offset + 1;
410 }
411 memcpy(&buf[len], in, offset);
412 len += offset;
413 /* in case of element content, keep the leading <,
414 * for attribute's value mova after the terminating quotation mark */
415 if (delim == '<') {
416 in += offset;
417 } else {
418 in += offset + 1;
419 }
420 goto success;
421 } else {
422 /* log lines */
423 if (in[offset] == '\n') {
424 ++context->line;
425 }
426
427 /* continue */
428 ++offset;
429 }
430 }
431 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF);
432error:
433 if (!(*buffer)) {
434 free(buf);
435 }
436 return LY_EVALID;
437
438success:
439 if (!(*buffer) && size != len + 1) {
440 /* not using provided buffer, so fit the allocated buffer to what we really have inside */
441 p = realloc(buf, len + 1);
442 /* ignore realloc fail because we are reducing the buffer,
443 * so just return bigger buffer than needed */
444 if (p) {
445 size = len + 1;
446 buf = p;
447 }
448 }
449 /* set terminating NULL byte */
450 buf[len] = '\0';
451
452 (*input) = in;
453 (*buffer) = buf;
454 (*buffer_size) = size;
455 return LY_SUCCESS;
456
457#undef BUFSIZE
458#undef BUFSIZE_STEP
459#undef BUFSIZE_CHECK
460}
461
462/**
Radek Krejcid972c252018-09-25 13:23:39 +0200463 * @brief Parse input expecting an XML attribute (including XML namespace).
464 *
465 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
466 * is returned.
467 *
468 * In case of a namespace definition, prefix just contains xmlns string. In case of the default namespace,
469 * prefix is NULL and the attribute name is xmlns.
470 *
471 * @param[in] context XML context to track lines or store errors into libyang context.
472 * @param[in,out] input Input string to process, updated according to the processed/read data so,
Radek Krejci7a7fa902018-09-25 17:08:21 +0200473 * when succeeded, it points to the opening quote of the attribute's value.
Radek Krejcid972c252018-09-25 13:23:39 +0200474 * @param[out] prefix Pointer to prefix if present in the attribute name, NULL otherwise.
475 * @param[out] prefix_len Length of the prefix if any.
476 * @param[out] name Attribute name. LY_SUCCESS can be returned with NULL name only in case the
477 * end of the element tag was reached.
478 * @param[out] name_len Length of the element name.
479 * @return LY_ERR values.
480 */
481LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200482lyxml_get_attribute(struct lyxml_context *context, const char **input,
Radek Krejcid972c252018-09-25 13:23:39 +0200483 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
484{
485 struct ly_ctx *ctx = context->ctx; /* shortcut */
486 const char *in = (*input);
487 const char *id;
488 const char *endtag;
489 LY_ERR rc;
490 unsigned int c;
491 size_t endtag_len;
492
493 /* initialize output variables */
494 (*prefix) = (*name) = NULL;
495 (*prefix_len) = (*name_len) = 0;
496
497 /* skip initial whitespaces */
498 ign_xmlws(context, in);
499
500 if (in[0] == '\0') {
501 /* EOF - not expected at this place */
502 return LY_EINVAL;
503 } else if (in[0] == '>' || in[0] == '/') {
504 /* element terminated by > or /> */
505 goto success;
506 }
507
508 /* remember the identifier start before checking its format */
509 id = in;
510 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
511 LY_CHECK_RET(rc);
512 if (c == ':') {
513 /* we have prefixed identifier */
514 endtag = in - endtag_len;
515
516 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
517 LY_CHECK_RET(rc);
518
519 (*prefix) = id;
520 (*prefix_len) = endtag - id;
521 id = endtag + 1;
522 }
523 if (!is_xmlws(c) && c != '=') {
524 in = in - endtag_len;
525 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "whitespace or '='");
526 return LY_EVALID;
527 }
528 in = in - endtag_len;
529 (*name) = id;
530 (*name_len) = in - id;
531
532 /* eat '=' and stop at the value beginning */
533 ign_xmlws(context, in);
534 if (in[0] != '=') {
535 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "'='");
536 return LY_EVALID;
537 }
538 ++in;
539 ign_xmlws(context, in);
540 if (in[0] != '\'' && in[0] != '"') {
541 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "either single or double quotation mark");
542 return LY_EVALID;
543 }
544
545success:
546 /* move caller's input */
547 (*input) = in;
548 return LY_SUCCESS;
549}
550
551/**
552 * @brief Parse input expecting an XML element.
553 *
554 * Able to silently skip comments, PIs and CData. DOCTYPE is not parsable, so it is reported as LY_EVALID error.
555 * If '<' is not found in input, LY_EINVAL is returned (but no error is logged), so it is possible to continue
556 * with parsing input as text content.
557 *
558 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
559 * is returned.
560 *
561 * @param[in] context XML context to track lines or store errors into libyang context.
562 * @param[in,out] input Input string to process, updated according to the processed/read data.
563 * @param[in] options Currently unused options to modify input processing.
564 * @param[out] prefix Pointer to prefix if present in the element name, NULL otherwise.
565 * @param[out] prefix_len Length of the prefix if any.
566 * @param[out] name Element name. LY_SUCCESS can be returned with NULL name only in case the
567 * end of the input string was reached (EOF).
568 * @param[out] name_len Length of the element name.
569 * @return LY_ERR values.
570 */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200571LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200572lyxml_get_element(struct lyxml_context *context, const char **input,
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200573 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
574{
575 struct ly_ctx *ctx = context->ctx; /* shortcut */
576 const char *in = (*input);
577 const char *endtag;
578 const char *sectname;
579 const char *id;
580 size_t endtag_len, newlines;
581 bool loop = true;
582 unsigned int c;
583 LY_ERR rc;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200584
585 /* initialize output variables */
586 (*prefix) = (*name) = NULL;
587 (*prefix_len) = (*name_len) = 0;
588
589 while (loop) {
590 ign_xmlws(context, in);
591
592 if (in[0] == '\0') {
593 /* EOF */
594 goto success;
595 } else if (in[0] != '<') {
596 return LY_EINVAL;
597 }
598 move_input(context, in, 1);
599
600 if (in[0] == '!') {
601 move_input(context, in, 1);
602 /* sections to ignore */
603 if (!strncmp(in, "--", 2)) {
604 /* comment */
605 move_input(context, in, 2);
606 sectname = "Comment";
607 endtag = "-->";
608 endtag_len = 3;
609 } else if (!strncmp(in, "[CDATA[", 7)) {
610 /* CDATA section */
611 move_input(context, in, 7);
612 sectname = "CData";
613 endtag = "]]>";
614 endtag_len = 3;
615 } else if (!strncmp(in, "DOCTYPE", 7)) {
616 /* Document type declaration - not supported */
617 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP, "Document Type Declaration");
618 return LY_EVALID;
619 }
620 in = ign_todelim(in, endtag, endtag_len, &newlines);
621 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, sectname), LY_EVALID);
622 context->line += newlines;
623 in += endtag_len;
624 } else if (in[0] == '?') {
625 in = ign_todelim(in, "?>", 2, &newlines);
626 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
627 context->line += newlines;
628 in += 2;
629 } else {
630 /* element */
631 ign_xmlws(context, in);
632 LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
633
634 /* remember the identifier start before checking its format */
635 id = in;
636 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
637 LY_CHECK_RET(rc);
638 if (c == ':') {
639 /* we have prefixed identifier */
640 endtag = in - endtag_len;
641
642 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
643 LY_CHECK_RET(rc);
644
645 (*prefix) = id;
646 (*prefix_len) = endtag - id;
647 id = endtag + 1;
648 }
649 if (!is_xmlws(c) && c != '/' && c != '>') {
650 in = in - endtag_len;
Radek Krejcid972c252018-09-25 13:23:39 +0200651 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in,
652 "whitespace or element tag termination ('>' or '/>'");
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200653 return LY_EVALID;
654 }
655 in = in - endtag_len;
656 (*name) = id;
657 (*name_len) = in - id;
658
659 loop = false;
660 }
661 }
662
663success:
664 /* move caller's input */
665 (*input) = in;
666 return LY_SUCCESS;
667}
668