blob: a318420808d477bf400cdaa084e80bb2725a2757 [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.
Radek Krejci17a78d82019-05-15 15:49:55 +0200219 */
Radek Krejci17dca992019-05-17 10:53:27 +0200220void
Radek Krejci17a78d82019-05-15 15:49:55 +0200221lyxml_ns_rm(struct lyxml_context *context)
222{
223 unsigned int u;
224
225 for (u = context->ns.count - 1; u + 1 > 0; --u) {
226 if (((struct lyxml_ns *)context->ns.objs[u])->depth != context->elements.count + 1) {
227 /* we are done, the namespaces from a single element are supposed to be together */
228 break;
229 }
230 /* remove the ns structure */
231 free(((struct lyxml_ns *)context->ns.objs[u])->prefix);
232 free(((struct lyxml_ns *)context->ns.objs[u])->uri);
233 free(context->ns.objs[u]);
234 --context->ns.count;
235 }
236
237 if (!context->ns.count) {
238 /* cleanup the context's namespaces storage */
239 ly_set_erase(&context->ns, NULL);
240 }
Radek Krejci17a78d82019-05-15 15:49:55 +0200241}
242
243const struct lyxml_ns *
244lyxml_ns_get(struct lyxml_context *context, const char *prefix, size_t prefix_len)
245{
246 unsigned int u;
247 struct lyxml_ns *ns;
248
249 for (u = context->ns.count - 1; u + 1 > 0; --u) {
250 ns = (struct lyxml_ns *)context->ns.objs[u];
251 if (prefix) {
252 if (!strncmp(prefix, ns->prefix, prefix_len) && ns->prefix[prefix_len] == '\0') {
253 return ns;
254 }
255 } else if (!ns->prefix) {
256 /* default namespace */
257 return ns;
258 }
259 }
260
261 return NULL;
262}
263
Radek Krejci7a7fa902018-09-25 17:08:21 +0200264LY_ERR
Radek Krejcid70d1072018-10-09 14:20:47 +0200265lyxml_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 +0200266{
267#define BUFSIZE 4096
268#define BUFSIZE_STEP 4096
269#define BUFSIZE_CHECK(CTX, BUF, SIZE, CURR, NEED) \
270 if (CURR+NEED >= SIZE) { \
271 BUF = ly_realloc(BUF, SIZE + BUFSIZE_STEP); \
272 LY_CHECK_ERR_RET(!BUF, LOGMEM(CTX), LY_EMEM); \
273 SIZE += BUFSIZE_STEP; \
274 }
275
276 struct ly_ctx *ctx = context->ctx; /* shortcut */
Radek Krejcid70d1072018-10-09 14:20:47 +0200277 const char *in = (*input), *start;
278 char *buf = NULL, delim;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200279 size_t offset; /* read offset in input buffer */
Radek Krejcid70d1072018-10-09 14:20:47 +0200280 size_t len; /* length of the output string (write offset in output buffer) */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200281 size_t size; /* size of the output buffer */
282 void *p;
Radek Krejci117d2082018-09-26 10:05:14 +0200283 uint32_t n;
284 size_t u, newlines;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200285 bool empty_content = false;
Radek Krejci17a78d82019-05-15 15:49:55 +0200286 LY_ERR rc = LY_SUCCESS;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200287
Radek Krejcib1890642018-10-03 14:05:40 +0200288 assert(context);
289 assert(context->status == LYXML_ELEM_CONTENT || context->status == LYXML_ATTR_CONTENT);
290
Radek Krejci7a7fa902018-09-25 17:08:21 +0200291 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 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200301 start = in;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200302
303 if (empty_content) {
304 /* only when processing element's content - try to ignore whitespaces used to format XML data
305 * before element's child or closing tag */
Radek Krejci117d2082018-09-26 10:05:14 +0200306 for (offset = newlines = 0; in[offset] && is_xmlws(in[offset]); ++offset) {
307 if (in[offset] == '\n') {
308 ++newlines;
309 }
310 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200311 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 +0200312 context->line += newlines;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200313 if (in[offset] == '<') {
Radek Krejci339e2de2019-05-17 14:28:24 +0200314 const char *name, *prefix;
315 size_t name_len, prefix_len;
316
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200317 (*input) = in + offset;
Radek Krejci339e2de2019-05-17 14:28:24 +0200318
319 /* get know if it is child element (indentation) or closing element (whitespace-only content) */
320 in = *input;
321 rc = lyxml_get_element(context, &in, &prefix, &prefix_len, &name, &name_len);
322 if (name) {
323 /* the element here is not closing element, so we have the just indentation formatting before the child */
324 free(context->elements.objs[--context->elements.count]);
325 context->status -= 1; /* LYXML_ELEMENT */
326 return LY_EINVAL;
327 } else if (rc) {
328 /* some parsing error, so pass it */
329 (*input) = in;
330 return rc;
331 } else {
332 /* whitespace-only content */
333 len = offset - 1;
334 goto success;
335 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200336 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200337 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200338 /* init */
339 offset = len = 0;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200340
Radek Krejcid70d1072018-10-09 14:20:47 +0200341 if (0) {
342getbuffer:
343 /* prepare output buffer */
344 if (*buffer) {
345 buf = *buffer;
346 size = *buffer_size;
347 } else {
348 buf = malloc(BUFSIZE);
349 size = BUFSIZE;
350 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
351 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200352 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200353
354 /* parse */
355 while (in[offset]) {
356 if (in[offset] == '&') {
Radek Krejcid70d1072018-10-09 14:20:47 +0200357 if (!buf) {
358 /* it is necessary to modify the input, so we will need a dynamically allocated buffer */
359 goto getbuffer;
360 }
361
Radek Krejci7a7fa902018-09-25 17:08:21 +0200362 if (offset) {
363 /* store what we have so far */
364 BUFSIZE_CHECK(ctx, buf, size, len, offset);
365 memcpy(&buf[len], in, offset);
366 len += offset;
367 in += offset;
368 offset = 0;
369 }
370 /* process reference */
371 /* we will need 4 bytes at most since we support only the predefined
372 * (one-char) entities and character references */
373 BUFSIZE_CHECK(ctx, buf, size, len, 4);
374 ++offset;
375 if (in[offset] != '#') {
376 /* entity reference - only predefined references are supported */
377 if (!strncmp(&in[offset], "lt;", 3)) {
378 buf[len++] = '<';
379 in += 4; /* &lt; */
380 } else if (!strncmp(&in[offset], "gt;", 3)) {
381 buf[len++] = '>';
382 in += 4; /* &gt; */
383 } else if (!strncmp(&in[offset], "amp;", 4)) {
384 buf[len++] = '&';
385 in += 5; /* &amp; */
386 } else if (!strncmp(&in[offset], "apos;", 5)) {
387 buf[len++] = '\'';
388 in += 6; /* &apos; */
389 } else if (!strncmp(&in[offset], "quot;", 5)) {
390 buf[len++] = '\"';
391 in += 6; /* &quot; */
392 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200393 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
394 "Entity reference \"%.*s\" not supported, only predefined references allowed.", 10, &in[offset-1]);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200395 goto error;
396 }
397 offset = 0;
398 } else {
399 p = (void*)&in[offset - 1];
400 /* character reference */
401 ++offset;
402 if (isdigit(in[offset])) {
403 for (n = 0; isdigit(in[offset]); offset++) {
404 n = (10 * n) + (in[offset] - '0');
405 }
406 } else if (in[offset] == 'x' && isxdigit(in[offset + 1])) {
407 for (n = 0, ++offset; isxdigit(in[offset]); offset++) {
408 if (isdigit(in[offset])) {
409 u = (in[offset] - '0');
410 } else if (in[offset] > 'F') {
411 u = 10 + (in[offset] - 'a');
412 } else {
413 u = 10 + (in[offset] - 'A');
414 }
415 n = (16 * n) + u;
416 }
417 } else {
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200418 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200419 goto error;
420
421 }
422 LY_CHECK_ERR_GOTO(in[offset] != ';',
423 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP,
424 LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
425 error);
426 ++offset;
427 rc = lyxml_pututf8(&buf[len], n, &u);
428 LY_CHECK_ERR_GOTO(rc, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
Radek Krejci117d2082018-09-26 10:05:14 +0200429 "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200430 error);
431 len += u;
432 in += offset;
433 offset = 0;
434 }
435 } else if (in[offset] == delim) {
436 /* end of string */
Radek Krejcid70d1072018-10-09 14:20:47 +0200437 if (buf) {
438 if (len + offset >= size) {
439 buf = ly_realloc(buf, len + offset + 1);
440 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
441 size = len + offset + 1;
442 }
443 memcpy(&buf[len], in, offset);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200444 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200445 len += offset;
446 /* in case of element content, keep the leading <,
Radek Krejcib1890642018-10-03 14:05:40 +0200447 * for attribute's value move after the terminating quotation mark */
448 if (context->status == LYXML_ELEM_CONTENT) {
Radek Krejci339e2de2019-05-17 14:28:24 +0200449 const char *name, *prefix;
450 size_t name_len, prefix_len;
451
Radek Krejci7a7fa902018-09-25 17:08:21 +0200452 in += offset;
Radek Krejci339e2de2019-05-17 14:28:24 +0200453
454 /* get know if it is child element (mixed content) or closing element (regular content) */
455 (*input) = in;
456 rc = lyxml_get_element(context, &in, &prefix, &prefix_len, &name, &name_len);
457 if (name) {
458 /* the element here is not closing element, so we have not allowed mixed content */
459 struct lyxml_elem *e = (struct lyxml_elem*)context->elements.objs[--context->elements.count];
460 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Mixed XML content is not allowed (%.*s).",
461 offset + (in - (*input)), &(*input)[-offset]);
462 free(e);
463 return LY_EVALID;
464 } else if (rc) {
465 /* some parsing error, so pass it */
466 return rc;
467 } else {
468 /* closing element, so we have regular content */
469 context->status++;
470 goto success;
471 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200472 } else {
473 in += offset + 1;
474 }
475 goto success;
476 } else {
477 /* log lines */
478 if (in[offset] == '\n') {
479 ++context->line;
480 }
481
482 /* continue */
483 ++offset;
484 }
485 }
486 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF);
487error:
488 if (!(*buffer)) {
Radek Krejcibb9b1982019-04-08 14:24:59 +0200489 /* buffer not provided, buf is local */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200490 free(buf);
Radek Krejcibb9b1982019-04-08 14:24:59 +0200491 } else if (buf) {
492 /* buf is shared with caller via buffer, but buf could be reallocated, so update the provided buffer */
493 (*buffer) = buf;
494 (*buffer_size) = size;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200495 }
496 return LY_EVALID;
497
498success:
Radek Krejcid70d1072018-10-09 14:20:47 +0200499 if (buf) {
500 if (!(*buffer) && size != len + 1) {
501 /* not using provided buffer, so fit the allocated buffer to what we really have inside */
502 p = realloc(buf, len + 1);
503 /* ignore realloc fail because we are reducing the buffer,
504 * so just return bigger buffer than needed */
505 if (p) {
506 size = len + 1;
507 buf = p;
508 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200509 }
Radek Krejcid70d1072018-10-09 14:20:47 +0200510 /* set terminating NULL byte */
511 buf[len] = '\0';
Radek Krejci7a7fa902018-09-25 17:08:21 +0200512 }
Radek Krejci7a7fa902018-09-25 17:08:21 +0200513
Radek Krejcib1890642018-10-03 14:05:40 +0200514 context->status -= 1;
Radek Krejcid70d1072018-10-09 14:20:47 +0200515 if (buf) {
516 (*buffer) = buf;
517 (*buffer_size) = size;
518 (*output) = buf;
519 (*dynamic) = 1;
520 } else {
521 (*output) = (char*)start;
522 (*dynamic) = 0;
523 }
524 (*length) = len;
525
Radek Krejci28e8cb52019-03-08 11:31:31 +0100526 if (context->status == LYXML_ATTRIBUTE) {
527 if (in[0] == '>') {
528 /* element terminated by > - termination of the opening tag */
529 context->status = LYXML_ELEM_CONTENT;
530 ++in;
531 } else if (in[0] == '/' && in[1] == '>') {
532 /* element terminated by /> - termination of an empty element */
533 context->status = LYXML_ELEMENT;
534 in += 2;
535
536 /* remove the closed element record from the tags list */
537 free(context->elements.objs[context->elements.count - 1]);
538 --context->elements.count;
Radek Krejci17a78d82019-05-15 15:49:55 +0200539
540 /* remove also the namespaces conneted with the element */
Radek Krejci17dca992019-05-17 10:53:27 +0200541 lyxml_ns_rm(context);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100542 }
543 }
544
545 (*input) = in;
Radek Krejci17a78d82019-05-15 15:49:55 +0200546 return rc;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200547
548#undef BUFSIZE
549#undef BUFSIZE_STEP
550#undef BUFSIZE_CHECK
551}
552
Radek Krejcid972c252018-09-25 13:23:39 +0200553LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200554lyxml_get_attribute(struct lyxml_context *context, const char **input,
Radek Krejcid972c252018-09-25 13:23:39 +0200555 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
556{
557 struct ly_ctx *ctx = context->ctx; /* shortcut */
558 const char *in = (*input);
559 const char *id;
560 const char *endtag;
561 LY_ERR rc;
562 unsigned int c;
563 size_t endtag_len;
Radek Krejci17a78d82019-05-15 15:49:55 +0200564 int is_ns = 0;
565 const char *ns_prefix = NULL;
566 size_t ns_prefix_len = 0;
Radek Krejcid972c252018-09-25 13:23:39 +0200567
Radek Krejci17a78d82019-05-15 15:49:55 +0200568start:
Radek Krejcid972c252018-09-25 13:23:39 +0200569 /* initialize output variables */
570 (*prefix) = (*name) = NULL;
571 (*prefix_len) = (*name_len) = 0;
572
573 /* skip initial whitespaces */
574 ign_xmlws(context, in);
575
576 if (in[0] == '\0') {
577 /* EOF - not expected at this place */
578 return LY_EINVAL;
Radek Krejcid972c252018-09-25 13:23:39 +0200579 }
580
581 /* remember the identifier start before checking its format */
582 id = in;
583 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
584 LY_CHECK_RET(rc);
585 if (c == ':') {
586 /* we have prefixed identifier */
587 endtag = in - endtag_len;
588
589 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
590 LY_CHECK_RET(rc);
591
592 (*prefix) = id;
593 (*prefix_len) = endtag - id;
594 id = endtag + 1;
595 }
596 if (!is_xmlws(c) && c != '=') {
597 in = in - endtag_len;
598 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "whitespace or '='");
599 return LY_EVALID;
600 }
601 in = in - endtag_len;
602 (*name) = id;
603 (*name_len) = in - id;
604
605 /* eat '=' and stop at the value beginning */
606 ign_xmlws(context, in);
607 if (in[0] != '=') {
608 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in, "'='");
609 return LY_EVALID;
610 }
611 ++in;
612 ign_xmlws(context, in);
613 if (in[0] != '\'' && in[0] != '"') {
Radek Krejcib1890642018-10-03 14:05:40 +0200614 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP,
615 LY_VCODE_INSTREXP_len(in), in, "either single or double quotation mark");
Radek Krejcid972c252018-09-25 13:23:39 +0200616 return LY_EVALID;
617 }
Radek Krejcib1890642018-10-03 14:05:40 +0200618 context->status = LYXML_ATTR_CONTENT;
Radek Krejcid972c252018-09-25 13:23:39 +0200619
Radek Krejci17a78d82019-05-15 15:49:55 +0200620 is_ns = 0;
621 if (*prefix && *prefix_len == 5 && !strncmp(*prefix, "xmlns", 5)) {
622 is_ns = 1;
623 ns_prefix = *name;
624 ns_prefix_len = *name_len;
625 } else if (*name_len == 5 && !strncmp(*name, "xmlns", 5)) {
626 is_ns = 1;
627 }
628 if (is_ns) {
629 /* instead of attribute, we have namespace specification,
630 * so process it automatically and then move to another attribute (if any) */
631 char *value = NULL;
632 size_t value_len = 0;
633 int dynamic = 0;
634
635 LY_CHECK_RET(lyxml_get_string(context, &in, &value, &value_len, &value, &value_len, &dynamic));
636 if ((rc = lyxml_ns_add(context, ns_prefix, ns_prefix_len, dynamic ? value : strndup(value, value_len)))) {
637 if (dynamic) {
638 free(value);
639 return rc;
640 }
641 }
642 if (context->status == LYXML_ATTRIBUTE) {
643 goto start;
644 } else {
645 (*prefix) = (*name) = NULL;
646 (*prefix_len) = (*name_len) = 0;
647 }
648 }
649
Radek Krejcid972c252018-09-25 13:23:39 +0200650 /* move caller's input */
651 (*input) = in;
652 return LY_SUCCESS;
653}
654
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200655LY_ERR
Radek Krejci7a7fa902018-09-25 17:08:21 +0200656lyxml_get_element(struct lyxml_context *context, const char **input,
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200657 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
658{
659 struct ly_ctx *ctx = context->ctx; /* shortcut */
660 const char *in = (*input);
661 const char *endtag;
662 const char *sectname;
663 const char *id;
664 size_t endtag_len, newlines;
Radek Krejcib1890642018-10-03 14:05:40 +0200665 bool loop = true, closing = false;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200666 unsigned int c;
667 LY_ERR rc;
Radek Krejcib1890642018-10-03 14:05:40 +0200668 struct lyxml_elem *e;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200669
670 /* initialize output variables */
671 (*prefix) = (*name) = NULL;
672 (*prefix_len) = (*name_len) = 0;
673
674 while (loop) {
675 ign_xmlws(context, in);
676
677 if (in[0] == '\0') {
678 /* EOF */
Radek Krejcib1890642018-10-03 14:05:40 +0200679 context->status = LYXML_END;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200680 goto success;
681 } else if (in[0] != '<') {
682 return LY_EINVAL;
683 }
684 move_input(context, in, 1);
685
686 if (in[0] == '!') {
687 move_input(context, in, 1);
688 /* sections to ignore */
689 if (!strncmp(in, "--", 2)) {
690 /* comment */
691 move_input(context, in, 2);
692 sectname = "Comment";
693 endtag = "-->";
694 endtag_len = 3;
695 } else if (!strncmp(in, "[CDATA[", 7)) {
696 /* CDATA section */
697 move_input(context, in, 7);
698 sectname = "CData";
699 endtag = "]]>";
700 endtag_len = 3;
701 } else if (!strncmp(in, "DOCTYPE", 7)) {
702 /* Document type declaration - not supported */
703 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP, "Document Type Declaration");
704 return LY_EVALID;
Radek Krejcic5c31bb2019-04-08 14:40:52 +0200705 } else {
706 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Unknown XML section \"%.20s\".", &in[-2]);
707 return LY_EVALID;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200708 }
709 in = ign_todelim(in, endtag, endtag_len, &newlines);
710 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, sectname), LY_EVALID);
711 context->line += newlines;
712 in += endtag_len;
713 } else if (in[0] == '?') {
714 in = ign_todelim(in, "?>", 2, &newlines);
715 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
716 context->line += newlines;
717 in += 2;
Radek Krejcib1890642018-10-03 14:05:40 +0200718 } else if (in[0] == '/') {
719 /* closing element */
720 closing = true;
721 ++in;
722 goto element;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200723 } else {
724 /* element */
Radek Krejcib1890642018-10-03 14:05:40 +0200725element:
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200726 ign_xmlws(context, in);
727 LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
728
729 /* remember the identifier start before checking its format */
730 id = in;
731 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
732 LY_CHECK_RET(rc);
733 if (c == ':') {
734 /* we have prefixed identifier */
735 endtag = in - endtag_len;
736
737 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
738 LY_CHECK_RET(rc);
739
740 (*prefix) = id;
741 (*prefix_len) = endtag - id;
742 id = endtag + 1;
743 }
744 if (!is_xmlws(c) && c != '/' && c != '>') {
745 in = in - endtag_len;
Radek Krejcid972c252018-09-25 13:23:39 +0200746 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in), in,
747 "whitespace or element tag termination ('>' or '/>'");
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200748 return LY_EVALID;
749 }
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200750 (*name) = id;
Radek Krejcib1890642018-10-03 14:05:40 +0200751 (*name_len) = in - endtag_len - id;
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200752
Radek Krejcib1890642018-10-03 14:05:40 +0200753 if (is_xmlws(c)) {
754 /* go to the next meaningful input */
755 ign_xmlws(context, in);
756 LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
757 c = in[0];
758 ++in;
759 endtag_len = 1;
760 }
761
762 if (closing) {
763 /* match opening and closing element tags */
764 LY_CHECK_ERR_RET(
765 !context->elements.count,
Radek Krejci3fbc9872019-04-16 16:50:01 +0200766 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 +0200767 LY_EVALID);
768 e = (struct lyxml_elem*)context->elements.objs[context->elements.count - 1];
769 LY_CHECK_ERR_RET(e->prefix_len != *prefix_len || e->name_len != *name_len
770 || (*prefix_len && strncmp(*prefix, e->prefix, e->prefix_len)) || strncmp(*name, e->name, e->name_len),
Radek Krejci3fbc9872019-04-16 16:50:01 +0200771 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 +0200772 LY_EVALID);
773 /* opening and closing element tags matches, remove record from the opening tags list */
774 free(e);
775 --context->elements.count;
Radek Krejci17a78d82019-05-15 15:49:55 +0200776
777 /* remove also the namespaces conneted with the element */
Radek Krejci17dca992019-05-17 10:53:27 +0200778 lyxml_ns_rm(context);
Radek Krejci17a78d82019-05-15 15:49:55 +0200779
Radek Krejcib1890642018-10-03 14:05:40 +0200780 /* do not return element information to announce closing element being currently processed */
781 *name = *prefix = NULL;
782 *name_len = *prefix_len = 0;
783
784 if (c == '>') {
785 /* end of closing element */
786 context->status = LYXML_ELEMENT;
787 } else {
788 in -= endtag_len;
789 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX, "Unexpected data \"%.*s\" in closing element tag.",
790 LY_VCODE_INSTREXP_len(in), in);
791 return LY_EVALID;
792 }
793 } else {
794 if (c == '>') {
795 /* end of opening element */
796 context->status = LYXML_ELEM_CONTENT;
797 } else if (c == '/' && in[0] == '>') {
798 /* empty element closing */
799 context->status = LYXML_ELEMENT;
800 ++in;
801 } else {
802 /* attribute */
803 context->status = LYXML_ATTRIBUTE;
804 in -= endtag_len;
805 }
806
807 if (context->status != LYXML_ELEMENT) {
808 /* store element opening tag information */
809 e = malloc(sizeof *e);
810 LY_CHECK_ERR_RET(!e, LOGMEM(ctx), LY_EMEM);
811 e->name = *name;
812 e->prefix = *prefix;
813 e->name_len = *name_len;
814 e->prefix_len = *prefix_len;
815 ly_set_add(&context->elements, e, LY_SET_OPT_USEASLIST);
816 }
817 }
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200818 loop = false;
819 }
820 }
821
822success:
823 /* move caller's input */
824 (*input) = in;
825 return LY_SUCCESS;
826}
827
Radek Krejcib1890642018-10-03 14:05:40 +0200828
829void
830lyxml_context_clear(struct lyxml_context *context)
831{
832 unsigned int u;
833
834 ly_set_erase(&context->elements, free);
835 for (u = context->ns.count - 1; u + 1 > 0; --u) {
836 /* remove the ns structure */
837 free(((struct lyxml_ns *)context->ns.objs[u])->prefix);
838 free(((struct lyxml_ns *)context->ns.objs[u])->uri);
839 free(context->ns.objs[u]);
840 }
841 ly_set_erase(&context->ns, NULL);
842}
Radek Krejcie7b95092019-05-15 11:03:07 +0200843
844LY_ERR
845lyxml_dump_text(struct lyout *out, const char *text, int attribute)
846{
847 LY_ERR ret = LY_SUCCESS;
848 unsigned int u;
849
850 if (!text) {
851 return 0;
852 }
853
854 for (u = 0; text[u]; u++) {
855 switch (text[u]) {
856 case '&':
857 ret = ly_print(out, "&amp;");
858 break;
859 case '<':
860 ret = ly_print(out, "&lt;");
861 break;
862 case '>':
863 /* not needed, just for readability */
864 ret = ly_print(out, "&gt;");
865 break;
866 case '"':
867 if (attribute) {
868 ret = ly_print(out, "&quot;");
869 break;
870 }
871 /* falls through */
872 default:
873 ly_write(out, &text[u], 1);
874 }
875 }
876
877 return ret;
878}
879