blob: 70431ea1daa8aaa7c29e565ad427586fd24fddda [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
18#include <stdint.h>
19
20#include "context.h"
21#include "set.h"
22
Radek Krejcib1646a92018-11-02 16:08:26 +010023/* Macro to test if character is whitespace */
24#define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd)
25
26/* Macro to test if character is allowed to be a first character of an qualified identifier */
27#define is_xmlqnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \
28 (c >= 'A' && c <= 'Z') || /* c == ':' || */ \
29 (c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \
30 (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
31 c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \
32 (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
33 (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
34 (c >= 0x10000 && c <= 0xeffff))
35
36/* Macro to test if character is allowed to be used in an qualified identifier */
37#define is_xmlqnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \
38 (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || /* c == ':' || */ \
39 c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\
40 (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
41 c == 0x200d || (c >= 0x300 && c <= 0x36f) || \
42 (c >= 0x2070 && c <= 0x218f) || (c >= 0x2030f && c <= 0x2040) || \
43 (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
44 (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
45 (c >= 0x10000 && c <= 0xeffff))
46
Radek Krejcid91dbaf2018-09-21 15:51:39 +020047struct lyxml_ns {
Radek Krejci4b74d5e2018-09-26 14:30:55 +020048 const char *element; /* element where the namespace is defined */
49 char *prefix; /* prefix of the namespace, NULL for the default namespace */
50 char *uri; /* namespace URI */
Radek Krejcie0734d22019-04-05 15:54:28 +020051 unsigned int element_depth; /* depth level of the element to distinguish parent-child elements of the same name */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020052};
53
Radek Krejcib1890642018-10-03 14:05:40 +020054/* element tag identifier for matching opening and closing tags */
55struct lyxml_elem {
56 const char *prefix;
57 const char *name;
58 size_t prefix_len;
59 size_t name_len;
60};
61
Radek Krejci28e8cb52019-03-08 11:31:31 +010062/**
63 * @brief Status of the parser providing information what is expected next (which function is supposed to be called).
64 */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020065enum LYXML_PARSER_STATUS {
Radek Krejcib1890642018-10-03 14:05:40 +020066 LYXML_ELEMENT, /* expecting XML element, call lyxml_get_element() */
67 LYXML_ELEM_CONTENT, /* expecting content of an element, call lyxml_get_string */
68 LYXML_ATTRIBUTE, /* expecting XML attribute, call lyxml_get_attribute() */
69 LYXML_ATTR_CONTENT, /* expecting value of an attribute, call lyxml_get_string */
70 LYXML_END /* end of input data */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020071};
72
73struct lyxml_context {
74 struct ly_ctx *ctx;
75 uint64_t line;
Radek Krejcib1890642018-10-03 14:05:40 +020076 enum LYXML_PARSER_STATUS status; /* status providing information about the next expected object in input data */
77 struct ly_set elements; /* list of not-yet-closed elements */
Radek Krejci4b74d5e2018-09-26 14:30:55 +020078 struct ly_set ns; /* handled with LY_SET_OPT_USEASLIST */
Radek Krejcid91dbaf2018-09-21 15:51:39 +020079};
80
Radek Krejci4b74d5e2018-09-26 14:30:55 +020081/**
82 * @brief Parse input expecting an XML element.
83 *
84 * Able to silently skip comments, PIs and CData. DOCTYPE is not parsable, so it is reported as LY_EVALID error.
85 * If '<' is not found in input, LY_EINVAL is returned (but no error is logged), so it is possible to continue
86 * with parsing input as text content.
87 *
88 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
89 * is returned.
90 *
91 * @param[in] context XML context to track lines or store errors into libyang context.
92 * @param[in,out] input Input string to process, updated according to the processed/read data.
93 * @param[in] options Currently unused options to modify input processing.
94 * @param[out] prefix Pointer to prefix if present in the element name, NULL otherwise.
95 * @param[out] prefix_len Length of the prefix if any.
Radek Krejcib1890642018-10-03 14:05:40 +020096 * @param[out] name Element name. When LY_SUCCESS is returned but name is NULL, check context's status field:
97 * - LYXML_END - end of input was reached
Radek Krejci28e8cb52019-03-08 11:31:31 +010098 * - LYXML_ELEMENT - closing element found, expecting now a sibling element so call lyxml_get_element() again
Radek Krejci4b74d5e2018-09-26 14:30:55 +020099 * @param[out] name_len Length of the element name.
100 * @return LY_ERR values.
101 */
102LY_ERR lyxml_get_element(struct lyxml_context *context, const char **input,
103 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len);
104
105/**
106 * @brief Parse input expecting an XML attribute (including XML namespace).
107 *
108 * Input string is not being modified, so the returned values are not NULL-terminated, instead their length
109 * is returned.
110 *
111 * In case of a namespace definition, prefix just contains xmlns string. In case of the default namespace,
112 * prefix is NULL and the attribute name is xmlns.
113 *
114 * @param[in] context XML context to track lines or store errors into libyang context.
115 * @param[in,out] input Input string to process, updated according to the processed/read data so,
116 * when succeeded, it points to the opening quote of the attribute's value.
117 * @param[out] prefix Pointer to prefix if present in the attribute name, NULL otherwise.
118 * @param[out] prefix_len Length of the prefix if any.
Radek Krejci28e8cb52019-03-08 11:31:31 +0100119 * @param[out] name Attribute name.
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200120 * @param[out] name_len Length of the element name.
121 * @return LY_ERR values.
122 */
123LY_ERR lyxml_get_attribute(struct lyxml_context *context, const char **input,
124 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len);
125
126/**
127 * @brief Parse input as XML text (attribute's values and element's content).
128 *
129 * Mixed content of XML elements is not allowed. Formating whitespaces before child element are ignored,
Radek Krejcid70d1072018-10-09 14:20:47 +0200130 * 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 +0200131 * to the beginning of a child definition.
132 *
133 * In the case of attribute's values, the input string is expected to start on a quotation mark to
134 * select which delimiter (single or double quote) is used. Otherwise, the element content is being
135 * parsed expected to be terminated by '<' character.
136 *
Radek Krejcid70d1072018-10-09 14:20:47 +0200137 * If function succeeds, the string in a dynamically allocated output buffer is always NULL-terminated.
138 *
139 * The dynamically allocated buffer is used only when necessary because of a character or the supported entity
140 * reference which modify the input data. These constructs are replaced by their real value, so in case the output
141 * 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 +0200142 *
143 * @param[in] context XML context to track lines or store errors into libyang context.
144 * @param[in,out] input Input string to process, updated according to the processed/read data.
Radek Krejcid70d1072018-10-09 14:20:47 +0200145 * @param[in, out] buffer Storage for the output string. If the parameter points to NULL, the buffer is allocated if needed.
146 * Otherwise, when needed, the buffer is used and enlarged when necessary. Whenever the buffer is used, the string is NULL-terminated.
147 * @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 +0200148 * 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 +0200149 * @param[out] output Returns pointer to the resulting string - to the provided/allocated buffer if it was necessary to modify
150 * the input string or directly into the input string (see the \p dynamic parameter).
151 * @param[out] length Length of the \p output string.
152 * @param[out] dynamic Flag if a dynamically allocated memory (\p buffer) was used and caller is supposed to free it at the end.
153 * In case the value is zero, the \p output points directly into the \p input string.
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200154 * @return LY_ERR value.
155 */
Radek Krejcid70d1072018-10-09 14:20:47 +0200156LY_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 +0200157
158/**
159 * @brief Add namespace definition into XML context.
160 *
161 * Namespaces from a single element are supposed to be added sequentially together (not interleaved by a namespace from other
162 * element). This mimic namespace visibility, since the namespace defined in element E is not visible from its parents or
163 * siblings. On the other hand, namespace from a parent element can be redefined in a child element. This is also reflected
164 * by lyxml_ns_get() which returns the most recent namespace definition for the given prefix.
165 *
166 * When leaving processing of a subtree of some element, caller is supposed to call lyxml_ns_rm() to remove all the namespaces
167 * defined in such an element from the context.
168 *
169 * @param[in] context XML context to work with.
170 * @param[in] element_name Pointer to the element name where the namespace is defined. Serve as an identifier to select
171 * which namespaces are supposed to be removed via lyxml_ns_rm() when leaving the element's subtree.
172 * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute(). Can be NULL for default namespace.
173 * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from lyxml_get_attribute()).
174 * @param[in] uri Namespace URI (value) to store. Value can be obtained via lyxml_get_string() and caller is not supposed to
175 * work with the pointer when the function succeeds.
176 * @return LY_ERR values.
177 */
178LY_ERR lyxml_ns_add(struct lyxml_context *context, const char *element_name, const char *prefix, size_t prefix_len, char *uri);
179
180/**
181 * @brief Get a namespace record for the given prefix in the current context.
182 *
183 * @param[in] context XML context to work with.
184 * @param[in] prefix Pointer to the namespace prefix as taken from lyxml_get_attribute() or lyxml_get_element().
185 * Can be NULL for default namespace.
186 * @param[in] prefix_len Length of the prefix string (since it is not NULL-terminated when returned from lyxml_get_attribute() or
187 * lyxml_get_element()).
188 * @return The namespace record or NULL if the record for the specified prefix not found.
189 */
190const struct lyxml_ns *lyxml_ns_get(struct lyxml_context *context, const char *prefix, size_t prefix_len);
191
192/**
193 * @brief Remove all the namespaces defined in the given element.
194 *
195 * @param[in] context XML context to work with.
196 * @param[in] element_name Pointer to the element name where the namespaces are defined. Serve as an identifier previously provided
197 * by lyxml_get_element()
198 * @return LY_ERR values.
199 */
200LY_ERR lyxml_ns_rm(struct lyxml_context *context, const char *element_name);
Radek Krejcib416be62018-10-01 14:51:45 +0200201
Radek Krejcib1890642018-10-03 14:05:40 +0200202/**
203 * @brief Remove the allocated working memory of the context.
204 *
205 * @param[in] context XML context to clear.
206 */
207void lyxml_context_clear(struct lyxml_context *context);
208
Radek Krejcib416be62018-10-01 14:51:45 +0200209#endif /* LY_XML_H_ */