blob: 9de0565efd9fac23d83d850be0c6694add4406a1 [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 Krejcic1c03d62018-11-27 10:52:43 +010015#include "common.h"
Radek Krejci4b74d5e2018-09-26 14:30:55 +020016
Radek Krejcib1890642018-10-03 14:05:40 +020017#include <assert.h>
Radek Krejci7a7fa902018-09-25 17:08:21 +020018#include <ctype.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020019#include <stdbool.h>
20#include <stdint.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <stdlib.h>
Radek Krejci4b74d5e2018-09-26 14:30:55 +020022#include <string.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020023
Radek Krejcid91dbaf2018-09-21 15:51:39 +020024#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "printer_internal.h"
Radek Krejcid91dbaf2018-09-21 15:51:39 +020026
Radek Krejcid91dbaf2018-09-21 15:51:39 +020027/* Move input p by s characters, if EOF log with lyxml_context c */
28#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)
29
Radek Krejcib1890642018-10-03 14:05:40 +020030/* Ignore whitespaces in the input string p */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020031#define ign_xmlws(c,p) while (is_xmlws(*(p))) {if (*(p) == '\n') {++c->line;} ++p;}
32
Radek Krejci4b74d5e2018-09-26 14:30:55 +020033/**
34 * @brief Ignore any characters until the delim of the size delim_len is read
35 *
36 * Detects number of read new lines.
37 * Returns the pointer to the beginning of the detected delim, or NULL in case the delim not found in
38 * NULL-terminated input string.
39 * */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020040static const char *
41ign_todelim(register const char *input, const char *delim, size_t delim_len, size_t *newlines)
42{
43 size_t i;
44 register const char *a, *b;
45
46 (*newlines) = 0;
47 for ( ; *input; ++input) {
48 if (*input != *delim) {
49 if (*input == '\n') {
50 ++(*newlines);
51 }
52 continue;
53 }
54 a = input;
55 b = delim;
56 for (i = 0; i < delim_len; ++i) {
57 if (*a++ != *b++) {
58 break;
59 }
60 }
61 if (i == delim_len) {
62 return input;
63 }
64 }
65 return NULL;
66}
67
Radek Krejci4b74d5e2018-09-26 14:30:55 +020068/**
Radek Krejci7a7fa902018-09-25 17:08:21 +020069 * Store UTF-8 character specified as 4byte integer into the dst buffer.
70 * Returns number of written bytes (4 max), expects that dst has enough space.
71 *
72 * UTF-8 mapping:
73 * 00000000 -- 0000007F: 0xxxxxxx
74 * 00000080 -- 000007FF: 110xxxxx 10xxxxxx
75 * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
76 * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
77 *
78 * Includes checking for valid characters (following RFC 7950, sec 9.4)
79 */
80static LY_ERR
Radek Krejci117d2082018-09-26 10:05:14 +020081lyxml_pututf8(char *dst, uint32_t value, size_t *bytes_written)
Radek Krejci7a7fa902018-09-25 17:08:21 +020082{
83 if (value < 0x80) {
84 /* one byte character */
85 if (value < 0x20 &&
86 value != 0x09 &&
87 value != 0x0a &&
88 value != 0x0d) {
89 return LY_EINVAL;
90 }
91
92 dst[0] = value;
93 (*bytes_written) = 1;
94 } else if (value < 0x800) {
95 /* two bytes character */
96 dst[0] = 0xc0 | (value >> 6);
97 dst[1] = 0x80 | (value & 0x3f);
98 (*bytes_written) = 2;
99 } else if (value < 0xfffe) {
100 /* three bytes character */
101 if (((value & 0xf800) == 0xd800) ||
102 (value >= 0xfdd0 && value <= 0xfdef)) {
103 /* exclude surrogate blocks %xD800-DFFF */
104 /* exclude noncharacters %xFDD0-FDEF */
105 return LY_EINVAL;
106 }
107
108 dst[0] = 0xe0 | (value >> 12);
109 dst[1] = 0x80 | ((value >> 6) & 0x3f);
110 dst[2] = 0x80 | (value & 0x3f);
111
112 (*bytes_written) = 3;
113 } else if (value < 0x10fffe) {
114 if ((value & 0xffe) == 0xffe) {
115 /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF,
116 * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF,
117 * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */
118 return LY_EINVAL;
119 }
120 /* four bytes character */
121 dst[0] = 0xf0 | (value >> 18);
122 dst[1] = 0x80 | ((value >> 12) & 0x3f);
123 dst[2] = 0x80 | ((value >> 6) & 0x3f);
124 dst[3] = 0x80 | (value & 0x3f);
125
126 (*bytes_written) = 4;
127 }
128 return LY_SUCCESS;
129}
130
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200131/**
132 * @brief Check/Get an XML qualified name from the input string.
133 *
134 * The identifier must have at least one valid character complying the name start character constraints.
135 * The identifier is terminated by the first character, which does not comply to the name character constraints.
136 *
137 * See https://www.w3.org/TR/xml-names/#NT-NCName
138 *
139 * @param[in] context XML context to track lines or store errors into libyang context.
140 * @param[in,out] input Input string to process, updated according to the processed/read data.
141 * Note that the term_char is also read, so input points after the term_char at the end.
142 * @param[out] term_char The first character in the input string which does not compy to the name constraints.
143 * @param[out] term_char_len Number of bytes used to encode UTF8 term_char. Serves to be able to go back in input string.
144 * @return LY_ERR value.
145 */
146static LY_ERR
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200147lyxml_check_qname(struct lyxml_context *context, const char **input, unsigned int *term_char, size_t *term_char_len)
148{
149 unsigned int c;
150 const char *id = (*input);
151 LY_ERR rc;
152
153 /* check NameStartChar (minus colon) */
Radek Krejcib416be62018-10-01 14:51:45 +0200154 LY_CHECK_ERR_RET(ly_getutf8(input, &c, NULL) != LY_SUCCESS,
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200155 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID);
156 LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c),
157 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
158 "Identifier \"%s\" starts with invalid character.", id),
159 LY_EVALID);
160
161 /* check rest of the identifier */
Radek Krejcib416be62018-10-01 14:51:45 +0200162 for (rc = ly_getutf8(input, &c, term_char_len);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200163 rc == LY_SUCCESS && is_xmlqnamechar(c);
Radek Krejcib416be62018-10-01 14:51:45 +0200164 rc = ly_getutf8(input, &c, term_char_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200165 LY_CHECK_ERR_RET(rc != LY_SUCCESS, LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID);
166
167 (*term_char) = c;
168 return LY_SUCCESS;
169}
170
Radek Krejci17a78d82019-05-15 15:49:55 +0200171/**
172 * @brief Add namespace definition into XML context.
173 *
174 * Namespaces from a single element are supposed to be added sequentially together (not interleaved by a namespace from other
175 * element). This mimic namespace visibility, since the namespace defined in element E is not visible from its parents or
176 * siblings. On the other hand, namespace from a parent element can be redefined in a child element. This is also reflected
177 * by lyxml_ns_get() which returns the most recent namespace definition for the given prefix.
178 *
179 * When leaving processing of a subtree of some element (after it is removed from context->elements), caller is supposed to call
180 * lyxml_ns_rm() to remove all the namespaces defined in such an element from the context.
181 *
182 * @param[in] context XML context to work with.
183 * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute(). Can be NULL for default namespace.
184 * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from lyxml_get_attribute()).
185 * @param[in] uri Namespace URI (value) to store. Value can be obtained via lyxml_get_string() and caller is not supposed to
186 * work with the pointer when the function succeeds. In case of error the value is freed.
187 * @return LY_ERR values.
188 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200189LY_ERR
Radek Krejci17a78d82019-05-15 15:49:55 +0200190lyxml_ns_add(struct lyxml_context *context, const char *prefix, size_t prefix_len, char *uri)
191{
192 struct lyxml_ns *ns;
193
194 ns = malloc(sizeof *ns);
195 LY_CHECK_ERR_RET(!ns, LOGMEM(context->ctx), LY_EMEM);
196
197 /* we need to connect the depth of the element where the namespace is defined with the
198 * namespace record to be able to maintain (remove) the record when the parser leaves
199 * (to its sibling or back to the parent) the element where the namespace was defined */
200 ns->depth = context->elements.count;
201
202 ns->uri = uri;
203 if (prefix) {
204 ns->prefix = strndup(prefix, prefix_len);
205 LY_CHECK_ERR_RET(!ns->prefix, LOGMEM(context->ctx); free(ns->uri); free(ns), LY_EMEM);
206 } else {
207 ns->prefix = NULL;
208 }
209
210 LY_CHECK_ERR_RET(ly_set_add(&context->ns, ns, LY_SET_OPT_USEASLIST) == -1,
211 free(ns->prefix); free(ns->uri); free(ns), LY_EMEM);
212 return LY_SUCCESS;
213}
214
215/**
216 * @brief Remove all the namespaces defined in the element recently closed (removed from the context->elements).
217 *
218 * @param[in] context XML context to work with.
219 * @return LY_ERR values.
220 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200221LY_ERR
Radek Krejci17a78d82019-05-15 15:49:55 +0200222lyxml_ns_rm(struct lyxml_context *context)
223{
224 unsigned int u;
225
226 for (u = context->ns.count - 1; u + 1 > 0; --u) {
227 if (((struct lyxml_ns *)context->ns.objs[u])->depth != context->elements.count + 1) {
228 /* we are done, the namespaces from a single element are supposed to be together */
229 break;
230 }
231 /* remove the ns structure */
232 free(((struct lyxml_ns *)context->ns.objs[u])->prefix);
233 free(((struct lyxml_ns *)context->ns.objs[u])->uri);
234 free(context->ns.objs[u]);
235 --context->ns.count;
236 }
237
238 if (!context->ns.count) {
239 /* cleanup the context's namespaces storage */
240 ly_set_erase(&context->ns, NULL);
241 }
242
243 return LY_SUCCESS;
244}
245
246const struct lyxml_ns *
247lyxml_ns_get(struct lyxml_context *context, const char *prefix, size_t prefix_len)
248{
249 unsigned int u;
250 struct lyxml_ns *ns;
251
252 for (u = context->ns.count - 1; u + 1 > 0; --u) {
253 ns = (struct lyxml_ns *)context->ns.objs[u];
254 if (prefix) {
255 if (!strncmp(prefix, ns->prefix, prefix_len) && ns->prefix[prefix_len] == '\0') {
256 return ns;
257 }
258 } else if (!ns->prefix) {
259 /* default namespace */
260 return ns;
261 }
262 }
263
264 return NULL;
265}
266
Radek Krejci7a7fa902018-09-25 17:08:21 +0200267LY_ERR
Radek Krejcid70d1072018-10-09 14:20:47 +0200268lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size, char **output, size_t *length, int *dynamic)
Radek Krejci7a7fa902018-09-25 17:08:21 +0200269{
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 */
Radek Krejcid70d1072018-10-09 14:20:47 +0200280 const char *in = (*input), *start;
281 char *buf = NULL, delim;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200282 size_t offset; /* read offset in input buffer */
Radek Krejcid70d1072018-10-09 14:20:47 +0200283 size_t len; /* length of the output string (write offset in output buffer) */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200284 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;
Radek Krejci17a78d82019-05-15 15:49:55 +0200289 LY_ERR rc = LY_SUCCESS;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200290
Radek Krejcib1890642018-10-03 14:05:40 +0200291 assert(context);
292 assert(context->status == LYXML_ELEM_CONTENT || context->status == LYXML_ATTR_CONTENT);
293
Radek Krejci7a7fa902018-09-25 17:08:21 +0200294 if (in[0] == '\'') {
295 delim = '\'';
296 ++in;
297 } else if (in[0] == '"') {
298 delim = '"';
299 ++in;
300 } else {
301 delim = '<';
302 empty_content = true;
303 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200304 start = in;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200305
306 if (empty_content) {
307 /* only when processing element's content - try to ignore whitespaces used to format XML data
308 * before element's child or closing tag */
Radek Krejci117d2082018-09-26 10:05:14 +0200309 for (offset = newlines = 0; in[offset] && is_xmlws(in[offset]); ++offset) {
310 if (in[offset] == '\n') {
311 ++newlines;
312 }
313 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200314 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 +0200315 context->line += newlines;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200316 if (in[offset] == '<') {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200317 (*input) = in + offset;
Radek Krejcie7b95092019-05-15 11:03:07 +0200318 context->status -= 1; /* LYXML_ELEMENT */;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200319 return LY_EINVAL;
320 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200321 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200322 /* init */
323 offset = len = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200324
Radek Krejcid70d1072018-10-09 14:20:47 +0200325 if (0) {
326getbuffer:
327 /* prepare output buffer */
328 if (*buffer) {
329 buf = *buffer;
330 size = *buffer_size;
331 } else {
332 buf = malloc(BUFSIZE);
333 size = BUFSIZE;
334 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
335 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200336 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200337
338 /* parse */
339 while (in[offset]) {
340 if (in[offset] == '&') {
Radek Krejcid70d1072018-10-09 14:20:47 +0200341 if (!buf) {
342 /* it is necessary to modify the input, so we will need a dynamically allocated buffer */
343 goto getbuffer;
344 }
345
Radek Krejci7a7fa902018-09-25 17:08:21 +0200346 if (offset) {
347 /* store what we have so far */
348 BUFSIZE_CHECK(ctx, buf, size, len, offset);
349 memcpy(&buf[len], in, offset);
350 len += offset;
351 in += offset;
352 offset = 0;
353 }
354 /* process reference */
355 /* we will need 4 bytes at most since we support only the predefined
356 * (one-char) entities and character references */
357 BUFSIZE_CHECK(ctx, buf, size, len, 4);
358 ++offset;
359 if (in[offset] != '#') {
360 /* entity reference - only predefined references are supported */
361 if (!strncmp(&in[offset], "lt;", 3)) {
362 buf[len++] = '<';
363 in += 4; /* &lt; */
364 } else if (!strncmp(&in[offset], "gt;", 3)) {
365 buf[len++] = '>';
366 in += 4; /* &gt; */
367 } else if (!strncmp(&in[offset], "amp;", 4)) {
368 buf[len++] = '&';
369 in += 5; /* &amp; */
370 } else if (!strncmp(&in[offset], "apos;", 5)) {
371 buf[len++] = '\'';
372 in += 6; /* &apos; */
373 } else if (!strncmp(&in[offset], "quot;", 5)) {
374 buf[len++] = '\"';
375 in += 6; /* &quot; */
376 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200377 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
378 "Entity reference \"%.*s\" not supported, only predefined references allowed.", 10, &in[offset-1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200379 goto error;
380 }
381 offset = 0;
382 } else {
383 p = (void*)&in[offset - 1];
384 /* character reference */
385 ++offset;
386 if (isdigit(in[offset])) {
387 for (n = 0; isdigit(in[offset]); offset++) {
388 n = (10 * n) + (in[offset] - '0');
389 }
390 } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) {
391 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
392 if (isdigit(in[offset])) {
393 u = (in[offset] - '0');
394 } else if (in[offset] > 'F') {
395 u = 10 + (in[offset] - 'a');
396 } else {
397 u = 10 + (in[offset] - 'A');
398 }
399 n = (16 * n) + u;
400 }
401 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200402 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200403 goto error;
404
405 }
406 LY_CHECK_ERR_GOTO(in[offset] != ';',
407 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP,
408 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
409 error);
410 ++offset;
411 rc = lyxml_pututf8(&buf[len], n, &u);
412 LY_CHECK_ERR_GOTO(rc, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
Radek Krejci117d2082018-09-26 10:05:14 +0200413 "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200414 error);
415 len += u;
416 in += offset;
417 offset = 0;
418 }
419 } else if (in[offset] == delim) {
420 /* end of string */
Radek Krejcid70d1072018-10-09 14:20:47 +0200421 if (buf) {
422 if (len + offset >= size) {
423 buf = ly_realloc(buf, len + offset + 1);
424 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
425 size = len + offset + 1;
426 }
427 memcpy(&buf[len], in, offset);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200428 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200429 len += offset;
430 /* in case of element content, keep the leading <,
Radek Krejcib1890642018-10-03 14:05:40 +0200431 * for attribute's value move after the terminating quotation mark */
432 if (context->status == LYXML_ELEM_CONTENT) {
Radek Krejci7a7fa902018-09-25 17:08:21 +0200433 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)) {
Radek Krejcibb9b1982019-04-08 14:24:59 +0200451 /* buffer not provided, buf is local */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200452 free(buf);
Radek Krejcibb9b1982019-04-08 14:24:59 +0200453 } else if (buf) {
454 /* buf is shared with caller via buffer, but buf could be reallocated, so update the provided buffer */
455 (*buffer) = buf;
456 (*buffer_size) = size;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200457 }
458 return LY_EVALID;
459
460success:
Radek Krejcid70d1072018-10-09 14:20:47 +0200461 if (buf) {
462 if (!(*buffer) && size != len + 1) {
463 /* not using provided buffer, so fit the allocated buffer to what we really have inside */
464 p = realloc(buf, len + 1);
465 /* ignore realloc fail because we are reducing the buffer,
466 * so just return bigger buffer than needed */
467 if (p) {
468 size = len + 1;
469 buf = p;
470 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200471 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200472 /* set terminating NULL byte */
473 buf[len] = '\0';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200474 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200475
Radek Krejcib1890642018-10-03 14:05:40 +0200476 context->status -= 1;
Radek Krejcid70d1072018-10-09 14:20:47 +0200477 if (buf) {
478 (*buffer) = buf;
479 (*buffer_size) = size;
480 (*output) = buf;
481 (*dynamic) = 1;
482 } else {
483 (*output) = (char*)start;
484 (*dynamic) = 0;
485 }
486 (*length) = len;
487
Radek Krejci28e8cb52019-03-08 11:31:31 +0100488 if (context->status == LYXML_ATTRIBUTE) {
489 if (in[0] == '>') {
490 /* element terminated by > - termination of the opening tag */
491 context->status = LYXML_ELEM_CONTENT;
492 ++in;
493 } else if (in[0] == '/' && in[1] == '>') {
494 /* element terminated by /> - termination of an empty element */
495 context->status = LYXML_ELEMENT;
496 in += 2;
497
498 /* remove the closed element record from the tags list */
499 free(context->elements.objs[context->elements.count - 1]);
500 --context->elements.count;
Radek Krejci17a78d82019-05-15 15:49:55 +0200501
502 /* remove also the namespaces conneted with the element */
503 rc = lyxml_ns_rm(context);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100504 }
505 }
506
507 (*input) = in;
Radek Krejci17a78d82019-05-15 15:49:55 +0200508 return rc;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200509
510#undef BUFSIZE
511#undef BUFSIZE_STEP
512#undef BUFSIZE_CHECK
513}
514
Radek Krejcid972c252018-09-25 13:23:39 +0200515LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200516lyxml_get_attribute(struct lyxml_context *context, const char **input,
Radek Krejcid972c252018-09-25 13:23:39 +0200517 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
518{
519 struct ly_ctx *ctx = context->ctx; /* shortcut */
520 const char *in = (*input);
521 const char *id;
522 const char *endtag;
523 LY_ERR rc;
524 unsigned int c;
525 size_t endtag_len;
Radek Krejci17a78d82019-05-15 15:49:55 +0200526 int is_ns = 0;
527 const char *ns_prefix = NULL;
528 size_t ns_prefix_len = 0;
Radek Krejcid972c252018-09-25 13:23:39 +0200529
Radek Krejci17a78d82019-05-15 15:49:55 +0200530start:
Radek Krejcid972c252018-09-25 13:23:39 +0200531 /* initialize output variables */
532 (*prefix) = (*name) = NULL;
533 (*prefix_len) = (*name_len) = 0;
534
535 /* skip initial whitespaces */
536 ign_xmlws(context, in);
537
538 if (in[0] == '\0') {
539 /* EOF - not expected at this place */
540 return LY_EINVAL;
Radek Krejcid972c252018-09-25 13:23:39 +0200541 }
542
543 /* remember the identifier start before checking its format */
544 id = in;
545 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
546 LY_CHECK_RET(rc);
547 if (c == ':') {
548 /* we have prefixed identifier */
549 endtag = in - endtag_len;
550
551 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
552 LY_CHECK_RET(rc);
553
554 (*prefix) = id;
555 (*prefix_len) = endtag - id;
556 id = endtag + 1;
557 }
558 if (!is_xmlws(c) && c != '=') {
559 in = in - endtag_len;
560 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "whitespace or '='");
561 return LY_EVALID;
562 }
563 in = in - endtag_len;
564 (*name) = id;
565 (*name_len) = in - id;
566
567 /* eat '=' and stop at the value beginning */
568 ign_xmlws(context, in);
569 if (in[0] != '=') {
570 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "'='");
571 return LY_EVALID;
572 }
573 ++in;
574 ign_xmlws(context, in);
575 if (in[0] != '\'' && in[0] != '"') {
Radek Krejcib1890642018-10-03 14:05:40 +0200576 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP,
577 LY_VCODE_INSTREXP_len(in), in, "either single or double quotation mark");
Radek Krejcid972c252018-09-25 13:23:39 +0200578 return LY_EVALID;
579 }
Radek Krejcib1890642018-10-03 14:05:40 +0200580 context->status = LYXML_ATTR_CONTENT;
Radek Krejcid972c252018-09-25 13:23:39 +0200581
Radek Krejci17a78d82019-05-15 15:49:55 +0200582 is_ns = 0;
583 if (*prefix && *prefix_len == 5 && !strncmp(*prefix, "xmlns", 5)) {
584 is_ns = 1;
585 ns_prefix = *name;
586 ns_prefix_len = *name_len;
587 } else if (*name_len == 5 && !strncmp(*name, "xmlns", 5)) {
588 is_ns = 1;
589 }
590 if (is_ns) {
591 /* instead of attribute, we have namespace specification,
592 * so process it automatically and then move to another attribute (if any) */
593 char *value = NULL;
594 size_t value_len = 0;
595 int dynamic = 0;
596
597 LY_CHECK_RET(lyxml_get_string(context, &in, &value, &value_len, &value, &value_len, &dynamic));
598 if ((rc = lyxml_ns_add(context, ns_prefix, ns_prefix_len, dynamic ? value : strndup(value, value_len)))) {
599 if (dynamic) {
600 free(value);
601 return rc;
602 }
603 }
604 if (context->status == LYXML_ATTRIBUTE) {
605 goto start;
606 } else {
607 (*prefix) = (*name) = NULL;
608 (*prefix_len) = (*name_len) = 0;
609 }
610 }
611
Radek Krejcid972c252018-09-25 13:23:39 +0200612 /* move caller's input */
613 (*input) = in;
614 return LY_SUCCESS;
615}
616
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200617LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200618lyxml_get_element(struct lyxml_context *context, const char **input,
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200619 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
620{
621 struct ly_ctx *ctx = context->ctx; /* shortcut */
622 const char *in = (*input);
623 const char *endtag;
624 const char *sectname;
625 const char *id;
626 size_t endtag_len, newlines;
Radek Krejcib1890642018-10-03 14:05:40 +0200627 bool loop = true, closing = false;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200628 unsigned int c;
629 LY_ERR rc;
Radek Krejcib1890642018-10-03 14:05:40 +0200630 struct lyxml_elem *e;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200631
632 /* initialize output variables */
633 (*prefix) = (*name) = NULL;
634 (*prefix_len) = (*name_len) = 0;
635
636 while (loop) {
637 ign_xmlws(context, in);
638
639 if (in[0] == '\0') {
640 /* EOF */
Radek Krejcib1890642018-10-03 14:05:40 +0200641 context->status = LYXML_END;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200642 goto success;
643 } else if (in[0] != '<') {
644 return LY_EINVAL;
645 }
646 move_input(context, in, 1);
647
648 if (in[0] == '!') {
649 move_input(context, in, 1);
650 /* sections to ignore */
651 if (!strncmp(in, "--", 2)) {
652 /* comment */
653 move_input(context, in, 2);
654 sectname = "Comment";
655 endtag = "-->";
656 endtag_len = 3;
657 } else if (!strncmp(in, "[CDATA[", 7)) {
658 /* CDATA section */
659 move_input(context, in, 7);
660 sectname = "CData";
661 endtag = "]]>";
662 endtag_len = 3;
663 } else if (!strncmp(in, "DOCTYPE", 7)) {
664 /* Document type declaration - not supported */
665 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP, "Document Type Declaration");
666 return LY_EVALID;
Radek Krejcic5c31bb2019-04-08 14:40:52 +0200667 } else {
668 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Unknown XML section \"%.20s\".", &in[-2]);
669 return LY_EVALID;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200670 }
671 in = ign_todelim(in, endtag, endtag_len, &newlines);
672 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, sectname), LY_EVALID);
673 context->line += newlines;
674 in += endtag_len;
675 } else if (in[0] == '?') {
676 in = ign_todelim(in, "?>", 2, &newlines);
677 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
678 context->line += newlines;
679 in += 2;
Radek Krejcib1890642018-10-03 14:05:40 +0200680 } else if (in[0] == '/') {
681 /* closing element */
682 closing = true;
683 ++in;
684 goto element;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200685 } else {
686 /* element */
Radek Krejcib1890642018-10-03 14:05:40 +0200687element:
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200688 ign_xmlws(context, in);
689 LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
690
691 /* remember the identifier start before checking its format */
692 id = in;
693 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
694 LY_CHECK_RET(rc);
695 if (c == ':') {
696 /* we have prefixed identifier */
697 endtag = in - endtag_len;
698
699 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
700 LY_CHECK_RET(rc);
701
702 (*prefix) = id;
703 (*prefix_len) = endtag - id;
704 id = endtag + 1;
705 }
706 if (!is_xmlws(c) && c != '/' && c != '>') {
707 in = in - endtag_len;
Radek Krejcid972c252018-09-25 13:23:39 +0200708 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in,
709 "whitespace or element tag termination ('>' or '/>'");
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200710 return LY_EVALID;
711 }
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200712 (*name) = id;
Radek Krejcib1890642018-10-03 14:05:40 +0200713 (*name_len) = in - endtag_len - id;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200714
Radek Krejcib1890642018-10-03 14:05:40 +0200715 if (is_xmlws(c)) {
716 /* go to the next meaningful input */
717 ign_xmlws(context, in);
718 LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
719 c = in[0];
720 ++in;
721 endtag_len = 1;
722 }
723
724 if (closing) {
725 /* match opening and closing element tags */
726 LY_CHECK_ERR_RET(
727 !context->elements.count,
Radek Krejci3fbc9872019-04-16 16:50:01 +0200728 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Opening and closing elements tag missmatch (\"%.*s\").", *name_len, *name),
Radek Krejcib1890642018-10-03 14:05:40 +0200729 LY_EVALID);
730 e = (struct lyxml_elem*)context->elements.objs[context->elements.count - 1];
731 LY_CHECK_ERR_RET(e->prefix_len != *prefix_len || e->name_len != *name_len
732 || (*prefix_len && strncmp(*prefix, e->prefix, e->prefix_len)) || strncmp(*name, e->name, e->name_len),
Radek Krejci3fbc9872019-04-16 16:50:01 +0200733 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Opening and closing elements tag missmatch (\"%.*s\").", *name_len, *name),
Radek Krejcib1890642018-10-03 14:05:40 +0200734 LY_EVALID);
735 /* opening and closing element tags matches, remove record from the opening tags list */
736 free(e);
737 --context->elements.count;
Radek Krejci17a78d82019-05-15 15:49:55 +0200738
739 /* remove also the namespaces conneted with the element */
740 rc = lyxml_ns_rm(context);
741
Radek Krejcib1890642018-10-03 14:05:40 +0200742 /* do not return element information to announce closing element being currently processed */
743 *name = *prefix = NULL;
744 *name_len = *prefix_len = 0;
745
746 if (c == '>') {
747 /* end of closing element */
748 context->status = LYXML_ELEMENT;
749 } else {
750 in -= endtag_len;
751 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Unexpected data \"%.*s\" in closing element tag.",
752 LY_VCODE_INSTREXP_len(in), in);
753 return LY_EVALID;
754 }
755 } else {
756 if (c == '>') {
757 /* end of opening element */
758 context->status = LYXML_ELEM_CONTENT;
759 } else if (c == '/' && in[0] == '>') {
760 /* empty element closing */
761 context->status = LYXML_ELEMENT;
762 ++in;
763 } else {
764 /* attribute */
765 context->status = LYXML_ATTRIBUTE;
766 in -= endtag_len;
767 }
768
769 if (context->status != LYXML_ELEMENT) {
770 /* store element opening tag information */
771 e = malloc(sizeof *e);
772 LY_CHECK_ERR_RET(!e, LOGMEM(ctx), LY_EMEM);
773 e->name = *name;
774 e->prefix = *prefix;
775 e->name_len = *name_len;
776 e->prefix_len = *prefix_len;
777 ly_set_add(&context->elements, e, LY_SET_OPT_USEASLIST);
778 }
779 }
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200780 loop = false;
781 }
782 }
783
784success:
785 /* move caller's input */
786 (*input) = in;
787 return LY_SUCCESS;
788}
789
Radek Krejcib1890642018-10-03 14:05:40 +0200790
791void
792lyxml_context_clear(struct lyxml_context *context)
793{
794 unsigned int u;
795
796 ly_set_erase(&context->elements, free);
797 for (u = context->ns.count - 1; u + 1 > 0; --u) {
798 /* remove the ns structure */
799 free(((struct lyxml_ns *)context->ns.objs[u])->prefix);
800 free(((struct lyxml_ns *)context->ns.objs[u])->uri);
801 free(context->ns.objs[u]);
802 }
803 ly_set_erase(&context->ns, NULL);
804}
Radek Krejcie7b95092019-05-15 11:03:07 +0200805
806LY_ERR
807lyxml_dump_text(struct lyout *out, const char *text, int attribute)
808{
809 LY_ERR ret = LY_SUCCESS;
810 unsigned int u;
811
812 if (!text) {
813 return 0;
814 }
815
816 for (u = 0; text[u]; u++) {
817 switch (text[u]) {
818 case '&':
819 ret = ly_print(out, "&amp;");
820 break;
821 case '<':
822 ret = ly_print(out, "&lt;");
823 break;
824 case '>':
825 /* not needed, just for readability */
826 ret = ly_print(out, "&gt;");
827 break;
828 case '"':
829 if (attribute) {
830 ret = ly_print(out, "&quot;");
831 break;
832 }
833 /* falls through */
834 default:
835 ly_write(out, &text[u], 1);
836 }
837 }
838
839 return ret;
840}
841