blob: bd26f1676f05a0c8ef9a1a63847a3f72394a66fb [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
Radek Krejci117d2082018-09-26 10:05:14 +0200171lyxml_pututf8(char *dst, uint32_t value, size_t *bytes_written)
Radek Krejci7a7fa902018-09-25 17:08:21 +0200172{
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;
Radek Krejci117d2082018-09-26 10:05:14 +0200286 uint32_t n;
287 size_t u, newlines;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200288 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 */
Radek Krejci117d2082018-09-26 10:05:14 +0200305 for (offset = newlines = 0; in[offset] && is_xmlws(in[offset]); ++offset) {
306 if (in[offset] == '\n') {
307 ++newlines;
308 }
309 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200310 LY_CHECK_ERR_RET(!in[offset], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
Radek Krejci117d2082018-09-26 10:05:14 +0200311 context->line += newlines;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200312 if (in[offset] == '<') {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200313 (*input) = in + offset;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200314 return LY_EINVAL;
315 }
316 } else {
317 /* init */
318 offset = 0;
319 }
320
321 /* prepare output buffer */
322 if (*buffer) {
323 buf = *buffer;
324 size = *buffer_size;
325 } else {
326 buf = malloc(BUFSIZE);
327 size = BUFSIZE;
328
329 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
330 }
331 len = 0;
332
333 /* parse */
334 while (in[offset]) {
335 if (in[offset] == '&') {
336 if (offset) {
337 /* store what we have so far */
338 BUFSIZE_CHECK(ctx, buf, size, len, offset);
339 memcpy(&buf[len], in, offset);
340 len += offset;
341 in += offset;
342 offset = 0;
343 }
344 /* process reference */
345 /* we will need 4 bytes at most since we support only the predefined
346 * (one-char) entities and character references */
347 BUFSIZE_CHECK(ctx, buf, size, len, 4);
348 ++offset;
349 if (in[offset] != '#') {
350 /* entity reference - only predefined references are supported */
351 if (!strncmp(&in[offset], "lt;", 3)) {
352 buf[len++] = '<';
353 in += 4; /* &lt; */
354 } else if (!strncmp(&in[offset], "gt;", 3)) {
355 buf[len++] = '>';
356 in += 4; /* &gt; */
357 } else if (!strncmp(&in[offset], "amp;", 4)) {
358 buf[len++] = '&';
359 in += 5; /* &amp; */
360 } else if (!strncmp(&in[offset], "apos;", 5)) {
361 buf[len++] = '\'';
362 in += 6; /* &apos; */
363 } else if (!strncmp(&in[offset], "quot;", 5)) {
364 buf[len++] = '\"';
365 in += 6; /* &quot; */
366 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200367 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
368 "Entity reference \"%.*s\" not supported, only predefined references allowed.", 10, &in[offset-1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200369 goto error;
370 }
371 offset = 0;
372 } else {
373 p = (void*)&in[offset - 1];
374 /* character reference */
375 ++offset;
376 if (isdigit(in[offset])) {
377 for (n = 0; isdigit(in[offset]); offset++) {
378 n = (10 * n) + (in[offset] - '0');
379 }
380 } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) {
381 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
382 if (isdigit(in[offset])) {
383 u = (in[offset] - '0');
384 } else if (in[offset] > 'F') {
385 u = 10 + (in[offset] - 'a');
386 } else {
387 u = 10 + (in[offset] - 'A');
388 }
389 n = (16 * n) + u;
390 }
391 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200392 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200393 goto error;
394
395 }
396 LY_CHECK_ERR_GOTO(in[offset] != ';',
397 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP,
398 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
399 error);
400 ++offset;
401 rc = lyxml_pututf8(&buf[len], n, &u);
402 LY_CHECK_ERR_GOTO(rc, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
Radek Krejci117d2082018-09-26 10:05:14 +0200403 "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200404 error);
405 len += u;
406 in += offset;
407 offset = 0;
408 }
409 } else if (in[offset] == delim) {
410 /* end of string */
411 if (len + offset >= size) {
412 buf = ly_realloc(buf, len + offset + 1);
413 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
414 size = len + offset + 1;
415 }
416 memcpy(&buf[len], in, offset);
417 len += offset;
418 /* in case of element content, keep the leading <,
419 * for attribute's value mova after the terminating quotation mark */
420 if (delim == '<') {
421 in += offset;
422 } else {
423 in += offset + 1;
424 }
425 goto success;
426 } else {
427 /* log lines */
428 if (in[offset] == '\n') {
429 ++context->line;
430 }
431
432 /* continue */
433 ++offset;
434 }
435 }
436 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF);
437error:
438 if (!(*buffer)) {
439 free(buf);
440 }
441 return LY_EVALID;
442
443success:
444 if (!(*buffer) && size != len + 1) {
445 /* not using provided buffer, so fit the allocated buffer to what we really have inside */
446 p = realloc(buf, len + 1);
447 /* ignore realloc fail because we are reducing the buffer,
448 * so just return bigger buffer than needed */
449 if (p) {
450 size = len + 1;
451 buf = p;
452 }
453 }
454 /* set terminating NULL byte */
455 buf[len] = '\0';
456
457 (*input) = in;
458 (*buffer) = buf;
459 (*buffer_size) = size;
460 return LY_SUCCESS;
461
462#undef BUFSIZE
463#undef BUFSIZE_STEP
464#undef BUFSIZE_CHECK
465}
466
467/**
Radek Krejcid972c252018-09-25 13:23:39 +0200468 * @brief Parse input expecting an XML attribute (including XML namespace).
469 *
470 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
471 * is returned.
472 *
473 * In case of a namespace definition, prefix just contains xmlns string. In case of the default namespace,
474 * prefix is NULL and the attribute name is xmlns.
475 *
476 * @param[in] context XML context to track lines or store errors into libyang context.
477 * @param[in,out] input Input string to process, updated according to the processed/read data so,
Radek Krejci7a7fa902018-09-25 17:08:21 +0200478 * when succeeded, it points to the opening quote of the attribute's value.
Radek Krejcid972c252018-09-25 13:23:39 +0200479 * @param[out] prefix Pointer to prefix if present in the attribute name, NULL otherwise.
480 * @param[out] prefix_len Length of the prefix if any.
481 * @param[out] name Attribute name. LY_SUCCESS can be returned with NULL name only in case the
482 * end of the element tag was reached.
483 * @param[out] name_len Length of the element name.
484 * @return LY_ERR values.
485 */
486LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200487lyxml_get_attribute(struct lyxml_context *context, const char **input,
Radek Krejcid972c252018-09-25 13:23:39 +0200488 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
489{
490 struct ly_ctx *ctx = context->ctx; /* shortcut */
491 const char *in = (*input);
492 const char *id;
493 const char *endtag;
494 LY_ERR rc;
495 unsigned int c;
496 size_t endtag_len;
497
498 /* initialize output variables */
499 (*prefix) = (*name) = NULL;
500 (*prefix_len) = (*name_len) = 0;
501
502 /* skip initial whitespaces */
503 ign_xmlws(context, in);
504
505 if (in[0] == '\0') {
506 /* EOF - not expected at this place */
507 return LY_EINVAL;
508 } else if (in[0] == '>' || in[0] == '/') {
509 /* element terminated by > or /> */
510 goto success;
511 }
512
513 /* remember the identifier start before checking its format */
514 id = in;
515 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
516 LY_CHECK_RET(rc);
517 if (c == ':') {
518 /* we have prefixed identifier */
519 endtag = in - endtag_len;
520
521 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
522 LY_CHECK_RET(rc);
523
524 (*prefix) = id;
525 (*prefix_len) = endtag - id;
526 id = endtag + 1;
527 }
528 if (!is_xmlws(c) && c != '=') {
529 in = in - endtag_len;
530 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "whitespace or '='");
531 return LY_EVALID;
532 }
533 in = in - endtag_len;
534 (*name) = id;
535 (*name_len) = in - id;
536
537 /* eat '=' and stop at the value beginning */
538 ign_xmlws(context, in);
539 if (in[0] != '=') {
540 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "'='");
541 return LY_EVALID;
542 }
543 ++in;
544 ign_xmlws(context, in);
545 if (in[0] != '\'' && in[0] != '"') {
546 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "either single or double quotation mark");
547 return LY_EVALID;
548 }
549
550success:
551 /* move caller's input */
552 (*input) = in;
553 return LY_SUCCESS;
554}
555
556/**
557 * @brief Parse input expecting an XML element.
558 *
559 * Able to silently skip comments, PIs and CData. DOCTYPE is not parsable, so it is reported as LY_EVALID error.
560 * If '<' is not found in input, LY_EINVAL is returned (but no error is logged), so it is possible to continue
561 * with parsing input as text content.
562 *
563 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
564 * is returned.
565 *
566 * @param[in] context XML context to track lines or store errors into libyang context.
567 * @param[in,out] input Input string to process, updated according to the processed/read data.
568 * @param[in] options Currently unused options to modify input processing.
569 * @param[out] prefix Pointer to prefix if present in the element name, NULL otherwise.
570 * @param[out] prefix_len Length of the prefix if any.
571 * @param[out] name Element name. LY_SUCCESS can be returned with NULL name only in case the
572 * end of the input string was reached (EOF).
573 * @param[out] name_len Length of the element name.
574 * @return LY_ERR values.
575 */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200576LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200577lyxml_get_element(struct lyxml_context *context, const char **input,
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200578 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
579{
580 struct ly_ctx *ctx = context->ctx; /* shortcut */
581 const char *in = (*input);
582 const char *endtag;
583 const char *sectname;
584 const char *id;
585 size_t endtag_len, newlines;
586 bool loop = true;
587 unsigned int c;
588 LY_ERR rc;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200589
590 /* initialize output variables */
591 (*prefix) = (*name) = NULL;
592 (*prefix_len) = (*name_len) = 0;
593
594 while (loop) {
595 ign_xmlws(context, in);
596
597 if (in[0] == '\0') {
598 /* EOF */
599 goto success;
600 } else if (in[0] != '<') {
601 return LY_EINVAL;
602 }
603 move_input(context, in, 1);
604
605 if (in[0] == '!') {
606 move_input(context, in, 1);
607 /* sections to ignore */
608 if (!strncmp(in, "--", 2)) {
609 /* comment */
610 move_input(context, in, 2);
611 sectname = "Comment";
612 endtag = "-->";
613 endtag_len = 3;
614 } else if (!strncmp(in, "[CDATA[", 7)) {
615 /* CDATA section */
616 move_input(context, in, 7);
617 sectname = "CData";
618 endtag = "]]>";
619 endtag_len = 3;
620 } else if (!strncmp(in, "DOCTYPE", 7)) {
621 /* Document type declaration - not supported */
622 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP, "Document Type Declaration");
623 return LY_EVALID;
624 }
625 in = ign_todelim(in, endtag, endtag_len, &newlines);
626 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, sectname), LY_EVALID);
627 context->line += newlines;
628 in += endtag_len;
629 } else if (in[0] == '?') {
630 in = ign_todelim(in, "?>", 2, &newlines);
631 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
632 context->line += newlines;
633 in += 2;
634 } else {
635 /* element */
636 ign_xmlws(context, in);
637 LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
638
639 /* remember the identifier start before checking its format */
640 id = in;
641 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
642 LY_CHECK_RET(rc);
643 if (c == ':') {
644 /* we have prefixed identifier */
645 endtag = in - endtag_len;
646
647 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
648 LY_CHECK_RET(rc);
649
650 (*prefix) = id;
651 (*prefix_len) = endtag - id;
652 id = endtag + 1;
653 }
654 if (!is_xmlws(c) && c != '/' && c != '>') {
655 in = in - endtag_len;
Radek Krejcid972c252018-09-25 13:23:39 +0200656 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in,
657 "whitespace or element tag termination ('>' or '/>'");
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200658 return LY_EVALID;
659 }
660 in = in - endtag_len;
661 (*name) = id;
662 (*name_len) = in - id;
663
664 loop = false;
665 }
666 }
667
668success:
669 /* move caller's input */
670 (*input) = in;
671 return LY_SUCCESS;
672}
673