]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/xml.c
Merge pull request #8700 from keszybz/hibernation
[thirdparty/systemd.git] / src / basic / xml.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <stddef.h>
10 #include <string.h>
11
12 #include "macro.h"
13 #include "string-util.h"
14 #include "xml.h"
15
16 enum {
17 STATE_NULL,
18 STATE_TEXT,
19 STATE_TAG,
20 STATE_ATTRIBUTE,
21 };
22
23 static void inc_lines(unsigned *line, const char *s, size_t n) {
24 const char *p = s;
25
26 if (!line)
27 return;
28
29 for (;;) {
30 const char *f;
31
32 f = memchr(p, '\n', n);
33 if (!f)
34 return;
35
36 n -= (f - p) + 1;
37 p = f + 1;
38 (*line)++;
39 }
40 }
41
42 /* We don't actually do real XML here. We only read a simplistic
43 * subset, that is a bit less strict that XML and lacks all the more
44 * complex features, like entities, or namespaces. However, we do
45 * support some HTML5-like simplifications */
46
47 int xml_tokenize(const char **p, char **name, void **state, unsigned *line) {
48 const char *c, *e, *b;
49 char *ret;
50 int t;
51
52 assert(p);
53 assert(*p);
54 assert(name);
55 assert(state);
56
57 t = PTR_TO_INT(*state);
58 c = *p;
59
60 if (t == STATE_NULL) {
61 if (line)
62 *line = 1;
63 t = STATE_TEXT;
64 }
65
66 for (;;) {
67 if (*c == 0)
68 return XML_END;
69
70 switch (t) {
71
72 case STATE_TEXT: {
73 int x;
74
75 e = strchrnul(c, '<');
76 if (e > c) {
77 /* More text... */
78 ret = strndup(c, e - c);
79 if (!ret)
80 return -ENOMEM;
81
82 inc_lines(line, c, e - c);
83
84 *name = ret;
85 *p = e;
86 *state = INT_TO_PTR(STATE_TEXT);
87
88 return XML_TEXT;
89 }
90
91 assert(*e == '<');
92 b = c + 1;
93
94 if (startswith(b, "!--")) {
95 /* A comment */
96 e = strstr(b + 3, "-->");
97 if (!e)
98 return -EINVAL;
99
100 inc_lines(line, b, e + 3 - b);
101
102 c = e + 3;
103 continue;
104 }
105
106 if (*b == '?') {
107 /* Processing instruction */
108
109 e = strstr(b + 1, "?>");
110 if (!e)
111 return -EINVAL;
112
113 inc_lines(line, b, e + 2 - b);
114
115 c = e + 2;
116 continue;
117 }
118
119 if (*b == '!') {
120 /* DTD */
121
122 e = strchr(b + 1, '>');
123 if (!e)
124 return -EINVAL;
125
126 inc_lines(line, b, e + 1 - b);
127
128 c = e + 1;
129 continue;
130 }
131
132 if (*b == '/') {
133 /* A closing tag */
134 x = XML_TAG_CLOSE;
135 b++;
136 } else
137 x = XML_TAG_OPEN;
138
139 e = strpbrk(b, WHITESPACE "/>");
140 if (!e)
141 return -EINVAL;
142
143 ret = strndup(b, e - b);
144 if (!ret)
145 return -ENOMEM;
146
147 *name = ret;
148 *p = e;
149 *state = INT_TO_PTR(STATE_TAG);
150
151 return x;
152 }
153
154 case STATE_TAG:
155
156 b = c + strspn(c, WHITESPACE);
157 if (*b == 0)
158 return -EINVAL;
159
160 inc_lines(line, c, b - c);
161
162 e = b + strcspn(b, WHITESPACE "=/>");
163 if (e > b) {
164 /* An attribute */
165
166 ret = strndup(b, e - b);
167 if (!ret)
168 return -ENOMEM;
169
170 *name = ret;
171 *p = e;
172 *state = INT_TO_PTR(STATE_ATTRIBUTE);
173
174 return XML_ATTRIBUTE_NAME;
175 }
176
177 if (startswith(b, "/>")) {
178 /* An empty tag */
179
180 *name = NULL; /* For empty tags we return a NULL name, the caller must be prepared for that */
181 *p = b + 2;
182 *state = INT_TO_PTR(STATE_TEXT);
183
184 return XML_TAG_CLOSE_EMPTY;
185 }
186
187 if (*b != '>')
188 return -EINVAL;
189
190 c = b + 1;
191 t = STATE_TEXT;
192 continue;
193
194 case STATE_ATTRIBUTE:
195
196 if (*c == '=') {
197 c++;
198
199 if (IN_SET(*c, '\'', '\"')) {
200 /* Tag with a quoted value */
201
202 e = strchr(c+1, *c);
203 if (!e)
204 return -EINVAL;
205
206 inc_lines(line, c, e - c);
207
208 ret = strndup(c+1, e - c - 1);
209 if (!ret)
210 return -ENOMEM;
211
212 *name = ret;
213 *p = e + 1;
214 *state = INT_TO_PTR(STATE_TAG);
215
216 return XML_ATTRIBUTE_VALUE;
217
218 }
219
220 /* Tag with a value without quotes */
221
222 b = strpbrk(c, WHITESPACE ">");
223 if (!b)
224 b = c;
225
226 ret = strndup(c, b - c);
227 if (!ret)
228 return -ENOMEM;
229
230 *name = ret;
231 *p = b;
232 *state = INT_TO_PTR(STATE_TAG);
233 return XML_ATTRIBUTE_VALUE;
234 }
235
236 t = STATE_TAG;
237 continue;
238 }
239
240 }
241
242 assert_not_reached("Bad state");
243 }