blob: fe1f2cd4ac86a72603e1fd8c7818fbfba479ffe3 [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/**
Radek Krejci7a7fa902018-09-25 17:08:21 +020092 * Store UTF-8 character specified as 4byte integer into the dst buffer.
93 * Returns number of written bytes (4 max), expects that dst has enough space.
94 *
95 * UTF-8 mapping:
96 * 00000000 -- 0000007F: 0xxxxxxx
97 * 00000080 -- 000007FF: 110xxxxx 10xxxxxx
98 * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
99 * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
100 *
101 * Includes checking for valid characters (following RFC 7950, sec 9.4)
102 */
103static LY_ERR
Radek Krejci117d2082018-09-26 10:05:14 +0200104lyxml_pututf8(char *dst, uint32_t value, size_t *bytes_written)
Radek Krejci7a7fa902018-09-25 17:08:21 +0200105{
106 if (value < 0x80) {
107 /* one byte character */
108 if (value < 0x20 &&
109 value != 0x09 &&
110 value != 0x0a &&
111 value != 0x0d) {
112 return LY_EINVAL;
113 }
114
115 dst[0] = value;
116 (*bytes_written) = 1;
117 } else if (value < 0x800) {
118 /* two bytes character */
119 dst[0] = 0xc0 | (value >> 6);
120 dst[1] = 0x80 | (value & 0x3f);
121 (*bytes_written) = 2;
122 } else if (value < 0xfffe) {
123 /* three bytes character */
124 if (((value & 0xf800) == 0xd800) ||
125 (value >= 0xfdd0 && value <= 0xfdef)) {
126 /* exclude surrogate blocks %xD800-DFFF */
127 /* exclude noncharacters %xFDD0-FDEF */
128 return LY_EINVAL;
129 }
130
131 dst[0] = 0xe0 | (value >> 12);
132 dst[1] = 0x80 | ((value >> 6) & 0x3f);
133 dst[2] = 0x80 | (value & 0x3f);
134
135 (*bytes_written) = 3;
136 } else if (value < 0x10fffe) {
137 if ((value & 0xffe) == 0xffe) {
138 /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF,
139 * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF,
140 * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */
141 return LY_EINVAL;
142 }
143 /* four bytes character */
144 dst[0] = 0xf0 | (value >> 18);
145 dst[1] = 0x80 | ((value >> 12) & 0x3f);
146 dst[2] = 0x80 | ((value >> 6) & 0x3f);
147 dst[3] = 0x80 | (value & 0x3f);
148
149 (*bytes_written) = 4;
150 }
151 return LY_SUCCESS;
152}
153
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200154/**
155 * @brief Check/Get an XML qualified name from the input string.
156 *
157 * The identifier must have at least one valid character complying the name start character constraints.
158 * The identifier is terminated by the first character, which does not comply to the name character constraints.
159 *
160 * See https://www.w3.org/TR/xml-names/#NT-NCName
161 *
162 * @param[in] context XML context to track lines or store errors into libyang context.
163 * @param[in,out] input Input string to process, updated according to the processed/read data.
164 * Note that the term_char is also read, so input points after the term_char at the end.
165 * @param[out] term_char The first character in the input string which does not compy to the name constraints.
166 * @param[out] term_char_len Number of bytes used to encode UTF8 term_char. Serves to be able to go back in input string.
167 * @return LY_ERR value.
168 */
169static LY_ERR
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200170lyxml_check_qname(struct lyxml_context *context, const char **input, unsigned int *term_char, size_t *term_char_len)
171{
172 unsigned int c;
173 const char *id = (*input);
174 LY_ERR rc;
175
176 /* check NameStartChar (minus colon) */
Radek Krejcib416be62018-10-01 14:51:45 +0200177 LY_CHECK_ERR_RET(ly_getutf8(input, &c, NULL) != LY_SUCCESS,
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200178 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID);
179 LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c),
180 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
181 "Identifier \"%s\" starts with invalid character.", id),
182 LY_EVALID);
183
184 /* check rest of the identifier */
Radek Krejcib416be62018-10-01 14:51:45 +0200185 for (rc = ly_getutf8(input, &c, term_char_len);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200186 rc == LY_SUCCESS && is_xmlqnamechar(c);
Radek Krejcib416be62018-10-01 14:51:45 +0200187 rc = ly_getutf8(input, &c, term_char_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200188 LY_CHECK_ERR_RET(rc != LY_SUCCESS, LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID);
189
190 (*term_char) = c;
191 return LY_SUCCESS;
192}
193
Radek Krejci7a7fa902018-09-25 17:08:21 +0200194LY_ERR
195lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size)
196{
197#define BUFSIZE 4096
198#define BUFSIZE_STEP 4096
199#define BUFSIZE_CHECK(CTX, BUF, SIZE, CURR, NEED) \
200 if (CURR+NEED >= SIZE) { \
201 BUF = ly_realloc(BUF, SIZE + BUFSIZE_STEP); \
202 LY_CHECK_ERR_RET(!BUF, LOGMEM(CTX), LY_EMEM); \
203 SIZE += BUFSIZE_STEP; \
204 }
205
206 struct ly_ctx *ctx = context->ctx; /* shortcut */
207 const char *in = (*input);
208 char *buf, delim;
209 size_t offset; /* read offset in input buffer */
210 size_t len; /* write offset in output buffer */
211 size_t size; /* size of the output buffer */
212 void *p;
Radek Krejci117d2082018-09-26 10:05:14 +0200213 uint32_t n;
214 size_t u, newlines;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200215 bool empty_content = false;
216 LY_ERR rc;
217
218 if (in[0] == '\'') {
219 delim = '\'';
220 ++in;
221 } else if (in[0] == '"') {
222 delim = '"';
223 ++in;
224 } else {
225 delim = '<';
226 empty_content = true;
227 }
228
229 if (empty_content) {
230 /* only when processing element's content - try to ignore whitespaces used to format XML data
231 * before element's child or closing tag */
Radek Krejci117d2082018-09-26 10:05:14 +0200232 for (offset = newlines = 0; in[offset] && is_xmlws(in[offset]); ++offset) {
233 if (in[offset] == '\n') {
234 ++newlines;
235 }
236 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200237 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 +0200238 context->line += newlines;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200239 if (in[offset] == '<') {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200240 (*input) = in + offset;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200241 return LY_EINVAL;
242 }
243 } else {
244 /* init */
245 offset = 0;
246 }
247
248 /* prepare output buffer */
249 if (*buffer) {
250 buf = *buffer;
251 size = *buffer_size;
252 } else {
253 buf = malloc(BUFSIZE);
254 size = BUFSIZE;
255
256 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
257 }
258 len = 0;
259
260 /* parse */
261 while (in[offset]) {
262 if (in[offset] == '&') {
263 if (offset) {
264 /* store what we have so far */
265 BUFSIZE_CHECK(ctx, buf, size, len, offset);
266 memcpy(&buf[len], in, offset);
267 len += offset;
268 in += offset;
269 offset = 0;
270 }
271 /* process reference */
272 /* we will need 4 bytes at most since we support only the predefined
273 * (one-char) entities and character references */
274 BUFSIZE_CHECK(ctx, buf, size, len, 4);
275 ++offset;
276 if (in[offset] != '#') {
277 /* entity reference - only predefined references are supported */
278 if (!strncmp(&in[offset], "lt;", 3)) {
279 buf[len++] = '<';
280 in += 4; /* &lt; */
281 } else if (!strncmp(&in[offset], "gt;", 3)) {
282 buf[len++] = '>';
283 in += 4; /* &gt; */
284 } else if (!strncmp(&in[offset], "amp;", 4)) {
285 buf[len++] = '&';
286 in += 5; /* &amp; */
287 } else if (!strncmp(&in[offset], "apos;", 5)) {
288 buf[len++] = '\'';
289 in += 6; /* &apos; */
290 } else if (!strncmp(&in[offset], "quot;", 5)) {
291 buf[len++] = '\"';
292 in += 6; /* &quot; */
293 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200294 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
295 "Entity reference \"%.*s\" not supported, only predefined references allowed.", 10, &in[offset-1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200296 goto error;
297 }
298 offset = 0;
299 } else {
300 p = (void*)&in[offset - 1];
301 /* character reference */
302 ++offset;
303 if (isdigit(in[offset])) {
304 for (n = 0; isdigit(in[offset]); offset++) {
305 n = (10 * n) + (in[offset] - '0');
306 }
307 } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) {
308 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
309 if (isdigit(in[offset])) {
310 u = (in[offset] - '0');
311 } else if (in[offset] > 'F') {
312 u = 10 + (in[offset] - 'a');
313 } else {
314 u = 10 + (in[offset] - 'A');
315 }
316 n = (16 * n) + u;
317 }
318 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200319 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200320 goto error;
321
322 }
323 LY_CHECK_ERR_GOTO(in[offset] != ';',
324 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP,
325 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
326 error);
327 ++offset;
328 rc = lyxml_pututf8(&buf[len], n, &u);
329 LY_CHECK_ERR_GOTO(rc, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
Radek Krejci117d2082018-09-26 10:05:14 +0200330 "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200331 error);
332 len += u;
333 in += offset;
334 offset = 0;
335 }
336 } else if (in[offset] == delim) {
337 /* end of string */
338 if (len + offset >= size) {
339 buf = ly_realloc(buf, len + offset + 1);
340 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
341 size = len + offset + 1;
342 }
343 memcpy(&buf[len], in, offset);
344 len += offset;
345 /* in case of element content, keep the leading <,
346 * for attribute's value mova after the terminating quotation mark */
347 if (delim == '<') {
348 in += offset;
349 } else {
350 in += offset + 1;
351 }
352 goto success;
353 } else {
354 /* log lines */
355 if (in[offset] == '\n') {
356 ++context->line;
357 }
358
359 /* continue */
360 ++offset;
361 }
362 }
363 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF);
364error:
365 if (!(*buffer)) {
366 free(buf);
367 }
368 return LY_EVALID;
369
370success:
371 if (!(*buffer) && size != len + 1) {
372 /* not using provided buffer, so fit the allocated buffer to what we really have inside */
373 p = realloc(buf, len + 1);
374 /* ignore realloc fail because we are reducing the buffer,
375 * so just return bigger buffer than needed */
376 if (p) {
377 size = len + 1;
378 buf = p;
379 }
380 }
381 /* set terminating NULL byte */
382 buf[len] = '\0';
383
384 (*input) = in;
385 (*buffer) = buf;
386 (*buffer_size) = size;
387 return LY_SUCCESS;
388
389#undef BUFSIZE
390#undef BUFSIZE_STEP
391#undef BUFSIZE_CHECK
392}
393
Radek Krejcid972c252018-09-25 13:23:39 +0200394LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200395lyxml_get_attribute(struct lyxml_context *context, const char **input,
Radek Krejcid972c252018-09-25 13:23:39 +0200396 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
397{
398 struct ly_ctx *ctx = context->ctx; /* shortcut */
399 const char *in = (*input);
400 const char *id;
401 const char *endtag;
402 LY_ERR rc;
403 unsigned int c;
404 size_t endtag_len;
405
406 /* initialize output variables */
407 (*prefix) = (*name) = NULL;
408 (*prefix_len) = (*name_len) = 0;
409
410 /* skip initial whitespaces */
411 ign_xmlws(context, in);
412
413 if (in[0] == '\0') {
414 /* EOF - not expected at this place */
415 return LY_EINVAL;
416 } else if (in[0] == '>' || in[0] == '/') {
417 /* element terminated by > or /> */
418 goto success;
419 }
420
421 /* remember the identifier start before checking its format */
422 id = in;
423 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
424 LY_CHECK_RET(rc);
425 if (c == ':') {
426 /* we have prefixed identifier */
427 endtag = in - endtag_len;
428
429 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
430 LY_CHECK_RET(rc);
431
432 (*prefix) = id;
433 (*prefix_len) = endtag - id;
434 id = endtag + 1;
435 }
436 if (!is_xmlws(c) && c != '=') {
437 in = in - endtag_len;
438 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "whitespace or '='");
439 return LY_EVALID;
440 }
441 in = in - endtag_len;
442 (*name) = id;
443 (*name_len) = in - id;
444
445 /* eat '=' and stop at the value beginning */
446 ign_xmlws(context, in);
447 if (in[0] != '=') {
448 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "'='");
449 return LY_EVALID;
450 }
451 ++in;
452 ign_xmlws(context, in);
453 if (in[0] != '\'' && in[0] != '"') {
454 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "either single or double quotation mark");
455 return LY_EVALID;
456 }
457
458success:
459 /* move caller's input */
460 (*input) = in;
461 return LY_SUCCESS;
462}
463
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200464LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200465lyxml_get_element(struct lyxml_context *context, const char **input,
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200466 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
467{
468 struct ly_ctx *ctx = context->ctx; /* shortcut */
469 const char *in = (*input);
470 const char *endtag;
471 const char *sectname;
472 const char *id;
473 size_t endtag_len, newlines;
474 bool loop = true;
475 unsigned int c;
476 LY_ERR rc;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200477
478 /* initialize output variables */
479 (*prefix) = (*name) = NULL;
480 (*prefix_len) = (*name_len) = 0;
481
482 while (loop) {
483 ign_xmlws(context, in);
484
485 if (in[0] == '\0') {
486 /* EOF */
487 goto success;
488 } else if (in[0] != '<') {
489 return LY_EINVAL;
490 }
491 move_input(context, in, 1);
492
493 if (in[0] == '!') {
494 move_input(context, in, 1);
495 /* sections to ignore */
496 if (!strncmp(in, "--", 2)) {
497 /* comment */
498 move_input(context, in, 2);
499 sectname = "Comment";
500 endtag = "-->";
501 endtag_len = 3;
502 } else if (!strncmp(in, "[CDATA[", 7)) {
503 /* CDATA section */
504 move_input(context, in, 7);
505 sectname = "CData";
506 endtag = "]]>";
507 endtag_len = 3;
508 } else if (!strncmp(in, "DOCTYPE", 7)) {
509 /* Document type declaration - not supported */
510 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP, "Document Type Declaration");
511 return LY_EVALID;
512 }
513 in = ign_todelim(in, endtag, endtag_len, &newlines);
514 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, sectname), LY_EVALID);
515 context->line += newlines;
516 in += endtag_len;
517 } else if (in[0] == '?') {
518 in = ign_todelim(in, "?>", 2, &newlines);
519 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
520 context->line += newlines;
521 in += 2;
522 } else {
523 /* element */
524 ign_xmlws(context, in);
525 LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
526
527 /* remember the identifier start before checking its format */
528 id = in;
529 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
530 LY_CHECK_RET(rc);
531 if (c == ':') {
532 /* we have prefixed identifier */
533 endtag = in - endtag_len;
534
535 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
536 LY_CHECK_RET(rc);
537
538 (*prefix) = id;
539 (*prefix_len) = endtag - id;
540 id = endtag + 1;
541 }
542 if (!is_xmlws(c) && c != '/' && c != '>') {
543 in = in - endtag_len;
Radek Krejcid972c252018-09-25 13:23:39 +0200544 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in,
545 "whitespace or element tag termination ('>' or '/>'");
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200546 return LY_EVALID;
547 }
548 in = in - endtag_len;
549 (*name) = id;
550 (*name_len) = in - id;
551
552 loop = false;
553 }
554 }
555
556success:
557 /* move caller's input */
558 (*input) = in;
559 return LY_SUCCESS;
560}
561
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200562LY_ERR
563lyxml_ns_add(struct lyxml_context *context, const char *element_name, const char *prefix, size_t prefix_len, char *uri)
564{
565 struct lyxml_ns *ns;
566
567 ns = malloc(sizeof *ns);
568 LY_CHECK_ERR_RET(!ns, LOGMEM(context->ctx), LY_EMEM);
569
570 ns->element = element_name;
571 ns->uri = uri;
572 if (prefix) {
573 ns->prefix = strndup(prefix, prefix_len);
574 LY_CHECK_ERR_RET(!ns->prefix, LOGMEM(context->ctx); free(ns), LY_EMEM);
575 } else {
576 ns->prefix = NULL;
577 }
578
579 LY_CHECK_ERR_RET(ly_set_add(&context->ns, ns, LY_SET_OPT_USEASLIST) == -1, free(ns->prefix), LY_EMEM);
580 return LY_SUCCESS;
581}
582
583const struct lyxml_ns *
584lyxml_ns_get(struct lyxml_context *context, const char *prefix, size_t prefix_len)
585{
586 unsigned int u;
587 struct lyxml_ns *ns;
588
589 for (u = context->ns.count - 1; u + 1 > 0; --u) {
590 ns = (struct lyxml_ns *)context->ns.objs[u];
591 if (prefix) {
592 if (!strncmp(prefix, ns->prefix, prefix_len) && ns->prefix[prefix_len] == '\0') {
593 return ns;
594 }
595 } else if (!ns->prefix) {
596 /* default namespace */
597 return ns;
598 }
599 }
600
601 return NULL;
602}
603
604LY_ERR
605lyxml_ns_rm(struct lyxml_context *context, const char *element_name)
606{
607 unsigned int u;
608
609 for (u = context->ns.count - 1; u + 1 > 0; --u) {
610 if (((struct lyxml_ns *)context->ns.objs[u])->element != element_name) {
611 /* we are done, the namespaces from a single element are supposed to be together */
612 break;
613 }
614 /* remove the ns structure */
615 free(((struct lyxml_ns *)context->ns.objs[u])->prefix);
616 free(((struct lyxml_ns *)context->ns.objs[u])->uri);
617 free(context->ns.objs[u]);
618 --context->ns.count;
619 }
620
621 if (!context->ns.count) {
622 /* cleanup the context's namespaces storage */
623 ly_set_erase(&context->ns, NULL);
624 }
625
626 return LY_SUCCESS;
627}