blob: c4bdb4bd6c8c823eb417b4b85ef52348ae4a94db [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
15#include <stdbool.h>
16#include <stdint.h>
17
18#include "libyang.h"
19#include "xml.h"
20#include "common.h"
21
22/* Macro to test if character is whitespace */
23#define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd)
24
25/* Macro to test if character is allowed to be a first character of an qualified identifier */
26#define is_xmlqnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \
27 (c >= 'A' && c <= 'Z') || /* c == ':' || */ \
28 (c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \
29 (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
30 c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \
31 (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
32 (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
33 (c >= 0x10000 && c <= 0xeffff))
34
35/* Macro to test if character is allowed to be used in an qualified identifier */
36#define is_xmlqnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \
37 (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || /* c == ':' || */ \
38 c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\
39 (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \
40 c == 0x200d || (c >= 0x300 && c <= 0x36f) || \
41 (c >= 0x2070 && c <= 0x218f) || (c >= 0x2030f && c <= 0x2040) || \
42 (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \
43 (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \
44 (c >= 0x10000 && c <= 0xeffff))
45
46/* Move input p by s characters, if EOF log with lyxml_context c */
47#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)
48
49/* Ignore whitespaces in the input string p, if EOF log with lyxml_context c */
50#define ign_xmlws(c,p) while (is_xmlws(*(p))) {if (*(p) == '\n') {++c->line;} ++p;}
51
52static const char *
53ign_todelim(register const char *input, const char *delim, size_t delim_len, size_t *newlines)
54{
55 size_t i;
56 register const char *a, *b;
57
58 (*newlines) = 0;
59 for ( ; *input; ++input) {
60 if (*input != *delim) {
61 if (*input == '\n') {
62 ++(*newlines);
63 }
64 continue;
65 }
66 a = input;
67 b = delim;
68 for (i = 0; i < delim_len; ++i) {
69 if (*a++ != *b++) {
70 break;
71 }
72 }
73 if (i == delim_len) {
74 return input;
75 }
76 }
77 return NULL;
78}
79
80static LY_ERR
81lyxml_getutf8(const char **input, unsigned int *utf8_char, size_t *bytes_read)
82{
83 unsigned int c, len;
84 int aux;
85 int i;
86
87 c = (*input)[0];
88 LY_CHECK_RET(!c, LY_EINVAL);
89
90 /* process character byte(s) */
91 if ((c & 0xf8) == 0xf0) {
92 /* four bytes character */
93 len = 4;
94
95 c &= 0x07;
96 for (i = 1; i <= 3; i++) {
97 aux = (*input)[i];
98 if ((aux & 0xc0) != 0x80) {
99 return LY_EINVAL;
100 }
101
102 c = (c << 6) | (aux & 0x3f);
103 }
104
105 if (c < 0x1000 || c > 0x10ffff) {
106 return LY_EINVAL;
107 }
108 } else if ((c & 0xf0) == 0xe0) {
109 /* three bytes character */
110 len = 3;
111
112 c &= 0x0f;
113 for (i = 1; i <= 2; i++) {
114 aux = (*input)[i];
115 if ((aux & 0xc0) != 0x80) {
116 return LY_EINVAL;
117 }
118
119 c = (c << 6) | (aux & 0x3f);
120 }
121
122 if (c < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) {
123 return LY_EINVAL;
124 }
125 } else if ((c & 0xe0) == 0xc0) {
126 /* two bytes character */
127 len = 2;
128
129 aux = (*input)[1];
130 if ((aux & 0xc0) != 0x80) {
131 return LY_EINVAL;
132 }
133 c = ((c & 0x1f) << 6) | (aux & 0x3f);
134
135 if (c < 0x80) {
136 return LY_EINVAL;
137 }
138 } else if (!(c & 0x80)) {
139 /* one byte character */
140 len = 1;
141
142 if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) {
143 return LY_EINVAL;
144 }
145 } else {
146 return LY_EINVAL;
147 }
148
149 (*utf8_char) = c;
150 (*input) += len;
151 if (bytes_read) {
152 (*bytes_read) = len;
153 }
154 return LY_SUCCESS;
155}
156
157LY_ERR
158lyxml_check_qname(struct lyxml_context *context, const char **input, unsigned int *term_char, size_t *term_char_len)
159{
160 unsigned int c;
161 const char *id = (*input);
162 LY_ERR rc;
163
164 /* check NameStartChar (minus colon) */
165 LY_CHECK_ERR_RET(lyxml_getutf8(input, &c, NULL) != LY_SUCCESS,
166 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID);
167 LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c),
168 LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LYVE_SYNTAX,
169 "Identifier \"%s\" starts with invalid character.", id),
170 LY_EVALID);
171
172 /* check rest of the identifier */
173 for (rc = lyxml_getutf8(input, &c, term_char_len);
174 rc == LY_SUCCESS && is_xmlqnamechar(c);
175 rc = lyxml_getutf8(input, &c, term_char_len));
176 LY_CHECK_ERR_RET(rc != LY_SUCCESS, LOGVAL(context->ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, (*input)[0]), LY_EVALID);
177
178 (*term_char) = c;
179 return LY_SUCCESS;
180}
181
182LY_ERR
183lyxml_get_element(struct lyxml_context *context, const char **input, int UNUSED(options),
184 const char **prefix, size_t *prefix_len, const char **name, size_t *name_len)
185{
186 struct ly_ctx *ctx = context->ctx; /* shortcut */
187 const char *in = (*input);
188 const char *endtag;
189 const char *sectname;
190 const char *id;
191 size_t endtag_len, newlines;
192 bool loop = true;
193 unsigned int c;
194 LY_ERR rc;
195 uint32_t x;
196
197 /* initialize output variables */
198 (*prefix) = (*name) = NULL;
199 (*prefix_len) = (*name_len) = 0;
200
201 while (loop) {
202 ign_xmlws(context, in);
203
204 if (in[0] == '\0') {
205 /* EOF */
206 goto success;
207 } else if (in[0] != '<') {
208 return LY_EINVAL;
209 }
210 move_input(context, in, 1);
211
212 if (in[0] == '!') {
213 move_input(context, in, 1);
214 /* sections to ignore */
215 if (!strncmp(in, "--", 2)) {
216 /* comment */
217 move_input(context, in, 2);
218 sectname = "Comment";
219 endtag = "-->";
220 endtag_len = 3;
221 } else if (!strncmp(in, "[CDATA[", 7)) {
222 /* CDATA section */
223 move_input(context, in, 7);
224 sectname = "CData";
225 endtag = "]]>";
226 endtag_len = 3;
227 } else if (!strncmp(in, "DOCTYPE", 7)) {
228 /* Document type declaration - not supported */
229 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NSUPP, "Document Type Declaration");
230 return LY_EVALID;
231 }
232 in = ign_todelim(in, endtag, endtag_len, &newlines);
233 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, sectname), LY_EVALID);
234 context->line += newlines;
235 in += endtag_len;
236 } else if (in[0] == '?') {
237 in = ign_todelim(in, "?>", 2, &newlines);
238 LY_CHECK_ERR_RET(!in, LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
239 context->line += newlines;
240 in += 2;
241 } else {
242 /* element */
243 ign_xmlws(context, in);
244 LY_CHECK_ERR_RET(!in[0], LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_EOF), LY_EVALID);
245
246 /* remember the identifier start before checking its format */
247 id = in;
248 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
249 LY_CHECK_RET(rc);
250 if (c == ':') {
251 /* we have prefixed identifier */
252 endtag = in - endtag_len;
253
254 rc = lyxml_check_qname(context, &in, &c, &endtag_len);
255 LY_CHECK_RET(rc);
256
257 (*prefix) = id;
258 (*prefix_len) = endtag - id;
259 id = endtag + 1;
260 }
261 if (!is_xmlws(c) && c != '/' && c != '>') {
262 in = in - endtag_len;
263 x = 0;
264 memcpy(&x, in, endtag_len);
265 LOGVAL(ctx, LY_VLOG_LINE, &context->line, LY_VCODE_INCHAR, x);
266 return LY_EVALID;
267 }
268 in = in - endtag_len;
269 (*name) = id;
270 (*name_len) = in - id;
271
272 loop = false;
273 }
274 }
275
276success:
277 /* move caller's input */
278 (*input) = in;
279 return LY_SUCCESS;
280}
281