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