blob: 8e30478449435867fa073f19d2a681e60182f5a0 [file] [log] [blame]
Radek Krejcid91dbaf2018-09-21 15:51:39 +02001/**
2 * @file xml.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Generic XML parser routines.
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
15#ifndef LY_XML_H_
16#define LY_XML_H_
17
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <stddef.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020019#include <stdint.h>
20
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "log.h"
Radek Krejcid91dbaf2018-09-21 15:51:39 +020022#include "set.h"
Michal Vasko44685da2020-03-17 15:38:06 +010023#include "tree_schema.h"
Radek Krejcid91dbaf2018-09-21 15:51:39 +020024
Radek Krejcie7b95092019-05-15 11:03:07 +020025struct lyout;
Michal Vasko52927e22020-03-16 17:26:14 +010026struct ly_prefix;
Radek Krejcie7b95092019-05-15 11:03:07 +020027
Radek Krejcib1646a92018-11-02 16:08:26 +010028/* Macro to test if character is whitespace */
29#define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd)
30
31/* Macro to test if character is allowed to be a first character of an qualified identifier */
32#define is_xmlqnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \
33 (c >= 'A' && c <= 'Z') || /* c == ':' || */ \
34 (c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \
35 (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
36 c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \
37 (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
38 (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
39 (c >= 0x10000 && c <= 0xeffff))
40
41/* Macro to test if character is allowed to be used in an qualified identifier */
42#define is_xmlqnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \
43 (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || /* c == ':' || */ \
44 c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\
45 (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
46 c == 0x200d || (c >= 0x300 && c <= 0x36f) || \
47 (c >= 0x2070 && c <= 0x218f) || (c >= 0x2030f && c <= 0x2040) || \
48 (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
49 (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
50 (c >= 0x10000 && c <= 0xeffff))
51
Radek Krejcid91dbaf2018-09-21 15:51:39 +020052struct lyxml_ns {
Radek Krejci4b74d5e2018-09-26 14:30:55 +020053 char *prefix; /* prefix of the namespace, NULL for the default namespace */
54 char *uri; /* namespace URI */
Radek Krejci17a78d82019-05-15 15:49:55 +020055 unsigned int depth; /* depth level of the element to maintain the list of accessible namespace definitions */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020056};
57
Radek Krejcib1890642018-10-03 14:05:40 +020058/* element tag identifier for matching opening and closing tags */
59struct lyxml_elem {
60 const char *prefix;
61 const char *name;
62 size_t prefix_len;
63 size_t name_len;
64};
65
Radek Krejci28e8cb52019-03-08 11:31:31 +010066/**
67 * @brief Status of the parser providing information what is expected next (which function is supposed to be called).
68 */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020069enum LYXML_PARSER_STATUS {
Michal Vasko9b368d32020-02-14 13:53:31 +010070 LYXML_ELEMENT = 0, /* expecting XML element, call lyxml_get_element() */
Radek Krejcib1890642018-10-03 14:05:40 +020071 LYXML_ELEM_CONTENT, /* expecting content of an element, call lyxml_get_string */
72 LYXML_ATTRIBUTE, /* expecting XML attribute, call lyxml_get_attribute() */
73 LYXML_ATTR_CONTENT, /* expecting value of an attribute, call lyxml_get_string */
74 LYXML_END /* end of input data */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020075};
76
77struct lyxml_context {
78 struct ly_ctx *ctx;
79 uint64_t line;
Radek Krejcib1890642018-10-03 14:05:40 +020080 enum LYXML_PARSER_STATUS status; /* status providing information about the next expected object in input data */
81 struct ly_set elements; /* list of not-yet-closed elements */
Radek Krejci4b74d5e2018-09-26 14:30:55 +020082 struct ly_set ns; /* handled with LY_SET_OPT_USEASLIST */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020083};
84
Radek Krejci4b74d5e2018-09-26 14:30:55 +020085/**
86 * @brief Parse input expecting an XML element.
87 *
Radek Krejcie7b95092019-05-15 11:03:07 +020088 * Able to silently skip comments, PIs and CData. DOCTYPE is not parseable, so it is reported as LY_EVALID error.
Radek Krejci4b74d5e2018-09-26 14:30:55 +020089 * If '<' is not found in input, LY_EINVAL is returned (but no error is logged), so it is possible to continue
90 * with parsing input as text content.
91 *
92 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
93 * is returned.
94 *
95 * @param[in] context XML context to track lines or store errors into libyang context.
96 * @param[in,out] input Input string to process, updated according to the processed/read data.
97 * @param[in] options Currently unused options to modify input processing.
Michal Vasko44685da2020-03-17 15:38:06 +010098 * @param[out] prefix_p Pointer to prefix if present in the element name, NULL otherwise.
99 * @param[out] prefix_len_p Length of the prefix if any.
100 * @param[out] name_p Element name. When LY_SUCCESS is returned but name is NULL, check context's status field:
Radek Krejcib1890642018-10-03 14:05:40 +0200101 * - LYXML_END - end of input was reached
Radek Krejci28e8cb52019-03-08 11:31:31 +0100102 * - LYXML_ELEMENT - closing element found, expecting now a sibling element so call lyxml_get_element() again
Michal Vasko44685da2020-03-17 15:38:06 +0100103 * @param[out] name_len_p Length of the element name.
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200104 * @return LY_ERR values.
105 */
Michal Vasko44685da2020-03-17 15:38:06 +0100106LY_ERR lyxml_get_element(struct lyxml_context *context, const char **input, const char **prefix_p, size_t *prefix_len_p,
107 const char **name_p, size_t *name_len_p);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200108
109/**
Michal Vasko52927e22020-03-16 17:26:14 +0100110 * @brief Skip an element after its opening tag was parsed.
111 *
112 * @param[in] context XML context.
113 * @param[in,out] input Input string to process, updated according to the read data.
114 * @return LY_ERR values.
115 */
116LY_ERR lyxml_skip_element(struct lyxml_context *context, const char **input);
117
118/**
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200119 * @brief Parse input expecting an XML attribute (including XML namespace).
120 *
121 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
122 * is returned.
123 *
Radek Krejci17a78d82019-05-15 15:49:55 +0200124 * Namespace definitions are processed automatically and stored internally. To get namespace for a specific
125 * prefix, use lyxml_get_ns(). This also means, that in case there are only the namespace definitions,
126 * lyxml_get_attribute() can succeed, but nothing (name, prefix) is returned.
127 *
128 * The status member of the context is updated to provide information what the caller is supposed to call
129 * after this function.
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200130 *
131 * @param[in] context XML context to track lines or store errors into libyang context.
132 * @param[in,out] input Input string to process, updated according to the processed/read data so,
133 * when succeeded, it points to the opening quote of the attribute's value.
134 * @param[out] prefix Pointer to prefix if present in the attribute name, NULL otherwise.
135 * @param[out] prefix_len Length of the prefix if any.
Radek Krejci17a78d82019-05-15 15:49:55 +0200136 * @param[out] name Attribute name. Can be NULL only in case there is actually no attribute, but namespaces.
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200137 * @param[out] name_len Length of the element name.
138 * @return LY_ERR values.
139 */
140LY_ERR lyxml_get_attribute(struct lyxml_context *context, const char **input,
141 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len);
142
143/**
144 * @brief Parse input as XML text (attribute's values and element's content).
145 *
146 * Mixed content of XML elements is not allowed. Formating whitespaces before child element are ignored,
Radek Krejcid70d1072018-10-09 14:20:47 +0200147 * LY_EINVAL is returned in such a case (output is not set, no error is printed) and input is moved
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200148 * to the beginning of a child definition.
149 *
150 * In the case of attribute's values, the input string is expected to start on a quotation mark to
151 * select which delimiter (single or double quote) is used. Otherwise, the element content is being
152 * parsed expected to be terminated by '<' character.
153 *
Radek Krejcid70d1072018-10-09 14:20:47 +0200154 * If function succeeds, the string in a dynamically allocated output buffer is always NULL-terminated.
155 *
156 * The dynamically allocated buffer is used only when necessary because of a character or the supported entity
157 * reference which modify the input data. These constructs are replaced by their real value, so in case the output
158 * string will be again printed as an XML data, it may be necessary to correctly encode such characters.
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200159 *
Radek Krejciee4cab22019-07-17 17:07:47 +0200160 * Optionally, the buffer, buffer_size, output, length and dynamic arguments (altogether) can be NULL.
161 * In such a case, the XML text in @p input is just checked, the @p input pointer is moved after the XML text, but nothing is stored.
162 *
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200163 * @param[in] context XML context to track lines or store errors into libyang context.
164 * @param[in,out] input Input string to process, updated according to the processed/read data.
Radek Krejcid70d1072018-10-09 14:20:47 +0200165 * @param[in, out] buffer Storage for the output string. If the parameter points to NULL, the buffer is allocated if needed.
166 * Otherwise, when needed, the buffer is used and enlarged when necessary. Whenever the buffer is used, the string is NULL-terminated.
167 * @param[in, out] buffer_size Allocated size of the returned buffer. If a buffer is provided by a caller, it
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200168 * is not being reduced even if the string is shorter. On the other hand, it can be enlarged if needed.
Radek Krejcid70d1072018-10-09 14:20:47 +0200169 * @param[out] output Returns pointer to the resulting string - to the provided/allocated buffer if it was necessary to modify
170 * the input string or directly into the input string (see the \p dynamic parameter).
171 * @param[out] length Length of the \p output string.
172 * @param[out] dynamic Flag if a dynamically allocated memory (\p buffer) was used and caller is supposed to free it at the end.
173 * In case the value is zero, the \p output points directly into the \p input string.
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200174 * @return LY_ERR value.
175 */
Radek Krejcid70d1072018-10-09 14:20:47 +0200176LY_ERR lyxml_get_string(struct lyxml_context *context, const char **input, char **buffer, size_t *buffer_size, char **output, size_t *length, int *dynamic);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200177
178/**
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200179 * @brief Get a namespace record for the given prefix in the current context.
180 *
181 * @param[in] context XML context to work with.
182 * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute() or lyxml_get_element().
183 * 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() or
185 * lyxml_get_element()).
186 * @return The namespace record or NULL if the record for the specified prefix not found.
187 */
188const struct lyxml_ns *lyxml_ns_get(struct lyxml_context *context, const char *prefix, size_t prefix_len);
189
190/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200191 * @brief Print the given @p text as XML string which replaces some of the characters which cannot appear in XML data.
192 *
193 * @param[in] out Output structure for printing.
194 * @param[in] text String to print.
195 * @param[in] attribute Flag for attribute's value where a double quotes must be replaced.
196 * @return LY_ERR values.
197 */
198LY_ERR lyxml_dump_text(struct lyout *out, const char *text, int attribute);
199
200/**
Radek Krejcib1890642018-10-03 14:05:40 +0200201 * @brief Remove the allocated working memory of the context.
202 *
203 * @param[in] context XML context to clear.
204 */
205void lyxml_context_clear(struct lyxml_context *context);
206
Michal Vasko52927e22020-03-16 17:26:14 +0100207/**
208 * @brief Find all possible prefixes in a value.
209 *
210 * @param[in] ctx XML context to use.
211 * @param[in] value Value to check.
212 * @param[in] value_len Value length.
213 * @param[out] val_prefs Array of found prefixes.
214 * @return LY_ERR value.
215 */
216LY_ERR lyxml_get_prefixes(struct lyxml_context *ctx, const char *value, size_t value_len, struct ly_prefix **val_prefs);
217
218/**
219 * @brief Compare values and their prefix mappings.
220 *
221 * @param[in] value1 First value.
222 * @param[in] prefs1 First value prefixes.
223 * @param[in] value2 Second value.
224 * @param[in] prefs2 Second value prefixes.
225 * @return LY_SUCCESS if values are equal.
226 * @return LY_ENOT if values are not equal.
227 * @return LY_ERR on error.
228 */
229LY_ERR lyxml_value_compare(const char *value1, const struct ly_prefix *prefs1, const char *value2,
230 const struct ly_prefix *prefs2);
231
Michal Vasko44685da2020-03-17 15:38:06 +0100232void *lyxml_elem_dup(void *item);
233
234void *lyxml_ns_dup(void *item);
235
Radek Krejcib416be62018-10-01 14:51:45 +0200236#endif /* LY_XML_H_ */