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