include/frx/frx_validate.h

filter: Statementstmt 94.8% 370 / 390 Branchbran 84.1% 362 / 430 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.2% 732 / 820 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_lex.h ↑ Summary include/frx/frx_xpath.h »
1 #ifndef FRX_VALIDATE_H
2 #define FRX_VALIDATE_H
3  
4 /* The validity constraints, over the finished tree.
5 *
6 * The parse read the declarations and applied the two that change what a
7 * document means: attribute defaults are already on the elements that
8 * lacked them, and attribute values are already normalised by their
9 * declared type. This is the rest, and it is a second pass rather than
10 * work threaded through the parse driver, because the tree the driver
11 * finished already has every element and every attribute including the
12 * defaulted ones, and one walk over it is simpler than automaton state
13 * carried down the parse. THE READER DOES NOT VALIDATE: an event stream
14 * has no finished tree to walk, and frx_reader.h says so.
15 *
16 * `validate => 1` stops at the first violation and reports it with its
17 * offset, in the same message shape as every other refusal. `validate
18 * => 'collect'` runs the whole pass, returns the document, and hangs the
19 * list off it in document order for `$doc->errors`. There is no warning
20 * channel: the specification's "at user option" errors are collected.
21 *
22 * THE ID INDEX. Validation knows which attributes are IDs because the
23 * DTD says so, which is a different question from the `id_attrs` option,
24 * and the two never both apply: a document parsed with `id_attrs` keeps
25 * that index and its source, and one validated without it gets an index
26 * built from the declared ID attributes, with `id_source` saying which,
27 * so XPath's id() does not have to guess. Uniqueness is a validity
28 * error here rather than the parse-time refusal `id_attrs` gives, and
29 * the reason is the same in both: which of two elements is "the" one is
30 * the whole of the signature wrapping attack.
31 *
32 * NAMESPACE DECLARATIONS ARE NOT ATTRIBUTES in this tree (frx_tree.h),
33 * and a DTD does not know that, so validation puts them back: Namespaces
34 * in XML is a layer above XML 1.0, and to the grammar `xmlns:foo="..."`
35 * is an Attribute like any other. It is therefore held to VC: Attribute
36 * Value Type like any other, and where the DTD does declare it, what the
37 * DTD says about its value is enforced too, which is what a #FIXED
38 * `xmlns` is written to say. The name is matched by comparison against
39 * the prefix, so asking the question allocates nothing.
40 *
41 * THAT IS STRICTER THAN IT LOOKS, and deliberately. A DTD that does not
42 * declare `xmlns` makes every namespaced document drawn from it invalid,
43 * which is a real friction between DTDs and namespaces and not a bug
44 * here: it is why a DTD written for namespaced markup declares the
45 * xmlns attributes it expects. The conformance suite pins it - eduni
46 * hst-bh-005 and hst-bh-006 are two elements whose only attribute is a
47 * namespace declaration, and the suite marks both invalid. It costs
48 * nothing unless `validate` is asked for, which is off by default.
49 *
50 * Nothing recurses (rule 2): the walk is frx_walk_next, and an element's
51 * content model is run over its own child list in one loop.
52 *
53 * Needs frx_tree.h, frx_dtd.h, frx_parse.h, frx_model.h. */
54  
55 typedef struct frx_verr {
56 const char *what; /* static; names the constraint */
57 size_t offset;
58 struct frx_verr *next;
59 } frx_verr;
60  
61 typedef struct frx_idref {
62 frx_str value;
63 size_t offset;
64 } frx_idref;
65  
66 typedef struct frx_val {
67 frx_doc *doc;
68 const frx_dtd *dtd;
69 frx_arena *arena;
70 frx_dtd_map models; /* element name to frx_cm_dfa * */
71 int collect; /* 0: stop at the first violation */
72 int standalone;
73 int own_ids; /* the index is ours to build (no id_attrs) */
74 frx_verr *first, *last;
75 int n;
76 int nomem;
77 int stop;
78 frx_idx *ids; int n_ids, cap_ids;
79 frx_idref *idrefs; int n_idrefs, cap_idrefs;
80 } frx_val;
81  
82 /* the type check, used by the declaration pass above the definition to
83 * check a declared default and by the document pass below it to check a
84 * written value */
85 static const char *frx_val_syntax_check(int type, const frx_str *enums,
86 int n_enums, const frx_str *value);
87  
88 /* One collected violation as a message, in the shape every refusal uses
89 * minus the context: the bytes the offset points into are gone by the
90 * time a caller reads the list. */
91 static size_t
92 264 frx_verr_format(const frx_verr *e, char *out, size_t cap)
93 {
94 264 size_t pos = 0;
95 264 frx_err_put(out, cap, &pos, FRX_ERR_PREFIX, sizeof FRX_ERR_PREFIX - 1);
96 264 frx_err_put(out, cap, &pos, e->what, strlen(e->what));
97 264 frx_err_put(out, cap, &pos, " at byte offset ", 16);
98 264 frx_err_put_num(out, cap, &pos, e->offset);
99 264 ▶ if (cap) out[pos < cap ? pos : cap - 1] = '\0';
Branch: 2 branches
BranchTrueFalseTotal
if (cap) out[pos < cap ? pos : cap - 1] = '\0'; 264 0 264
if (cap) out[pos < cap ? pos : cap - 1] = '\0'; 0 264 264
100 264 ▶ return pos < cap ? pos : cap - 1;
Branch: 1 branch
BranchTrueFalseTotal
return pos < cap ? pos : cap - 1; 0 264 264
101 }
102  
103 /* ---- names, section 2.3 ------------------------------------------------- */
104  
105 /* Nmtoken over UTF-8. frx_utf8.h's tables leave the colon out, because
106 * the lexer applies the QName rule to it itself; in an Nmtoken it is an
107 * ordinary name character, which is what XML 1.0 says it is, and which
108 * the erratum below does not narrow.
109 *
110 * A Name is not used for a tokenised attribute value. Namespaces in XML
111 * erratum NE08 narrows the six tokenised types - ID, IDREF, IDREFS,
112 * ENTITY, ENTITIES and NOTATION - to NCName in a namespace-aware
113 * document, and this parser is always namespace-aware, so `id="a:b"` is
114 * a violation and not a name with a colon in it. frx_is_ncname is that
115 * production. */
116 static int
117 184 frx_val_is_nmtoken(const frx_str *s)
118 {
119 unsigned long cp;
120 184 size_t at = 0, k;
121 184 ▶ if (!s->len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!s->len) return 0; 0 184 184
122 806 ▶ while (at < s->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (at < s->len) { 642 164 806
123 642 k = frx_utf8_decode((const unsigned char *)s->p + at, s->len - at, &cp);
124 642 ▶ if (!k || !(frx_is_name_char(cp) || cp == ':')) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!k || !(frx_is_name_char(cp) || cp == ':')) return 0; 642 0 642
if (!k || !(frx_is_name_char(cp) || cp == ':')) return 0; 26 616 642
if (!k || !(frx_is_name_char(cp) || cp == ':')) return 0; 20 6 26
125 622 at += k;
126 }
127 164 return 1;
128 }
129  
130 /* NE08: an NCName, so a colon is a violation and not a name character */
131 static int
132 2864 frx_val_is_name(const frx_str *s) { return frx_is_ncname(s->p, s->len); }
133  
134 /* The next token of a list value; 0 at the end. THE SEPARATOR IS A SPACE
135 * AND NOTHING ELSE: section 3.3.3 normalised this value by its declared
136 * type, which turned every literal tab, line feed and carriage return
137 * into a space and collapsed the runs, so a tab still in it arrived as a
138 * character reference and is part of the token, not between two. That is
139 * what makes `bar="abc&#9;xyz"` one token and not a name token. */
140 static int
141 327 frx_val_token(const frx_str *v, size_t *at, frx_str *out)
142 {
143 size_t start;
144 437 ▶ while (*at < v->len && v->p[*at] == ' ') (*at)++;
Branch: 2 branches
BranchTrueFalseTotal
while (*at < v->len && v->p[*at] == ' ') (*at)++; 342 95 437
while (*at < v->len && v->p[*at] == ' ') (*at)++; 110 232 342
145 327 start = *at;
146 1783 ▶ while (*at < v->len && v->p[*at] != ' ') (*at)++;
Branch: 2 branches
BranchTrueFalseTotal
while (*at < v->len && v->p[*at] != ' ') (*at)++; 1571 212 1783
while (*at < v->len && v->p[*at] != ' ') (*at)++; 1456 115 1571
147 327 ▶ if (*at == start) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*at == start) return 0; 95 232 327
148 232 out->p = v->p + start;
149 232 out->len = *at - start;
150 232 return 1;
151 }
152  
153 /* is this the name of a namespace declaration rather than an attribute? */
154 static int
155 2521 frx_val_is_xmlns(const frx_str *name)
156 {
157 2521 return frx_str_eq(name, "xmlns", 5)
158 2521 ▶ || (name->len > 6 && memcmp(name->p, "xmlns:", 6) == 0);
Branch: 3 branches
BranchTrueFalseTotal
|| (name->len > 6 && memcmp(name->p, "xmlns:", 6) == 0); 2521 0 2521
|| (name->len > 6 && memcmp(name->p, "xmlns:", 6) == 0); 28 2493 2521
|| (name->len > 6 && memcmp(name->p, "xmlns:", 6) == 0); 0 28 28
159 }
160  
161 /* The attribute definition, if any, for the declaration of this prefix.
162 * Built by comparison rather than by making the name, so nothing is
163 * allocated to ask the question. */
164 static const frx_attdef *
165 182 frx_val_nsdecl_attdef(const frx_eldecl *ed, const frx_str *prefix)
166 {
167 const frx_attdef *ad;
168 645 ▶ for (ad = ed->attdefs; ad; ad = ad->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (ad = ed->attdefs; ad; ad = ad->next) { 490 155 645
169 490 ▶ if (!prefix->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (!prefix->len) { 8 482 490
170 8 ▶ if (frx_str_eq(&ad->name, "xmlns", 5)) return ad;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(&ad->name, "xmlns", 5)) return ad; 8 0 8
171 482 ▶ } else if (ad->name.len == 6 + prefix->len
Branch: 1 branch
BranchTrueFalseTotal
} else if (ad->name.len == 6 + prefix->len 31 451 482
172 31 ▶ && memcmp(ad->name.p, "xmlns:", 6) == 0
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(ad->name.p, "xmlns:", 6) == 0 31 0 31
173 31 ▶ && memcmp(ad->name.p + 6, prefix->p, prefix->len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(ad->name.p + 6, prefix->p, prefix->len) == 0) { 19 12 31
174 19 return ad;
175 }
176 }
177 155 return NULL;
178 }
179  
180 static int
181 0 frx_val_has_nsdecl(const frx_node *n, const frx_str *name)
182 {
183 int i;
184 0 ▶ for (i = 0; i < n->n_decls; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->n_decls; i++) { 0 0 0
185 0 const frx_str *p = &n->decls[i].prefix;
186 0 ▶ if (!p->len) { if (frx_str_eq(name, "xmlns", 5)) return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!p->len) { if (frx_str_eq(name, "xmlns", 5)) return 1; } 0 0 0
if (!p->len) { if (frx_str_eq(name, "xmlns", 5)) return 1; } 0 0 0
187 0 ▶ else if (name->len == 6 + p->len && memcmp(name->p, "xmlns:", 6) == 0
Branch: 2 branches
BranchTrueFalseTotal
else if (name->len == 6 + p->len && memcmp(name->p, "xmlns:", 6) == 0 0 0 0
else if (name->len == 6 + p->len && memcmp(name->p, "xmlns:", 6) == 0 0 0 0
188 0 ▶ && memcmp(name->p + 6, p->p, p->len) == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(name->p + 6, p->p, p->len) == 0) return 1; 0 0 0
189 }
190 0 return 0;
191 }
192  
193 /* ---- reporting ---------------------------------------------------------- */
194  
195 /* Record a violation. Returns 0 when the pass should stop, which is
196 * always under `validate => 1` and never under collect. */
197 static int
198 752 frx_val_err(frx_val *v, const char *what, size_t offset)
199 {
200 752 frx_verr *e = (frx_verr *)frx_arena_alloc(v->arena, sizeof *e);
201 752 ▶ if (!e) { v->nomem = 1; v->stop = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { v->nomem = 1; v->stop = 1; return 0; } 0 752 752
202 752 e->what = what;
203 752 e->offset = offset;
204 752 e->next = NULL;
205 752 ▶ if (v->last) v->last->next = e;
Branch: 1 branch
BranchTrueFalseTotal
if (v->last) v->last->next = e; 197 555 752
206 555 else v->first = e;
207 752 v->last = e;
208 752 v->n++;
209 752 ▶ if (!v->collect) v->stop = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!v->collect) v->stop = 1; 71 681 752
210 752 return v->collect;
211 }
212  
213 /* The same, but placed by offset rather than appended, for a violation
214 * found before the walk that has to appear where it belongs in the list.
215 * The list is short and this runs once per exempted reference. */
216 static int
217 10 frx_val_err_ordered(frx_val *v, const char *what, size_t offset)
218 {
219 frx_verr *e, *prev, *cur;
220 10 ▶ if (!v->first || v->last->offset <= offset) return frx_val_err(v, what, offset);
Branch: 2 branches
BranchTrueFalseTotal
if (!v->first || v->last->offset <= offset) return frx_val_err(v, what, offset); 7 3 10
if (!v->first || v->last->offset <= offset) return frx_val_err(v, what, offset); 0 7 7
221 7 e = (frx_verr *)frx_arena_alloc(v->arena, sizeof *e);
222 7 ▶ if (!e) { v->nomem = 1; v->stop = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { v->nomem = 1; v->stop = 1; return 0; } 0 7 7
223 7 e->what = what;
224 7 e->offset = offset;
225 14 ▶ for (prev = NULL, cur = v->first; cur && cur->offset <= offset; prev = cur, cur = cur->next)
Branch: 2 branches
BranchTrueFalseTotal
for (prev = NULL, cur = v->first; cur && cur->offset <= offset; prev = cur, cur = cur->next) 14 0 14
for (prev = NULL, cur = v->first; cur && cur->offset <= offset; prev = cur, cur = cur->next) 7 7 14
226 ;
227 7 e->next = cur;
228 7 ▶ if (prev) prev->next = e;
Branch: 1 branch
BranchTrueFalseTotal
if (prev) prev->next = e; 7 0 7
229 0 else v->first = e;
230 7 v->n++;
231 7 ▶ if (!v->collect) v->stop = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!v->collect) v->stop = 1; 0 7 7
232 7 return v->collect;
233 }
234  
235 /* ---- the ID bookkeeping ------------------------------------------------- */
236  
237 static int
238 981 frx_val_push_id(frx_val *v, const frx_str *attr, const frx_str *value,
239 const frx_node *n, size_t offset)
240 {
241 981 ▶ if (v->n_ids == v->cap_ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->n_ids == v->cap_ids) { 198 783 981
242 198 ▶ int ncap = v->cap_ids ? v->cap_ids * 2 : 32;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = v->cap_ids ? v->cap_ids * 2 : 32; 12 186 198
243 198 frx_idx *ni = (frx_idx *)realloc(v->ids, (size_t)ncap * sizeof *ni);
244 198 ▶ if (!ni) { v->nomem = 1; v->stop = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ni) { v->nomem = 1; v->stop = 1; return 0; } 0 198 198
245 198 v->ids = ni;
246 198 v->cap_ids = ncap;
247 }
248 981 v->ids[v->n_ids].attr = *attr;
249 981 v->ids[v->n_ids].value = *value;
250 981 v->ids[v->n_ids].node = n;
251 981 v->ids[v->n_ids].offset = offset;
252 981 v->n_ids++;
253 981 return 1;
254 }
255  
256 static int
257 1792 frx_val_push_idref(frx_val *v, const frx_str *value, size_t offset)
258 {
259 1792 ▶ if (v->n_idrefs == v->cap_idrefs) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->n_idrefs == v->cap_idrefs) { 38 1754 1792
260 38 ▶ int ncap = v->cap_idrefs ? v->cap_idrefs * 2 : 32;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = v->cap_idrefs ? v->cap_idrefs * 2 : 32; 15 23 38
261 38 frx_idref *ni = (frx_idref *)realloc(v->idrefs, (size_t)ncap * sizeof *ni);
262 38 ▶ if (!ni) { v->nomem = 1; v->stop = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ni) { v->nomem = 1; v->stop = 1; return 0; } 0 38 38
263 38 v->idrefs = ni;
264 38 v->cap_idrefs = ncap;
265 }
266 1792 v->idrefs[v->n_idrefs].value = *value;
267 1792 v->idrefs[v->n_idrefs].offset = offset;
268 1792 v->n_idrefs++;
269 1792 return 1;
270 }
271  
272 /* an ID value already seen, whatever attribute carried it */
273 static int
274 2776 frx_val_id_seen(const frx_val *v, const frx_str *value)
275 {
276 int i;
277 254244 ▶ for (i = 0; i < v->n_ids; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < v->n_ids; i++) 253257 987 254244
278 253257 ▶ if (frx_str_eq2(&v->ids[i].value, value)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&v->ids[i].value, value)) return 1; 1789 251468 253257
279 987 return 0;
280 }
281  
282 /* ---- the declarations --------------------------------------------------- */
283  
284 static int
285 244 frx_val_notation_declared(const frx_val *v, const frx_str *name)
286 {
287 244 return frx_dtd_map_find(&v->dtd->notations, name) != NULL;
288 }
289  
290 /* Compile every content model once, and make the checks that are about
291 * the declarations rather than the document. */
292 static int
293 1199 frx_val_declarations(frx_val *v)
294 {
295 const frx_eldecl *ed;
296 int i;
297  
298 3878 ▶ for (ed = v->dtd->first_element; ed && !v->stop; ed = ed->next) {
Branch: 2 branches
BranchTrueFalseTotal
for (ed = v->dtd->first_element; ed && !v->stop; ed = ed->next) { 2693 1185 3878
for (ed = v->dtd->first_element; ed && !v->stop; ed = ed->next) { 2693 0 2693
299 const frx_attdef *ad;
300 2693 int n_id = 0, n_notation = 0;
301  
302 2693 ▶ if (!ed->declared) continue; /* an ATTLIST for an undeclared type */
Branch: 1 branch
BranchTrueFalseTotal
if (!ed->declared) continue; /* an ATTLIST for an undeclared type */ 2 2691 2693
303 2691 ▶ if (ed->declared > 1
Branch: 1 branch
BranchTrueFalseTotal
if (ed->declared > 1 3 2688 2691
304 3 ▶ && !frx_val_err(v, "the element type is declared more than once (VC: Unique Element Type Declaration)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "the element type is declared more than once (VC: Unique Element Type Declaration)", 1 2 3
305 4 ed->offset)) return 0;
306  
307 2690 ▶ if (ed->content == FRX_CONTENT_MIXED || ed->content == FRX_CONTENT_CHILDREN) {
Branch: 2 branches
BranchTrueFalseTotal
if (ed->content == FRX_CONTENT_MIXED || ed->content == FRX_CONTENT_CHILDREN) { 2133 557 2690
if (ed->content == FRX_CONTENT_MIXED || ed->content == FRX_CONTENT_CHILDREN) { 623 1510 2133
308 1180 frx_cm_dfa *d = NULL;
309 1180 switch (frx_model_build(v->arena, ed, &d)) {
310 1174 case FRX_MODEL_OK:
311 1174 ▶ if (!frx_dtd_map_add(v->arena, &v->models, &ed->name, d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_map_add(v->arena, &v->models, &ed->name, d)) { 0 1174 1174
312 3 v->nomem = 1; v->stop = 1; return 0;
313 }
314 1174 break;
315 4 case FRX_MODEL_DUPTYPE:
316 4 ▶ if (!frx_val_err(v, "a mixed content model names the same element type twice (VC: No Duplicate Types)",
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_err(v, "a mixed content model names the same element type twice (VC: No Duplicate Types)", 1 3 4
317 5 ed->offset)) return 0;
318 3 break;
319 2 case FRX_MODEL_NONDET:
320 2 ▶ if (!frx_val_err(v, "the content model is not deterministic: one name could match two positions (VC: Deterministic Content Model)",
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_err(v, "the content model is not deterministic: one name could match two positions (VC: Deterministic Content Model)", 2 0 2
321 4 ed->offset)) return 0;
322 0 break;
323 0 default:
324 0 v->nomem = 1; v->stop = 1; return 0;
325 }
326 }
327  
328 4874 ▶ for (ad = ed->attdefs; ad; ad = ad->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (ad = ed->attdefs; ad; ad = ad->next) { 2197 2677 4874
329 /* VC: Attribute Default Value Syntactically Correct. A
330 * default is a value of the declared type like any other,
331 * and the declaration is where a bad one is written. */
332 2197 ▶ if (ad->has_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ad->has_value) { 525 1672 2197
333 525 const char *bad = frx_val_syntax_check(ad->type, ad->enums, ad->n_enums, &ad->value);
334 525 ▶ if (bad
Branch: 1 branch
BranchTrueFalseTotal
if (bad 26 499 525
335 26 ▶ && !frx_val_err(v, "the default value declared for an attribute does not meet its type's constraints (VC: Attribute Default Value Syntactically Correct)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "the default value declared for an attribute does not meet its type's constraints (VC: Attribute Default Value Syntactically Correct)", 5 21 26
336 31 ad->offset)) return 0;
337 }
338 /* VC: No Duplicate Tokens */
339 2192 ▶ if (ad->type == FRX_ATT_ENUM || ad->type == FRX_ATT_NOTATION) {
Branch: 2 branches
BranchTrueFalseTotal
if (ad->type == FRX_ATT_ENUM || ad->type == FRX_ATT_NOTATION) { 1951 241 2192
if (ad->type == FRX_ATT_ENUM || ad->type == FRX_ATT_NOTATION) { 23 1928 1951
340 int j;
341 564 ▶ for (i = 1; i < ad->n_enums; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < ad->n_enums; i++) 301 263 564
342 673 ▶ for (j = 0; j < i; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < i; j++) 373 300 673
343 373 ▶ if (frx_str_eq2(&ad->enums[i], &ad->enums[j])
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&ad->enums[i], &ad->enums[j]) 4 369 373
344 4 ▶ && !frx_val_err(v, "an enumeration or notation list names the same token twice (VC: No Duplicate Tokens)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an enumeration or notation list names the same token twice (VC: No Duplicate Tokens)", 1 3 4
345 5 ad->offset)) return 0;
346 }
347 2191 ▶ if (ad->type == FRX_ATT_ID) {
Branch: 1 branch
BranchTrueFalseTotal
if (ad->type == FRX_ATT_ID) { 578 1613 2191
348 578 n_id++;
349 578 ▶ if (n_id > 1
Branch: 1 branch
BranchTrueFalseTotal
if (n_id > 1 3 575 578
350 3 ▶ && !frx_val_err(v, "an element type has more than one ID attribute (VC: One ID per Element Type)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an element type has more than one ID attribute (VC: One ID per Element Type)", 1 2 3
351 4 ad->offset)) return 0;
352 577 ▶ if (ad->def != FRX_DEF_IMPLIED && ad->def != FRX_DEF_REQUIRED
Branch: 2 branches
BranchTrueFalseTotal
if (ad->def != FRX_DEF_IMPLIED && ad->def != FRX_DEF_REQUIRED 40 537 577
if (ad->def != FRX_DEF_IMPLIED && ad->def != FRX_DEF_REQUIRED 4 36 40
353 4 ▶ && !frx_val_err(v, "an ID attribute must be #IMPLIED or #REQUIRED (VC: ID Attribute Default)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an ID attribute must be #IMPLIED or #REQUIRED (VC: ID Attribute Default)", 1 3 4
354 5 ad->offset)) return 0;
355 }
356 2189 ▶ if (ad->type == FRX_ATT_NOTATION) {
Branch: 1 branch
BranchTrueFalseTotal
if (ad->type == FRX_ATT_NOTATION) { 23 2166 2189
357 23 n_notation++;
358 23 ▶ if (n_notation > 1
Branch: 1 branch
BranchTrueFalseTotal
if (n_notation > 1 0 23 23
359 0 ▶ && !frx_val_err(v, "an element type has more than one NOTATION attribute (VC: One Notation per Element Type)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an element type has more than one NOTATION attribute (VC: One Notation per Element Type)", 0 0 0
360 0 ad->offset)) return 0;
361 23 ▶ if (ed->content == FRX_CONTENT_EMPTY
Branch: 1 branch
BranchTrueFalseTotal
if (ed->content == FRX_CONTENT_EMPTY 4 19 23
362 4 ▶ && !frx_val_err(v, "an element declared EMPTY may not have a NOTATION attribute (VC: No Notation on Empty Element)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an element declared EMPTY may not have a NOTATION attribute (VC: No Notation on Empty Element)", 1 3 4
363 5 ad->offset)) return 0;
364 59 ▶ for (i = 0; i < ad->n_enums; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ad->n_enums; i++) 38 21 59
365 38 ▶ if (!frx_val_notation_declared(v, &ad->enums[i])
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_notation_declared(v, &ad->enums[i]) 4 34 38
366 4 ▶ && !frx_val_err(v, "a NOTATION attribute names a notation that is not declared (VC: Notation Attributes)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "a NOTATION attribute names a notation that is not declared (VC: Notation Attributes)", 1 3 4
367 5 ad->offset)) return 0;
368 }
369 }
370 }
371 1185 return !v->stop;
372 }
373  
374 /* VC: Notation Declared, over the unparsed entities */
375 static int
376 1185 frx_val_notations(frx_val *v)
377 {
378 int i;
379 76993 ▶ for (i = 0; i < FRX_DTD_BUCKETS && !v->stop; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < FRX_DTD_BUCKETS && !v->stop; i++) { 75809 1184 76993
for (i = 0; i < FRX_DTD_BUCKETS && !v->stop; i++) { 75809 0 75809
380 const frx_dtd_slot *s;
381 77013 ▶ for (s = v->dtd->general.b[i]; s && !v->stop; s = s->next) {
Branch: 2 branches
BranchTrueFalseTotal
for (s = v->dtd->general.b[i]; s && !v->stop; s = s->next) { 1205 75808 77013
for (s = v->dtd->general.b[i]; s && !v->stop; s = s->next) { 1205 0 1205
382 1205 const frx_entity *e = (const frx_entity *)s->value;
383 1205 ▶ if (!e->unparsed) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e->unparsed) continue; 999 206 1205
384 206 ▶ if (!frx_val_notation_declared(v, &e->ndata)
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_notation_declared(v, &e->ndata) 9 197 206
385 9 ▶ && !frx_val_err(v, "an unparsed entity names a notation that is not declared (VC: Notation Declared)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an unparsed entity names a notation that is not declared (VC: Notation Declared)", 1 8 9
386 10 e->offset)) return 0;
387 }
388 }
389 1184 return !v->stop;
390 }
391  
392 /* ---- attributes --------------------------------------------------------- */
393  
394 /* THE SHAPE OF A VALUE UNDER ITS TYPE, and nothing that depends on the
395 * rest of the document: is it a Name, a list of Names, a name token, one
396 * of the enumerated tokens. A static message when it fails, NULL when it
397 * holds, no side effects.
398 *
399 * This is the whole of what a declared default has to satisfy at its
400 * declaration (VC: Attribute Default Value Syntactically Correct). The
401 * constraints that look at the rest of the document - does this ENTITY
402 * name a declared unparsed entity, does this IDREF resolve - apply to a
403 * default only when the default is actually used, which is the third
404 * edition's erratum E06, so they are checked over the tree, where a
405 * defaulted attribute is an attribute like any other and one the
406 * document supplied instead is not there to check. */
407 static const char *
408 5788 frx_val_syntax_check(int type, const frx_str *enums, int n_enums, const frx_str *value)
409 {
410 frx_str tok;
411 5788 size_t at = 0;
412 5788 int i, any = 0;
413  
414 5788 switch (type) {
415 995 case FRX_ATT_ID:
416 995 ▶ return frx_val_is_name(value) ? NULL : "an ID value is not an NCName (VC: ID, and Namespaces erratum NE08)";
Branch: 1 branch
BranchTrueFalseTotal
return frx_val_is_name(value) ? NULL : "an ID value is not an NCName (VC: ID, and Namespaces erratum NE08)"; 7 988 995
417  
418 1768 case FRX_ATT_IDREF:
419 1768 ▶ return frx_val_is_name(value) ? NULL : "an IDREF value is not an NCName (VC: IDREF, and Namespaces erratum NE08)";
Branch: 1 branch
BranchTrueFalseTotal
return frx_val_is_name(value) ? NULL : "an IDREF value is not an NCName (VC: IDREF, and Namespaces erratum NE08)"; 9 1759 1768
420  
421 21 case FRX_ATT_IDREFS:
422 60 ▶ while (frx_val_token(value, &at, &tok)) {
Branch: 1 branch
BranchTrueFalseTotal
while (frx_val_token(value, &at, &tok)) { 45 15 60
423 45 any = 1;
424 45 ▶ if (!frx_val_is_name(&tok))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_is_name(&tok)) 6 39 45
425 6 return "an IDREFS value holds something that is not an NCName (VC: IDREF, and Namespaces erratum NE08)";
426 }
427 15 ▶ return any ? NULL : "an IDREFS value is empty (VC: IDREF)";
Branch: 1 branch
BranchTrueFalseTotal
return any ? NULL : "an IDREFS value is empty (VC: IDREF)"; 1 14 15
428  
429 29 case FRX_ATT_ENTITY:
430 29 ▶ if (!frx_val_is_name(value))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_is_name(value)) 5 24 29
431 5 return "an ENTITY value is not an NCName (VC: Entity Name, and Namespaces erratum NE08)";
432 24 return NULL;
433  
434 15 case FRX_ATT_ENTITIES:
435 39 ▶ while (frx_val_token(value, &at, &tok)) {
Branch: 1 branch
BranchTrueFalseTotal
while (frx_val_token(value, &at, &tok)) { 27 12 39
436 27 any = 1;
437 27 ▶ if (!frx_val_is_name(&tok))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_is_name(&tok)) 3 24 27
438 3 return "an ENTITIES value holds something that is not an NCName (VC: Entity Name, and Namespaces erratum NE08)";
439 }
440 12 ▶ return any ? NULL : "an ENTITIES value is empty (VC: Entity Name)";
Branch: 1 branch
BranchTrueFalseTotal
return any ? NULL : "an ENTITIES value is empty (VC: Entity Name)"; 0 12 12
441  
442 100 case FRX_ATT_NMTOKEN:
443 100 return frx_val_is_nmtoken(value) ? NULL
444 100 ▶ : "an NMTOKEN value is not a name token (VC: Name Token)";
Branch: 1 branch
BranchTrueFalseTotal
: "an NMTOKEN value is not a name token (VC: Name Token)"; 8 92 100
445  
446 41 case FRX_ATT_NMTOKENS:
447 113 ▶ while (frx_val_token(value, &at, &tok)) {
Branch: 1 branch
BranchTrueFalseTotal
while (frx_val_token(value, &at, &tok)) { 84 29 113
448 84 any = 1;
449 84 ▶ if (!frx_val_is_nmtoken(&tok))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_is_nmtoken(&tok)) 12 72 84
450 12 return "an NMTOKENS value holds something that is not a name token (VC: Name Token)";
451 }
452 29 ▶ return any ? NULL : "an NMTOKENS value is empty (VC: Name Token)";
Branch: 1 branch
BranchTrueFalseTotal
return any ? NULL : "an NMTOKENS value is empty (VC: Name Token)"; 1 28 29
453  
454 547 case FRX_ATT_NOTATION:
455 case FRX_ATT_ENUM:
456 1074 ▶ for (i = 0; i < n_enums; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_enums; i++) 1058 16 1074
457 1058 ▶ if (frx_str_eq2(&enums[i], value)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&enums[i], value)) return NULL; 531 527 1058
458 return type == FRX_ATT_NOTATION
459 ? "a NOTATION value is not one of the notations declared for it (VC: Notation Attributes)"
460 16 ▶ : "an enumerated value is not one of the ones declared for it (VC: Enumeration)";
Branch: 1 branch
BranchTrueFalseTotal
: "an enumerated value is not one of the ones declared for it (VC: Enumeration)"; 5 11 16
461  
462 2272 default:
463 2272 return NULL; /* CDATA takes any value */
464 }
465 }
466  
467 /* what the rest of the document has to say about a value of this type */
468 static const char *
469 5196 frx_val_semantic_check(const frx_val *v, int type, const frx_str *value)
470 {
471 frx_str tok;
472 5196 size_t at = 0;
473 5196 ▶ if (type != FRX_ATT_ENTITY && type != FRX_ATT_ENTITIES) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (type != FRX_ATT_ENTITY && type != FRX_ATT_ENTITIES) return NULL; 5175 21 5196
if (type != FRX_ATT_ENTITY && type != FRX_ATT_ENTITIES) return NULL; 5163 12 5175
474 67 ▶ while (frx_val_token(value, &at, &tok)) {
Branch: 1 branch
BranchTrueFalseTotal
while (frx_val_token(value, &at, &tok)) { 42 25 67
475 42 const frx_entity *e = frx_dtd_entity(v->dtd, &tok, 0);
476 42 ▶ if (!e || !e->unparsed)
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !e->unparsed) 37 5 42
if (!e || !e->unparsed) 3 34 37
477 8 return "an ENTITY or ENTITIES value does not name an unparsed entity (VC: Entity Name)";
478 }
479 25 return NULL;
480 }
481  
482 static int
483 5239 frx_val_attr(frx_val *v, const frx_node *n, const frx_attr *a, const frx_attdef *ad)
484 {
485 const char *bad;
486 frx_str tok;
487 5239 size_t at = 0;
488  
489 5239 ▶ if (ad->def == FRX_DEF_FIXED && !frx_str_eq2(&a->value, &ad->value)
Branch: 2 branches
BranchTrueFalseTotal
if (ad->def == FRX_DEF_FIXED && !frx_str_eq2(&a->value, &ad->value) 807 4432 5239
if (ad->def == FRX_DEF_FIXED && !frx_str_eq2(&a->value, &ad->value) 3 804 807
490 3 ▶ && !frx_val_err(v, "an attribute declared #FIXED has another value (VC: Fixed Attribute Default)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an attribute declared #FIXED has another value (VC: Fixed Attribute Default)", 1 2 3
491 4 a->offset)) return 0;
492  
493 5238 bad = frx_val_syntax_check(ad->type, ad->enums, ad->n_enums, &a->value);
494 5238 ▶ if (!bad) bad = frx_val_semantic_check(v, ad->type, &a->value);
Branch: 1 branch
BranchTrueFalseTotal
if (!bad) bad = frx_val_semantic_check(v, ad->type, &a->value); 5196 42 5238
495 5238 ▶ if (bad) return frx_val_err(v, bad, a->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (bad) return frx_val_err(v, bad, a->offset); 50 5188 5238
496  
497 /* the two types that are about the document as a whole rather than
498 * about this value's shape */
499 5188 ▶ if (ad->type == FRX_ATT_ID) {
Branch: 1 branch
BranchTrueFalseTotal
if (ad->type == FRX_ATT_ID) { 984 4204 5188
500 984 ▶ if (frx_val_id_seen(v, &a->value))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_val_id_seen(v, &a->value)) 3 981 984
501 3 return frx_val_err(v, "two elements carry the same ID value (VC: ID)", a->offset);
502 981 return frx_val_push_id(v, &a->local, &a->value, n, a->offset);
503 }
504 4204 ▶ if (ad->type == FRX_ATT_IDREF) return frx_val_push_idref(v, &a->value, a->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (ad->type == FRX_ATT_IDREF) return frx_val_push_idref(v, &a->value, a->offset); 1758 2446 4204
505 2446 ▶ if (ad->type == FRX_ATT_IDREFS) {
Branch: 1 branch
BranchTrueFalseTotal
if (ad->type == FRX_ATT_IDREFS) { 14 2432 2446
506 48 ▶ while (frx_val_token(&a->value, &at, &tok))
Branch: 1 branch
BranchTrueFalseTotal
while (frx_val_token(&a->value, &at, &tok)) 34 14 48
507 34 ▶ if (!frx_val_push_idref(v, &tok, a->offset)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_push_idref(v, &tok, a->offset)) return 0; 0 34 34
508 }
509 2446 return 1;
510 }
511  
512 static int
513 8713 frx_val_attrs(frx_val *v, const frx_node *n, const frx_eldecl *ed)
514 {
515 const frx_attdef *ad;
516 int i;
517  
518 13947 ▶ for (i = 0; i < n->n_attrs && !v->stop; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n->n_attrs && !v->stop; i++) { 5249 8698 13947
for (i = 0; i < n->n_attrs && !v->stop; i++) { 5249 0 5249
519 5249 const frx_attr *a = &n->attrs[i];
520 5249 ▶ const frx_attdef *ad2 = ed ? frx_dtd_attdef(ed, &a->qname) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const frx_attdef *ad2 = ed ? frx_dtd_attdef(ed, &a->qname) : NULL; 5249 0 5249
521 5249 ▶ if (!ad2) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ad2) { 10 5239 5249
522 10 ▶ if (!frx_val_err(v, "the attribute is not declared for this element type (VC: Attribute Value Type)",
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_err(v, "the attribute is not declared for this element type (VC: Attribute Value Type)", 1 9 10
523 11 a->offset)) return 0;
524 9 continue;
525 }
526 5239 ▶ if (!frx_val_attr(v, n, a, ad2)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_attr(v, n, a, ad2)) return 0; 14 5225 5239
527 }
528 8698 ▶ if (!ed) return !v->stop;
Branch: 1 branch
BranchTrueFalseTotal
if (!ed) return !v->stop; 0 8698 8698
529  
530 /* A namespace declaration is a declaration and not an attribute in
531 * this tree, but a DTD does not know that: Namespaces is a layer above
532 * XML 1.0, and to the grammar `xmlns:foo="..."` is an Attribute like
533 * any other. So a validating parse holds it to VC: Attribute Value
534 * Type exactly as it holds a written attribute, and where the DTD does
535 * declare it, what the DTD says about its value holds too, which is
536 * what a #FIXED xmlns is for. */
537 8874 ▶ for (i = 0; i < n->n_decls && !v->stop; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n->n_decls && !v->stop; i++) { 179 8695 8874
for (i = 0; i < n->n_decls && !v->stop; i++) { 179 0 179
538 179 const frx_attdef *ad2 = frx_val_nsdecl_attdef(ed, &n->decls[i].prefix);
539 const char *bad;
540 179 ▶ if (!ad2) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ad2) { 153 26 179
541 153 ▶ if (!frx_val_err(v, "the attribute is not declared for this element type (VC: Attribute Value Type)",
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_err(v, "the attribute is not declared for this element type (VC: Attribute Value Type)", 2 151 153
542 155 n->offset)) return 0;
543 151 continue;
544 }
545 26 ▶ if (ad2->def == FRX_DEF_FIXED && !frx_str_eq2(&n->decls[i].uri, &ad2->value)
Branch: 2 branches
BranchTrueFalseTotal
if (ad2->def == FRX_DEF_FIXED && !frx_str_eq2(&n->decls[i].uri, &ad2->value) 3 23 26
if (ad2->def == FRX_DEF_FIXED && !frx_str_eq2(&n->decls[i].uri, &ad2->value) 2 1 3
546 2 ▶ && !frx_val_err(v, "an attribute declared #FIXED has another value (VC: Fixed Attribute Default)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an attribute declared #FIXED has another value (VC: Fixed Attribute Default)", 1 1 2
547 3 n->offset)) return 0;
548 25 bad = frx_val_syntax_check(ad2->type, ad2->enums, ad2->n_enums, &n->decls[i].uri);
549 25 ▶ if (bad && !frx_val_err(v, bad, n->offset)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (bad && !frx_val_err(v, bad, n->offset)) return 0; 0 25 25
if (bad && !frx_val_err(v, bad, n->offset)) return 0; 0 0 0
550 }
551  
552 /* An xmlns:xml declaration is accepted and dropped from decls, so the
553 * loop above cannot see it; the element carries a bit saying it was
554 * written (frx_tree.h). VC: Attribute Value Type does not care that
555 * the binding it makes is the one the xml prefix already has. */
556 8695 ▶ if (!v->stop && (n->from_ref & FRX_NODE_XMLNS_XML)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!v->stop && (n->from_ref & FRX_NODE_XMLNS_XML)) { 8695 0 8695
if (!v->stop && (n->from_ref & FRX_NODE_XMLNS_XML)) { 3 8692 8695
557 static const frx_str xml_prefix = { "xml", 3 };
558 3 ▶ if (!frx_val_nsdecl_attdef(ed, &xml_prefix)
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_nsdecl_attdef(ed, &xml_prefix) 2 1 3
559 2 ▶ && !frx_val_err(v, "the attribute is not declared for this element type (VC: Attribute Value Type)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "the attribute is not declared for this element type (VC: Attribute Value Type)", 1 1 2
560 3 n->offset)) return 0;
561 }
562  
563 27267 ▶ for (ad = ed->attdefs; ad && !v->stop; ad = ad->next) {
Branch: 2 branches
BranchTrueFalseTotal
for (ad = ed->attdefs; ad && !v->stop; ad = ad->next) { 18574 8693 27267
for (ad = ed->attdefs; ad && !v->stop; ad = ad->next) { 18574 0 18574
564 18574 int have = 0;
565 18574 ▶ if (ad->def != FRX_DEF_REQUIRED) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ad->def != FRX_DEF_REQUIRED) continue; 16053 2521 18574
566 2521 ▶ if (frx_val_is_xmlns(&ad->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_val_is_xmlns(&ad->name)) { 0 2521 2521
567 0 ▶ if (!frx_val_has_nsdecl(n, &ad->name)
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_has_nsdecl(n, &ad->name) 0 0 0
568 0 ▶ && !frx_val_err(v, "an attribute declared #REQUIRED is absent (VC: Required Attribute)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an attribute declared #REQUIRED is absent (VC: Required Attribute)", 0 0 0
569 0 n->offset)) return 0;
570 0 continue;
571 }
572 2719 ▶ for (i = 0; i < n->n_attrs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->n_attrs; i++) 2714 5 2719
573 2714 ▶ if (frx_str_eq2(&n->attrs[i].qname, &ad->name)) { have = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&n->attrs[i].qname, &ad->name)) { have = 1; break; } 2516 198 2714
574 2521 ▶ if (!have
Branch: 1 branch
BranchTrueFalseTotal
if (!have 5 2516 2521
575 5 ▶ && !frx_val_err(v, "an attribute declared #REQUIRED is absent (VC: Required Attribute)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an attribute declared #REQUIRED is absent (VC: Required Attribute)", 1 4 5
576 6 n->offset)) return 0;
577 }
578 8693 return !v->stop;
579 }
580  
581 /* ---- content ------------------------------------------------------------ */
582  
583 /* VC: Element Valid, over one element's own children */
584 static int
585 8693 frx_val_content(frx_val *v, const frx_node *n, const frx_eldecl *ed)
586 {
587 8693 const frx_cm_dfa *d = (const frx_cm_dfa *)frx_dtd_map_find(&v->models, &ed->name);
588 const frx_node *c;
589 8693 int at = -1;
590  
591 /* a reference that left no characters is still content, and an
592 * element declared EMPTY may hold none (erratum E15, case E15a);
593 * frx_sink.h records it here because there is no node to hang it on */
594 8693 ▶ if (ed->content == FRX_CONTENT_EMPTY && (n->from_ref & FRX_NODE_FROM_REF)
Branch: 2 branches
BranchTrueFalseTotal
if (ed->content == FRX_CONTENT_EMPTY && (n->from_ref & FRX_NODE_FROM_REF) 604 8089 8693
if (ed->content == FRX_CONTENT_EMPTY && (n->from_ref & FRX_NODE_FROM_REF) 1 603 604
595 1 ▶ && !frx_val_err(v, "an element declared EMPTY has content (VC: Element Valid)", n->offset))
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an element declared EMPTY has content (VC: Element Valid)", n->offset)) 0 1 1
596 0 return 0;
597  
598 29765 ▶ for (c = n->first_child; c && !v->stop; c = c->next) {
Branch: 2 branches
BranchTrueFalseTotal
for (c = n->first_child; c && !v->stop; c = c->next) { 21098 8667 29765
for (c = n->first_child; c && !v->stop; c = c->next) { 21098 0 21098
599 21098 ▶ if (c->kind == FRX_COMMENT || c->kind == FRX_PI) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->kind == FRX_COMMENT || c->kind == FRX_PI) { 20739 359 21098
if (c->kind == FRX_COMMENT || c->kind == FRX_PI) { 13 20726 20739
600 /* a content model ignores these, but EMPTY means no content
601 * at all, comments and processing instructions included
602 * (second edition erratum E15) */
603 372 ▶ if (ed->content == FRX_CONTENT_EMPTY
Branch: 1 branch
BranchTrueFalseTotal
if (ed->content == FRX_CONTENT_EMPTY 4 368 372
604 4 ▶ && !frx_val_err(v, "an element declared EMPTY has content (VC: Element Valid)", c->offset))
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an element declared EMPTY has content (VC: Element Valid)", c->offset)) 2 2 4
605 2 return 0;
606 370 continue;
607 }
608  
609 20726 ▶ if (c->kind == FRX_TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->kind == FRX_TEXT) { 13008 7718 20726
610 /* A CDATA section is character data whatever it holds, and
611 * an empty one is still a section: it is not the ignorable
612 * white space of section 2.10, which is white space written
613 * directly in element content. The spans are recorded so the
614 * writer can put the sections back; this is the other thing
615 * knowing where they were is good for.
616 *
617 * Second edition erratum E15 says the same of a reference:
618 * "the white space in element content must be white space
619 * characters that appear directly in the document, not
620 * characters that result from the expansion of a character or
621 * entity reference". The parse marks such a run, for the same
622 * reason and by the same means. */
623 13008 ▶ int ws = frx_str_is_s(&c->value) && !c->n_cdata_spans && !(c->from_ref & FRX_NODE_FROM_REF);
Branch: 3 branches
BranchTrueFalseTotal
int ws = frx_str_is_s(&c->value) && !c->n_cdata_spans && !(c->from_ref & FRX_NODE_FROM_REF); 5190 7818 13008
int ws = frx_str_is_s(&c->value) && !c->n_cdata_spans && !(c->from_ref & FRX_NODE_FROM_REF); 5183 7 5190
int ws = frx_str_is_s(&c->value) && !c->n_cdata_spans && !(c->from_ref & FRX_NODE_FROM_REF); 5177 6 5183
624 13008 ▶ if (ed->content == FRX_CONTENT_MIXED || ed->content == FRX_CONTENT_ANY) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (ed->content == FRX_CONTENT_MIXED || ed->content == FRX_CONTENT_ANY) continue; 4872 8136 13008
if (ed->content == FRX_CONTENT_MIXED || ed->content == FRX_CONTENT_ANY) continue; 594 4278 4872
625 4278 ▶ if (ed->content == FRX_CONTENT_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ed->content == FRX_CONTENT_EMPTY) { 6 4272 4278
626 6 ▶ if (!frx_val_err(v, "an element declared EMPTY has content (VC: Element Valid)", c->offset))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_err(v, "an element declared EMPTY has content (VC: Element Valid)", c->offset)) 1 5 6
627 1 return 0;
628 5 continue;
629 }
630 4272 ▶ if (!ws) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ws) { 17 4255 4272
631 17 ▶ if (!frx_val_err(v, "character data where the content model allows only elements (VC: Element Valid)",
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_err(v, "character data where the content model allows only elements (VC: Element Valid)", 3 14 17
632 20 c->offset)) return 0;
633 14 continue;
634 }
635 /* whitespace directly in element content is ignored by the
636 * model, and is the fourth clause of the standalone VC when
637 * the declaration came from external markup */
638 4255 ▶ if (v->standalone && ed->external
Branch: 2 branches
BranchTrueFalseTotal
if (v->standalone && ed->external 12 4243 4255
if (v->standalone && ed->external 5 7 12
639 5 ▶ && !frx_val_err(v, "standalone=\"yes\" but white space stands directly in an element whose content model was declared in external markup (VC: Standalone Document Declaration)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "standalone=\"yes\" but white space stands directly in an element whose content model was declared in external markup (VC: Standalone Document Declaration)", 0 5 5
640 5 c->offset)) return 0;
641 4255 continue;
642 }
643  
644 7718 ▶ if (c->kind != FRX_ELEMENT) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (c->kind != FRX_ELEMENT) continue; 0 7718 7718
645  
646 7718 switch (ed->content) {
647 2 case FRX_CONTENT_EMPTY:
648 2 ▶ if (!frx_val_err(v, "an element declared EMPTY has content (VC: Element Valid)", c->offset))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_err(v, "an element declared EMPTY has content (VC: Element Valid)", c->offset)) 1 1 2
649 1 return 0;
650 1 break;
651 432 case FRX_CONTENT_ANY:
652 432 break; /* any declared element, checked when visited */
653 3154 case FRX_CONTENT_MIXED:
654 3154 ▶ if (d && !frx_model_mixed_has(d, &c->qname)
Branch: 2 branches
BranchTrueFalseTotal
if (d && !frx_model_mixed_has(d, &c->qname) 3154 0 3154
if (d && !frx_model_mixed_has(d, &c->qname) 3 3151 3154
655 3 ▶ && !frx_val_err(v, "an element the mixed content model does not name (VC: Element Valid)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(v, "an element the mixed content model does not name (VC: Element Valid)", 1 2 3
656 4 c->offset)) return 0;
657 3153 break;
658 4130 default:
659 4130 ▶ if (!d) break; /* the model was refused; do not report twice */
Branch: 1 branch
BranchTrueFalseTotal
if (!d) break; /* the model was refused; do not report twice */ 0 4130 4130
660 4130 at = frx_model_step(d, at, &c->qname);
661 4130 ▶ if (at < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (at < 0) { 18 4112 4130
662 18 ▶ if (!frx_val_err(v, "an element the content model does not allow here (VC: Element Valid)",
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_err(v, "an element the content model does not allow here (VC: Element Valid)", 8 10 18
663 26 c->offset)) return 0;
664 10 return !v->stop; /* the run is over; one report per element */
665 }
666 4112 break;
667 }
668 }
669  
670 8667 ▶ if (!v->stop && ed->content == FRX_CONTENT_CHILDREN && d && !frx_model_accepts(d, at))
Branch: 4 branches
BranchTrueFalseTotal
if (!v->stop && ed->content == FRX_CONTENT_CHILDREN && d && !frx_model_accepts(d, at)) 8667 0 8667
if (!v->stop && ed->content == FRX_CONTENT_CHILDREN && d && !frx_model_accepts(d, at)) 1529 7138 8667
if (!v->stop && ed->content == FRX_CONTENT_CHILDREN && d && !frx_model_accepts(d, at)) 1529 0 1529
if (!v->stop && ed->content == FRX_CONTENT_CHILDREN && d && !frx_model_accepts(d, at)) 19 1510 1529
671 19 return frx_val_err(v, "the element's content ends before the content model does (VC: Element Valid)",
672 19 n->offset);
673 8648 return !v->stop;
674 }
675  
676 /* ---- the pass ----------------------------------------------------------- */
677  
678 static void
679 1386 frx_val_free(frx_val *v)
680 {
681 1386 free(v->ids);
682 1386 free(v->idrefs);
683 1386 v->ids = NULL;
684 1386 v->idrefs = NULL;
685 1386 }
686  
687 /* The index this pass built, sorted into the arena for by_id and XPath.
688 * The names go with it: by_id is asked for one, and XPath's id() has no
689 * name to give, so it searches every name the index was built from. The
690 * sort is by (attr, value), so the distinct names are contiguous. */
691 static int
692 1206 frx_val_install_ids(frx_val *v)
693 {
694 frx_idx *in_arena;
695 frx_str *names;
696 1206 int i, n_names = 0;
697  
698 1206 ▶ if (!v->own_ids || !v->n_ids) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!v->own_ids || !v->n_ids) return 1; 1056 150 1206
if (!v->own_ids || !v->n_ids) return 1; 1015 41 1056
699 41 qsort(v->ids, (size_t)v->n_ids, sizeof *v->ids, frx_idx_cmp);
700 41 in_arena = (frx_idx *)frx_arena_alloc(v->arena, (size_t)v->n_ids * sizeof *in_arena);
701 41 names = (frx_str *)frx_arena_alloc(v->arena, (size_t)v->n_ids * sizeof *names);
702 41 ▶ if (!in_arena || !names) { v->nomem = 1; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (!in_arena || !names) { v->nomem = 1; return 0; } 41 0 41
if (!in_arena || !names) { v->nomem = 1; return 0; } 0 41 41
703 41 memcpy(in_arena, v->ids, (size_t)v->n_ids * sizeof *in_arena);
704 877 ▶ for (i = 0; i < v->n_ids; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < v->n_ids; i++) 836 41 877
705 836 ▶ if (!n_names || !frx_str_eq2(&names[n_names - 1], &in_arena[i].attr))
Branch: 2 branches
BranchTrueFalseTotal
if (!n_names || !frx_str_eq2(&names[n_names - 1], &in_arena[i].attr)) 795 41 836
if (!n_names || !frx_str_eq2(&names[n_names - 1], &in_arena[i].attr)) 7 788 795
706 48 names[n_names++] = in_arena[i].attr;
707  
708 41 v->doc->ids = in_arena;
709 41 v->doc->n_ids = v->n_ids;
710 41 v->doc->id_attrs = names;
711 41 v->doc->n_id_attrs = n_names;
712 41 v->doc->id_source = FRX_ID_DTD;
713 41 return 1;
714 }
715  
716 /* Validate the finished document against its DTD. 1 when it is valid or
717 * when every violation was collected; 0 with *err filled when the caller
718 * asked to stop at the first one, or on an allocation failure. */
719 static int
720 1386 frx_validate_doc(frx_doc *doc, int collect, frx_err *err)
721 {
722 frx_val v;
723 const frx_node *n;
724 int i;
725  
726 1386 memset(&v, 0, sizeof v);
727 1386 v.doc = doc;
728 1386 v.dtd = doc->dtd;
729 1386 v.arena = &doc->arena;
730 1386 v.collect = collect;
731 1386 v.standalone = doc->standalone;
732 1386 v.own_ids = doc->id_source == FRX_ID_NONE;
733  
734 1386 ▶ if (!doc->doctype) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->doctype) { 180 1206 1386
735 /* a document with no document type declaration is not valid, and
736 * that is the whole of what can be said about it */
737 180 frx_val_err(&v, "the document has no document type declaration to be valid against (VC: Element Valid)", 0);
738 180 goto finish;
739 }
740 1206 ▶ if (!v.dtd) goto finish;
Branch: 1 branch
BranchTrueFalseTotal
if (!v.dtd) goto finish; 0 1206 1206
741  
742 1206 ▶ if (!frx_str_eq2(&doc->doctype->name, &doc->root->qname))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_str_eq2(&doc->doctype->name, &doc->root->qname)) 5 1201 1206
743 5 (void)frx_val_err(&v, "the root element is not the one the DOCTYPE names (VC: Root Element Type)",
744 5 doc->root->offset);
745  
746 1206 ▶ if (v.dtd->pe_nest_bad)
Branch: 1 branch
BranchTrueFalseTotal
if (v.dtd->pe_nest_bad) 13 1193 1206
747 13 (void)frx_val_err(&v, "a markup declaration, a content model group or a conditional section began inside one parameter entity's replacement text and ended outside it (VC: Proper Declaration/PE Nesting)",
748 13 v.dtd->pe_nest_offset);
749  
750 1206 ▶ if (!v.stop) (void)frx_val_declarations(&v);
Branch: 1 branch
BranchTrueFalseTotal
if (!v.stop) (void)frx_val_declarations(&v); 1199 7 1206
751 1206 ▶ if (!v.stop) (void)frx_val_notations(&v);
Branch: 1 branch
BranchTrueFalseTotal
if (!v.stop) (void)frx_val_notations(&v); 1185 21 1206
752  
753 23872 ▶ for (n = doc->document->first_child; n && !v.stop; n = frx_walk_next(n, doc->document)) {
Branch: 2 branches
BranchTrueFalseTotal
for (n = doc->document->first_child; n && !v.stop; n = frx_walk_next(n, doc->document)) { 22733 1139 23872
for (n = doc->document->first_child; n && !v.stop; n = frx_walk_next(n, doc->document)) { 22711 22 22733
754 const frx_eldecl *ed;
755 22711 ▶ if (n->kind != FRX_ELEMENT) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (n->kind != FRX_ELEMENT) continue; 13819 8892 22711
756 8892 ed = frx_dtd_element(v.dtd, &n->qname);
757 8892 ▶ if (!ed || !ed->declared) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ed || !ed->declared) { 8714 178 8892
if (!ed || !ed->declared) { 1 8713 8714
758 179 ▶ if (!frx_val_err(&v, "the element type is not declared (VC: Element Valid)", n->offset)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_err(&v, "the element type is not declared (VC: Element Valid)", n->offset)) break; 3 176 179
759 176 continue;
760 }
761 8713 ▶ if (!frx_val_attrs(&v, n, ed)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_attrs(&v, n, ed)) break; 20 8693 8713
762 8693 ▶ if (!frx_val_content(&v, n, ed)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_content(&v, n, ed)) break; 22 8671 8693
763 }
764  
765 /* VC: IDREF, once every ID in the document has been seen */
766 2997 ▶ for (i = 0; i < v.n_idrefs && !v.stop; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < v.n_idrefs && !v.stop; i++) 1792 1205 2997
for (i = 0; i < v.n_idrefs && !v.stop; i++) 1792 0 1792
767 1792 ▶ if (!frx_val_id_seen(&v, &v.idrefs[i].value)
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_id_seen(&v, &v.idrefs[i].value) 6 1786 1792
768 6 ▶ && !frx_val_err(&v, "an IDREF or IDREFS value matches no ID in the document (VC: IDREF)",
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_val_err(&v, "an IDREF or IDREFS value matches no ID in the document (VC: IDREF)", 1 5 6
769 7 v.idrefs[i].offset)) break;
770  
771 1206 ▶ if (!v.nomem) (void)frx_val_install_ids(&v);
Branch: 1 branch
BranchTrueFalseTotal
if (!v.nomem) (void)frx_val_install_ids(&v); 1206 0 1206
772  
773 /* VC: Entity Declared, for each reference the parse exempted from the
774 * well-formedness constraint (frx_entity.h). They are collected in
775 * document order and the walk above produced its own list in document
776 * order, so the two are merged rather than appended: `collect` says
777 * the list is in document order and means it. */
778 1214 ▶ for (i = 0; i < doc->n_undeclared_refs && !v.stop; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < doc->n_undeclared_refs && !v.stop; i++) 1204 10 1214
for (i = 0; i < doc->n_undeclared_refs && !v.stop; i++) 10 0 10
779 10 ▶ if (!frx_val_err_ordered(&v, "an entity reference names no declared entity (VC: Entity Declared)",
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_val_err_ordered(&v, "an entity reference names no declared entity (VC: Entity Declared)", 2 8 10
780 12 doc->undeclared_refs[i])) break;
781  
782 1204 finish:
783 1386 frx_val_free(&v);
784 1386 ▶ if (v.nomem) {
Branch: 1 branch
BranchTrueFalseTotal
if (v.nomem) { 0 1386 1386
785 0 frx_err_set(err, FRX_E_NOMEM, 0, "out of memory");
786 0 return 0;
787 }
788 1386 doc->errors = v.first;
789 1386 doc->n_errors = v.n;
790 1386 ▶ if (!collect && v.first) {
Branch: 2 branches
BranchTrueFalseTotal
if (!collect && v.first) { 106 1280 1386
if (!collect && v.first) { 71 35 106
791 71 frx_err_set(err, FRX_E_VALIDITY, v.first->offset, v.first->what);
792 71 return 0;
793 }
794 1315 return 1;
795 }
796  
797 #endif /* FRX_VALIDATE_H */
« include/frx/frx_lex.h ↑ Summary include/frx/frx_xpath.h »