include/frx/frx_dtd.h

filter: Statementstmt 98.4% 588 / 597 Branchbran 82.9% 635 / 766 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.7% 1223 / 1363 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_xpath.h ↑ Summary include/frx/frx_write.h »
1 #ifndef FRX_DTD_H
2 #define FRX_DTD_H
3  
4 /* The document type declaration, the internal subset and, through a
5 * resolver, the external subset: the structures a DTD becomes in the
6 * arena, and the declaration parser that fills them. Full profile only.
7 *
8 * What is kept: element declarations with their content model as a token
9 * list (frx_validate.h validates against it; here its syntax is checked and
10 * nothing else), attribute lists with each attribute's type and default,
11 * general and parameter entities, and notations, each marked with where
12 * it was declared, because the standalone constraints of section 2.9
13 * turn on that origin. What is used here: attribute types select the
14 * 3.3.3 normalisation, defaults are materialised onto every element that
15 * lacks them (in declaration order, after the written attributes, and a
16 * defaulted xmlns attribute is a namespace declaration like any other,
17 * which is why defaulting happens in the lexer before the parser reads
18 * the tag), and entities are expanded through frx_entity.h.
19 *
20 * The internal subset is read first and the external subset after it,
21 * so a declaration in the internal subset is the one that binds (section
22 * 2.9: internal declarations take precedence). Section 2.8's rules are
23 * enforced per frame: in the internal subset a parameter entity
24 * reference may stand only between declarations (WFC: PEs in Internal
25 * Subset) and a conditional section is refused; in the external subset
26 * and in an external parameter entity a PE reference may stand wherever
27 * whitespace may (4.4.8: its text is included with a space on each side),
28 * inside an entity value (4.4.5: included in the literal), and
29 * conditional sections nest. A PE whose text ends inside a declaration is
30 * popped and reading continues, which is the non-validating reading of
31 * VC: Proper Declaration/PE Nesting; validation may enforce the VC. The
32 * first declaration of an entity or of an attribute binds and a later
33 * one is read for its syntax and dropped (sections 4.2 and 3.3), as is a
34 * second ELEMENT declaration for one name.
35 *
36 * Nothing external is fetched here; frx_entity.h fetches through the
37 * caller's resolver, and with none an external subset is refused at its
38 * system literal and an external entity at its reference, each with a
39 * message naming the option, because a document whose declarations were
40 * only partly read would canonicalise differently from a processor that
41 * read them all, and this dist exists to canonicalise for signatures.
42 *
43 * Nothing recurses (rule 2): the content model parser keeps its group
44 * stack in a malloc'd array on the lexer, conditional sections are a
45 * counter, and the subset loop is one loop over frames. Every name lookup
46 * is a small hash over the arena.
47 *
48 * Needs frx_tree.h (frx_doctype, the frx_str helpers) and frx_lex.h (its
49 * primitives and the forward declarations there); frx_entity.h follows
50 * and is declared below. */
51  
52 /* attribute types, XML 1.0 section 3.3.1 */
53 enum {
54 FRX_ATT_CDATA = 0, FRX_ATT_ID, FRX_ATT_IDREF, FRX_ATT_IDREFS,
55 FRX_ATT_ENTITY, FRX_ATT_ENTITIES, FRX_ATT_NMTOKEN, FRX_ATT_NMTOKENS,
56 FRX_ATT_NOTATION, FRX_ATT_ENUM
57 };
58  
59 /* default declarations, section 3.3.2 */
60 enum { FRX_DEF_VALUE = 0, FRX_DEF_REQUIRED, FRX_DEF_IMPLIED, FRX_DEF_FIXED };
61  
62 /* content specifications, section 3.2 */
63 enum { FRX_CONTENT_EMPTY = 0, FRX_CONTENT_ANY, FRX_CONTENT_MIXED, FRX_CONTENT_CHILDREN };
64  
65 /* content model tokens: the kind is the character for ( ) | , ? * + and
66 * one of these two for a name and #PCDATA */
67 enum { FRX_CM_NAME = 'N', FRX_CM_PCDATA = 'P' };
68  
69 typedef struct frx_cm_tok {
70 int kind;
71 frx_str name; /* FRX_CM_NAME only */
72 } frx_cm_tok;
73  
74 struct frx_attdef {
75 frx_str name; /* as written; a QName, xmlns:* included */
76 int type; /* FRX_ATT_* */
77 const frx_str *enums; /* NOTATION and ENUM: the names or tokens */
78 int n_enums;
79 int def; /* FRX_DEF_* */
80 int has_value; /* FRX_DEF_VALUE or FRX_DEF_FIXED */
81 frx_str value; /* the default, normalised by type */
82 int external; /* declared in the external subset or an external PE */
83 size_t offset; /* document offset of the definition */
84 struct frx_attdef *next; /* declaration order */
85 };
86  
87 struct frx_eldecl {
88 frx_str name;
89 int declared; /* how many ELEMENT declarations were read, so 0 is none (an
90 * ATTLIST may come first) and more than one is a validity
91 * error validation reports; only the first one binds */
92 int content; /* FRX_CONTENT_* */
93 const frx_cm_tok *model; /* MIXED and CHILDREN: the tokens, parentheses included */
94 int n_model;
95 int external; /* the ELEMENT declaration was external markup */
96 frx_attdef *attdefs; /* declaration order */
97 frx_attdef *last_attdef;
98 int n_attdefs;
99 size_t offset;
100 struct frx_eldecl *next; /* declaration order, for validation */
101 };
102  
103 struct frx_entity {
104 frx_str name;
105 int is_pe;
106 int external; /* SYSTEM or PUBLIC */
107 int unparsed; /* NDATA */
108 int external_decl; /* declared in the external subset or an external PE */
109 int direct; /* declared directly in the internal subset: not in the
110 * external subset nor inside any parameter entity, which
111 * is what WFC: Entity Declared asks of a standalone document */
112 frx_str public_id; /* "" when none */
113 frx_str system_id;
114 frx_str ndata;
115 frx_str base; /* base URI of the entity the declaration stood in */
116 frx_str text; /* the replacement text of section 4.5; fetched once for an external one */
117 frx_str padded; /* text with a space on each side (4.4.8), built on first use */
118 frx_str uri; /* external: the resolved identifier, once fetched */
119 int fetched; /* external: text is filled */
120 int version11; /* external: its text declaration said 1.1 */
121 int flat; /* declared in fetched text: offsets inside it are the reference's */
122 size_t offset; /* document offset of the declaration */
123 size_t text_offset; /* document offset of the literal's first byte */
124 int expanding; /* on the frame stack now; WFC: No Recursion */
125 };
126  
127 typedef struct frx_notation {
128 frx_str name;
129 frx_str public_id;
130 frx_str system_id;
131 size_t offset;
132 } frx_notation;
133  
134 typedef struct frx_dtd_slot {
135 frx_str name;
136 void *value;
137 struct frx_dtd_slot *next;
138 } frx_dtd_slot;
139  
140 #define FRX_DTD_BUCKETS 64
141  
142 typedef struct frx_dtd_map {
143 frx_dtd_slot *b[FRX_DTD_BUCKETS];
144 } frx_dtd_map;
145  
146 struct frx_dtd {
147 frx_dtd_map elements;
148 frx_dtd_map general;
149 frx_dtd_map params;
150 frx_dtd_map notations;
151 frx_eldecl *first_element;
152 frx_eldecl *last_element;
153 int external_subset; /* the DOCTYPE carried an external ID */
154 int external_read; /* and it was fetched and read */
155 int pe_ref_seen; /* a parameter entity reference stood in the subset */
156 /* A markup declaration, a content model group or a conditional
157 * section that began inside one parameter entity's replacement text
158 * and ended outside it, or the other way about. Reading continues
159 * either way, because the text of a parameter entity is included in
160 * the stream and the construct does finish; it is a validity
161 * constraint and frx_validate.h reports it. */
162 int pe_nest_bad;
163 size_t pe_nest_offset;
164 };
165  
166 /* the first straddling construct, remembered for validation */
167 static void
168 frx_dtd_pe_nest(frx_lex *l, size_t at);
169  
170 /* which replacement text is being read: 0 for the document or the
171 * external subset itself, and each parameter entity's own serial
172 * otherwise. Frame depth alone will not do, because `%a;%b;` puts two
173 * different texts at one depth. */
174 static int
175 50950 frx_dtd_cur_seq(const frx_lex *l)
176 {
177 50950 ▶ return l->n_frames ? l->frames[l->n_frames - 1].seq : 0;
Branch: 1 branch
BranchTrueFalseTotal
return l->n_frames ? l->frames[l->n_frames - 1].seq : 0; 14610 36340 50950
178 }
179  
180 /* frx_entity.h */
181 static int frx_entity_pe_reference(frx_lex *l, size_t start, int padded);
182 static int frx_entity_push_text(frx_lex *l, frx_entity *e, const frx_str *text, size_t ref_rel,
183 int flat, size_t base_off, const frx_str *cur_base, int ext_decl);
184 static int frx_entity_fetch(frx_lex *l, int kind, const frx_str *pub, const frx_str *sys,
185 const frx_str *base, size_t ref_rel,
186 frx_str *text, frx_str *uri, int *version11);
187  
188 /* the lexer names the entity a token came from through this, since the
189 * struct is complete only here */
190 static const frx_str *
191 4130537 frx_entity_name_of(const frx_entity *e)
192 {
193 4130537 return &e->name;
194 }
195  
196 /* ---- the maps ---------------------------------------------------------- */
197  
198 static unsigned
199 2246564 frx_dtd_hash(const char *p, size_t n)
200 {
201 2246564 unsigned h = 2166136261u; /* FNV-1a */
202 size_t i;
203 9295797 ▶ for (i = 0; i < n; i++) { h ^= (unsigned char)p[i]; h *= 16777619u; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { h ^= (unsigned char)p[i]; h *= 16777619u; } 7049233 2246564 9295797
204 2246564 return h & (FRX_DTD_BUCKETS - 1);
205 }
206  
207 static void *
208 2229642 frx_dtd_map_find(const frx_dtd_map *m, const frx_str *name)
209 {
210 const frx_dtd_slot *s;
211 2250642 ▶ for (s = m->b[frx_dtd_hash(name->p, name->len)]; s; s = s->next)
Branch: 1 branch
BranchTrueFalseTotal
for (s = m->b[frx_dtd_hash(name->p, name->len)]; s; s = s->next) 2232786 17856 2250642
212 2232786 ▶ if (frx_str_eq2(&s->name, name)) return s->value;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&s->name, name)) return s->value; 2211786 21000 2232786
213 17856 return NULL;
214 }
215  
216 static int
217 16922 frx_dtd_map_add(frx_arena *a, frx_dtd_map *m, const frx_str *name, void *value)
218 {
219 16922 unsigned h = frx_dtd_hash(name->p, name->len);
220 16922 frx_dtd_slot *s = (frx_dtd_slot *)frx_arena_alloc(a, sizeof *s);
221 16922 ▶ if (!s) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return 0; 0 16922 16922
222 16922 s->name = *name;
223 16922 s->value = value;
224 16922 s->next = m->b[h];
225 16922 m->b[h] = s;
226 16922 return 1;
227 }
228  
229 static void
230 13 frx_dtd_pe_nest(frx_lex *l, size_t at)
231 {
232 13 ▶ if (l->dtd && !l->dtd->pe_nest_bad) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->dtd && !l->dtd->pe_nest_bad) { 13 0 13
if (l->dtd && !l->dtd->pe_nest_bad) { 13 0 13
233 13 l->dtd->pe_nest_bad = 1;
234 13 ▶ l->dtd->pe_nest_offset = FRX_LEX_ABS(l, at);
Branch: 1 branch
BranchTrueFalseTotal
l->dtd->pe_nest_offset = FRX_LEX_ABS(l, at); 13 0 13
235 }
236 13 }
237  
238 static frx_dtd *
239 5665 frx_dtd_new(frx_arena *a)
240 {
241 5665 frx_dtd *d = (frx_dtd *)frx_arena_alloc(a, sizeof *d);
242 5665 ▶ if (d) memset(d, 0, sizeof *d);
Branch: 1 branch
BranchTrueFalseTotal
if (d) memset(d, 0, sizeof *d); 5665 0 5665
243 5665 return d;
244 }
245  
246 static const frx_eldecl *
247 22264 frx_dtd_element(const frx_dtd *dtd, const frx_str *name)
248 {
249 22264 return (const frx_eldecl *)frx_dtd_map_find(&dtd->elements, name);
250 }
251  
252 static const frx_attdef *
253 18252 frx_dtd_attdef(const frx_eldecl *ed, const frx_str *name)
254 {
255 const frx_attdef *ad;
256 48888 ▶ for (ad = ed->attdefs; ad; ad = ad->next)
Branch: 1 branch
BranchTrueFalseTotal
for (ad = ed->attdefs; ad; ad = ad->next) 41405 7483 48888
257 41405 ▶ if (frx_str_eq2(&ad->name, name)) return ad;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&ad->name, name)) return ad; 10769 30636 41405
258 7483 return NULL;
259 }
260  
261 /* section 3.3.3: every type but CDATA collapses */
262 static int
263 5425 frx_dtd_attdef_collapses(const frx_attdef *ad)
264 {
265 5425 return ad->type != FRX_ATT_CDATA;
266 }
267  
268 static int
269 20 frx_dtd_attdef_external(const frx_attdef *ad)
270 {
271 20 return ad->external;
272 }
273  
274 static frx_entity *
275 2185445 frx_dtd_entity(const frx_dtd *dtd, const frx_str *name, int is_pe)
276 {
277 2185445 ▶ return (frx_entity *)frx_dtd_map_find(is_pe ? &dtd->params : &dtd->general, name);
Branch: 1 branch
BranchTrueFalseTotal
return (frx_entity *)frx_dtd_map_find(is_pe ? &dtd->params : &dtd->general, name); 3357 2182088 2185445
278 }
279  
280 /* the element's record, created on first mention by ELEMENT or ATTLIST */
281 static frx_eldecl *
282 12139 frx_dtd_eldecl_get(frx_lex *l, const frx_str *name, size_t offset)
283 {
284 12139 frx_eldecl *ed = (frx_eldecl *)frx_dtd_map_find(&l->dtd->elements, name);
285 12139 ▶ if (ed) return ed;
Branch: 1 branch
BranchTrueFalseTotal
if (ed) return ed; 3569 8570 12139
286 8570 ed = (frx_eldecl *)frx_arena_alloc(l->arena, sizeof *ed);
287 8570 ▶ if (!ed) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!ed) return NULL; 0 8570 8570
288 8570 memset(ed, 0, sizeof *ed);
289 8570 ed->name = *name;
290 8570 ed->offset = offset;
291 8570 ▶ if (!frx_dtd_map_add(l->arena, &l->dtd->elements, name, ed)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_map_add(l->arena, &l->dtd->elements, name, ed)) return NULL; 0 8570 8570
292 8570 ▶ if (l->dtd->last_element) l->dtd->last_element->next = ed;
Branch: 1 branch
BranchTrueFalseTotal
if (l->dtd->last_element) l->dtd->last_element->next = ed; 3277 5293 8570
293 5293 else l->dtd->first_element = ed;
294 8570 l->dtd->last_element = ed;
295 8570 return ed;
296 }
297  
298 /* the declared defaults the tag lacks, appended after what was written */
299 static int
300 12680 frx_dtd_apply_defaults(frx_lex *l, const frx_eldecl *ed)
301 {
302 const frx_attdef *ad;
303 12680 int n_written = l->n_attrs;
304 33358 ▶ for (ad = ed->attdefs; ad; ad = ad->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (ad = ed->attdefs; ad; ad = ad->next) { 20681 12677 33358
305 20681 int i, have = 0;
306 20681 ▶ if (!ad->has_value) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ad->has_value) continue; 18612 2069 20681
307 4660 ▶ for (i = 0; i < n_written; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_written; i++) 2998 1662 4660
308 2998 ▶ if (frx_str_eq2(&l->attrs[i].name, &ad->name)) { have = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&l->attrs[i].name, &ad->name)) { have = 1; break; } 407 2591 2998
309 2069 ▶ if (have) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (have) continue; 407 1662 2069
310 1662 ▶ if (ad->external && l->standalone && l->validate)
Branch: 3 branches
BranchTrueFalseTotal
if (ad->external && l->standalone && l->validate) 1059 603 1662
if (ad->external && l->standalone && l->validate) 6 1053 1059
if (ad->external && l->standalone && l->validate) 3 3 6
311 3 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->tok_start,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->tok_start, 0 3 3
312 "standalone=\"yes\" but an attribute default declared in the external subset applies (VC: Standalone Document Declaration)");
313 1659 ▶ if (!frx_lex_push_attr(l, &ad->name, &ad->value, ad->offset, 1)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_push_attr(l, &ad->name, &ad->value, ad->offset, 1)) return 0; 0 1659 1659
314 }
315 12677 return 1;
316 }
317  
318 /* ---- the declaration parser --------------------------------------------- */
319  
320 #define FRX_DTD_PE_MESSAGE \
321 "a parameter entity reference is not allowed inside a markup declaration in the internal subset (WFC: PEs in Internal Subset)"
322  
323 /* Whitespace inside a declaration, and what section 2.8 lets stand where
324 * it does: in the internal subset a parameter entity reference is
325 * refused; in external markup it is expanded with its padding (4.4.8) and
326 * reading continues into it, and a frame that ends here is popped. *had
327 * says whether anything was consumed, which is what "S required" means
328 * when a PE supplied it. */
329 static int
330 103648 frx_dtd_ws(frx_lex *l, size_t *had)
331 {
332 103648 *had = 0;
333 for (;;) {
334 105400 *had += frx_lex_skip_s(l);
335 105400 ▶ if (l->pos >= l->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) { 950 104450 105400
336 950 ▶ if (l->n_frames > l->decl_frame0) { if (!frx_entity_pop(l)) return 0; (*had)++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (l->n_frames > l->decl_frame0) { if (!frx_entity_pop(l)) return 0; (*had)++; continue; } 872 78 950
if (l->n_frames > l->decl_frame0) { if (!frx_entity_pop(l)) return 0; (*had)++; continue; } 0 872 872
337 78 return 1;
338 }
339 104450 ▶ if (l->in[l->pos] == '%') {
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos] == '%') { 888 103562 104450
340 888 size_t at = l->pos;
341 888 ▶ if (!l->ext_decl) return FRX_LEX_FAIL(l, FRX_E_DTD, at, FRX_DTD_PE_MESSAGE);
Branch: 2 branches
BranchTrueFalseTotal
if (!l->ext_decl) return FRX_LEX_FAIL(l, FRX_E_DTD, at, FRX_DTD_PE_MESSAGE); 8 880 888
if (!l->ext_decl) return FRX_LEX_FAIL(l, FRX_E_DTD, at, FRX_DTD_PE_MESSAGE); 0 8 8
342 880 l->pos++;
343 880 ▶ if (!frx_entity_pe_reference(l, at, 1)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_entity_pe_reference(l, at, 1)) return 0; 0 880 880
344 880 (*had)++;
345 880 continue;
346 }
347 103562 return 1;
348 }
349 }
350  
351 static int
352 47569 frx_dtd_need_s(frx_lex *l, const char *what)
353 {
354 size_t had;
355 47569 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 4 47565 47569
356 47565 ▶ if (!had) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, what);
Branch: 2 branches
BranchTrueFalseTotal
if (!had) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, what); 85 47480 47565
if (!had) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, what); 0 85 85
357 47480 return 1;
358 }
359  
360 static int
361 18761 frx_dtd_is_ascii_name_char(unsigned char c)
362 {
363 18761 ▶ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
Branch: 5 branches
BranchTrueFalseTotal
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 0 1 1
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 254 18506 18760
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 252 2 254
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 6364 12394 18758
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 6364 0 6364
364 37522 ▶ || c == '_' || c == '-' || c == '.' || c == ':' || c >= 0x80;
Branch: 6 branches
BranchTrueFalseTotal
|| c == '_' || c == '-' || c == '.' || c == ':' || c >= 0x80; 1 18760 18761
|| c == '_' || c == '-' || c == '.' || c == ':' || c >= 0x80; 18758 0 18758
|| c == '_' || c == '-' || c == '.' || c == ':' || c >= 0x80; 18758 0 18758
|| c == '_' || c == '-' || c == '.' || c == ':' || c >= 0x80; 18758 0 18758
|| c == '_' || c == '-' || c == '.' || c == ':' || c >= 0x80; 18758 0 18758
|| c == '_' || c == '-' || c == '.' || c == ':' || c >= 0x80; 0 18758 18758
365 }
366  
367 /* a keyword at pos, not run into a longer name; consumed when matched */
368 static int
369 51682 frx_dtd_word(frx_lex *l, const char *lit)
370 {
371 51682 size_t n = strlen(lit);
372 51682 ▶ if (!frx_lex_starts(l, l->pos, lit)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_starts(l, l->pos, lit)) return 0; 32912 18770 51682
373 18770 ▶ if (l->pos + n < l->len && frx_dtd_is_ascii_name_char(l->in[l->pos + n])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos + n < l->len && frx_dtd_is_ascii_name_char(l->in[l->pos + n])) return 0; 18761 9 18770
if (l->pos + n < l->len && frx_dtd_is_ascii_name_char(l->in[l->pos + n])) return 0; 3 18758 18761
374 18767 l->pos += n;
375 18767 return 1;
376 }
377  
378 /* a quoted literal: SystemLiteral (any characters) or PubidLiteral
379 * (section 2.3's PubidChar) */
380 static int
381 4145 frx_dtd_literal(frx_lex *l, int pubid, frx_str *out)
382 {
383 unsigned char q;
384 4145 size_t start = l->pos;
385 4145 ▶ if (l->pos >= l->len || ((q = l->in[l->pos]) != '"' && q != '\''))
Branch: 3 branches
BranchTrueFalseTotal
if (l->pos >= l->len || ((q = l->in[l->pos]) != '"' && q != '\'')) 4141 4 4145
if (l->pos >= l->len || ((q = l->in[l->pos]) != '"' && q != '\'')) 48 4093 4141
if (l->pos >= l->len || ((q = l->in[l->pos]) != '"' && q != '\'')) 8 40 48
386 12 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, pubid ? "expected a quoted public ID" : "expected a quoted system literal");
Branch: 2 branches
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, pubid ? "expected a quoted public ID" : "expected a quoted system literal"); 4 8 12
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, pubid ? "expected a quoted public ID" : "expected a quoted system literal"); 0 12 12
387 4133 l->pos++;
388 4133 l->tmp.len = 0;
389 39989 for (;;) {
390 unsigned char c;
391 44122 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 37 44085 44122
392 37 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, start, pubid ? "unterminated public ID" : "unterminated system literal");
Branch: 2 branches
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, start, pubid ? "unterminated public ID" : "unterminated system literal"); 12 25 37
return FRX_LEX_FAIL(l, FRX_E_DTD, start, pubid ? "unterminated public ID" : "unterminated system literal"); 0 37 37
393 44085 c = l->in[l->pos];
394 44085 ▶ if (c == q) { l->pos++; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == q) { l->pos++; break; } 4073 40012 44085
395 40012 ▶ if (pubid) {
Branch: 1 branch
BranchTrueFalseTotal
if (pubid) { 10191 29821 40012
396 11537 ▶ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
Branch: 7 branches
BranchTrueFalseTotal
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 6771 3420 10191
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 5 6766 6771
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 773 2652 3425
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 38 735 773
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 355 2335 2690
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 156 199 355
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') 1346 0 1346
397 2491 ▶ || c == 0x20 || c == 0xD || c == 0xA
Branch: 3 branches
BranchTrueFalseTotal
|| c == 0x20 || c == 0xD || c == 0xA 1364 1127 2491
|| c == 0x20 || c == 0xD || c == 0xA 1358 6 1364
|| c == 0x20 || c == 0xD || c == 0xA 1346 12 1358
398 1346 ▶ || (c && c < 0x80 && strchr("-'()+,./:=?;!*#@$_%", c))))
Branch: 2 branches
BranchTrueFalseTotal
|| (c && c < 0x80 && strchr("-'()+,./:=?;!*#@$_%", c)))) 1345 1 1346
|| (c && c < 0x80 && strchr("-'()+,./:=?;!*#@$_%", c)))) 22 1323 1345
399 23 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "not a public ID character");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "not a public ID character"); 0 23 23
400 10168 frx_buf_append_ch(&l->tmp, (char)c);
401 10168 l->pos++;
402 } else {
403 29821 ▶ if (!frx_lex_char(l)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_char(l)) return 0; 0 29821 29821
404 }
405 }
406 4073 return frx_lex_intern_buf(l, out);
407 }
408  
409 /* SYSTEM S SystemLiteral | PUBLIC S PubidLiteral S SystemLiteral, and with
410 * pubid_only the NOTATION form that stops after the public ID. *sys_at
411 * gets the frame-relative offset of the system literal, or of the keyword. */
412 static int
413 4023 frx_dtd_external_id(frx_lex *l, frx_str *pub, frx_str *sys, int pubid_only, size_t *sys_at)
414 {
415 size_t had;
416 4023 *pub = *sys = FRX_EMPTY_STR;
417 4023 *sys_at = l->pos;
418 4023 ▶ if (frx_dtd_word(l, "SYSTEM")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_word(l, "SYSTEM")) { 3230 793 4023
419 3230 ▶ if (!frx_dtd_need_s(l, "expected whitespace after SYSTEM")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after SYSTEM")) return 0; 8 3222 3230
420 3222 *sys_at = l->pos;
421 3222 return frx_dtd_literal(l, 0, sys);
422 }
423 793 ▶ if (frx_dtd_word(l, "PUBLIC")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_word(l, "PUBLIC")) { 730 63 793
424 730 ▶ if (!frx_dtd_need_s(l, "expected whitespace after PUBLIC")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after PUBLIC")) return 0; 5 725 730
425 725 ▶ if (!frx_dtd_literal(l, 1, pub)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_literal(l, 1, pub)) return 0; 39 686 725
426 686 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 0 686 686
427 686 ▶ if (l->pos < l->len && (l->in[l->pos] == '"' || l->in[l->pos] == '\'')) {
Branch: 3 branches
BranchTrueFalseTotal
if (l->pos < l->len && (l->in[l->pos] == '"' || l->in[l->pos] == '\'')) { 685 1 686
if (l->pos < l->len && (l->in[l->pos] == '"' || l->in[l->pos] == '\'')) { 488 197 685
if (l->pos < l->len && (l->in[l->pos] == '"' || l->in[l->pos] == '\'')) { 4 484 488
428 201 ▶ if (!had) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace before the system literal");
Branch: 2 branches
BranchTrueFalseTotal
if (!had) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace before the system literal"); 3 198 201
if (!had) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace before the system literal"); 0 3 3
429 198 *sys_at = l->pos;
430 198 return frx_dtd_literal(l, 0, sys);
431 }
432 485 ▶ if (!pubid_only) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected a system literal after the public ID");
Branch: 2 branches
BranchTrueFalseTotal
if (!pubid_only) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected a system literal after the public ID"); 4 481 485
if (!pubid_only) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected a system literal after the public ID"); 0 4 4
433 481 return 1;
434 }
435 63 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected SYSTEM or PUBLIC");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected SYSTEM or PUBLIC"); 0 63 63
436 }
437  
438 /* an Nmtoken: NameChar+, the colon included (an Nmtoken is not a QName) */
439 static int
440 2256 frx_dtd_nmtoken(frx_lex *l, frx_str *out)
441 {
442 2256 size_t start = l->pos;
443 6376 ▶ while (l->pos < l->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (l->pos < l->len) { 6372 4 6376
444 6372 unsigned char c = l->in[l->pos];
445 unsigned long cp;
446 size_t n;
447 6372 ▶ if (c < 0x80) { cp = c; n = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { cp = c; n = 1; } 6372 0 6372
448 else {
449 0 n = frx_utf8_decode(l->in + l->pos, l->len - l->pos, &cp);
450 0 ▶ if (!n) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8");
Branch: 2 branches
BranchTrueFalseTotal
if (!n) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); 0 0 0
if (!n) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); 0 0 0
451 }
452 6372 ▶ if (cp != ':' && !frx_is_name_char(cp)) break;
Branch: 2 branches
BranchTrueFalseTotal
if (cp != ':' && !frx_is_name_char(cp)) break; 6360 12 6372
if (cp != ':' && !frx_is_name_char(cp)) break; 2252 4108 6360
453 4120 l->pos += n;
454 }
455 2256 ▶ if (l->pos == start) return FRX_LEX_FAIL(l, FRX_E_DTD, start, "expected a name token");
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos == start) return FRX_LEX_FAIL(l, FRX_E_DTD, start, "expected a name token"); 8 2248 2256
if (l->pos == start) return FRX_LEX_FAIL(l, FRX_E_DTD, start, "expected a name token"); 0 8 8
456 2248 return frx_lex_intern(l, l->in + start, l->pos - start, out);
457 }
458  
459 static int
460 19003 frx_dtd_cm_push(frx_lex *l, int kind, const frx_str *name)
461 {
462 frx_cm_tok t;
463 19003 t.kind = kind;
464 19003 ▶ t.name = name ? *name : FRX_EMPTY_STR;
Branch: 1 branch
BranchTrueFalseTotal
t.name = name ? *name : FRX_EMPTY_STR; 4209 14794 19003
465 19003 frx_buf_append_n(&l->tmp, (const char *)&t, sizeof t);
466 19003 ▶ return l->tmp.failed ? frx_lex_nomem(l) : 1;
Branch: 1 branch
BranchTrueFalseTotal
return l->tmp.failed ? frx_lex_nomem(l) : 1; 0 19003 19003
467 }
468  
469 static int
470 1737 frx_dtd_cm_group_push(frx_lex *l, int *sp, int frame)
471 {
472 1737 ▶ if (*sp == l->cm_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (*sp == l->cm_cap) { 530 1207 1737
473 530 ▶ int ncap = l->cm_cap ? l->cm_cap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = l->cm_cap ? l->cm_cap * 2 : 16; 0 530 530
474 530 int *ns = (int *)realloc(l->cm_stack, (size_t)ncap * sizeof *ns);
475 int *nf;
476 530 ▶ if (!ns) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!ns) return frx_lex_nomem(l); 0 530 530
477 530 l->cm_stack = ns;
478 530 nf = (int *)realloc(l->cm_frame, (size_t)ncap * sizeof *nf);
479 530 ▶ if (!nf) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!nf) return frx_lex_nomem(l); 0 530 530
480 530 l->cm_frame = nf;
481 530 l->cm_cap = ncap;
482 }
483 1737 l->cm_frame[*sp] = frame; /* VC: Proper Group/PE Nesting */
484 1737 l->cm_stack[(*sp)++] = 0; /* separator not yet known */
485 1737 return 1;
486 }
487  
488 /* a modifier right after a name or a closing parenthesis, no S between */
489 static int
490 4822 frx_dtd_cm_modifier(frx_lex *l)
491 {
492 4822 ▶ if (l->pos < l->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos < l->len) { 4822 0 4822
493 4822 unsigned char c = l->in[l->pos];
494 4822 ▶ if (c == '?' || c == '*' || c == '+') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '?' || c == '*' || c == '+') { 4518 304 4822
if (c == '?' || c == '*' || c == '+') { 4063 455 4518
if (c == '?' || c == '*' || c == '+') { 263 3800 4063
495 1022 l->pos++;
496 1022 return frx_dtd_cm_push(l, c, NULL);
497 }
498 }
499 3800 return 1;
500 }
501  
502 /* Mixed or children, section 3.2.1 and 3.2.2; pos is at the opening
503 * parenthesis. The tokens accumulate in tmp as raw frx_cm_tok bytes and
504 * are copied into the arena at the end. A PE may supply any part of it
505 * in external markup, since frx_dtd_ws expands one wherever S may stand. */
506 static int
507 3535 frx_dtd_content_model(frx_lex *l, int *content, const frx_cm_tok **model, int *n_model)
508 {
509 3535 size_t open = l->pos;
510 3535 int open_frame = frx_dtd_cur_seq(l); /* VC: Proper Group/PE Nesting */
511 size_t had;
512 frx_str name;
513 3535 l->tmp.len = 0;
514 3535 l->pos++;
515 3535 ▶ if (!frx_dtd_cm_push(l, '(', NULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_push(l, '(', NULL)) return 0; 0 3535 3535
516 3535 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 1 3534 3535
517  
518 3534 ▶ if (frx_lex_starts(l, l->pos, "#PCDATA")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "#PCDATA")) { 2325 1209 3534
519 2325 int names = 0;
520 2325 l->pos += 7;
521 2325 ▶ if (!frx_dtd_cm_push(l, FRX_CM_PCDATA, NULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_push(l, FRX_CM_PCDATA, NULL)) return 0; 2325 0 2325
522 for (;;) {
523 3401 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 0 3401 3401
524 3401 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 0 3401 3401
525 0 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, open, "unterminated content model");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, open, "unterminated content model"); 0 0 0
526 3401 ▶ if (l->in[l->pos] == ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos] == ')') { 2317 1084 3401
527 2317 ▶ if (frx_dtd_cur_seq(l) != open_frame) frx_dtd_pe_nest(l, open);
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_cur_seq(l) != open_frame) frx_dtd_pe_nest(l, open); 3 2314 2317
528 2317 l->pos++;
529 2317 ▶ if (!frx_dtd_cm_push(l, ')', NULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_push(l, ')', NULL)) return 0; 0 2317 2317
530 2317 ▶ if (l->pos < l->len && l->in[l->pos] == '*') {
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos < l->len && l->in[l->pos] == '*') { 2317 0 2317
if (l->pos < l->len && l->in[l->pos] == '*') { 355 1962 2317
531 355 l->pos++;
532 355 ▶ if (!frx_dtd_cm_push(l, '*', NULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_push(l, '*', NULL)) return 0; 0 355 355
533 1962 ▶ } else if (names) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (names) { 3 1959 1962
534 3 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "a mixed content model that names elements must end in )*");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "a mixed content model that names elements must end in )*"); 0 3 3
535 }
536 2314 break;
537 }
538 1084 ▶ if (l->in[l->pos] != '|')
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos] != '|') 6 1078 1084
539 6 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected | or ) in a mixed content model");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected | or ) in a mixed content model"); 0 6 6
540 1078 l->pos++;
541 1078 ▶ if (!frx_dtd_cm_push(l, '|', NULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_push(l, '|', NULL)) return 0; 0 1078 1078
542 1078 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 0 1078 1078
543 1078 ▶ if (!frx_lex_name(l, 1, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 1, &name)) return 0; 2 1076 1078
544 1076 ▶ if (!frx_dtd_cm_push(l, FRX_CM_NAME, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_push(l, FRX_CM_NAME, &name)) return 0; 0 1076 1076
545 1076 names++;
546 }
547 2314 *content = FRX_CONTENT_MIXED;
548 } else {
549 1209 int sp = 0;
550 1209 int expect_cp = 1;
551 1255 ▶ if (!frx_dtd_cm_group_push(l, &sp, open_frame)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_group_push(l, &sp, open_frame)) return 0; 0 1209 1209
552 8504 ▶ while (sp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (sp > 0) { 7341 1163 8504
553 unsigned char c;
554 7341 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 0 7341 7341
555 7341 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 0 7341 7341
556 0 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, open, "unterminated content model");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, open, "unterminated content model"); 0 0 0
557 7341 c = l->in[l->pos];
558 7341 ▶ if (expect_cp) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_cp) { 3682 3659 7341
559 3682 ▶ if (c == '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '(') { 528 3154 3682
560 528 l->pos++;
561 528 ▶ if (!frx_dtd_cm_push(l, '(', NULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_push(l, '(', NULL)) return 0; 0 528 528
562 528 ▶ if (!frx_dtd_cm_group_push(l, &sp, frx_dtd_cur_seq(l))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_group_push(l, &sp, frx_dtd_cur_seq(l))) return 0; 0 528 528
563 528 continue;
564 }
565 3154 ▶ if (!frx_lex_name(l, 1, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 1, &name)) return 0; 21 3133 3154
566 3133 ▶ if (!frx_dtd_cm_push(l, FRX_CM_NAME, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_push(l, FRX_CM_NAME, &name)) return 0; 0 3133 3133
567 3133 ▶ if (!frx_dtd_cm_modifier(l)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_modifier(l)) return 0; 0 3133 3133
568 3133 expect_cp = 0;
569 3133 continue;
570 }
571 3659 ▶ if (c == '|' || c == ',') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '|' || c == ',') { 2517 1142 3659
if (c == '|' || c == ',') { 811 1706 2517
572 1953 int *sep = &l->cm_stack[sp - 1];
573 1953 ▶ if (*sep == 0) *sep = c;
Branch: 1 branch
BranchTrueFalseTotal
if (*sep == 0) *sep = c; 962 991 1953
574 991 ▶ else if (*sep != (int)c)
Branch: 1 branch
BranchTrueFalseTotal
else if (*sep != (int)c) 8 983 991
575 8 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "a content model group is a choice or a sequence, not both");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "a content model group is a choice or a sequence, not both"); 0 8 8
576 1945 l->pos++;
577 1945 ▶ if (!frx_dtd_cm_push(l, c, NULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_push(l, c, NULL)) return 0; 0 1945 1945
578 1945 expect_cp = 1;
579 1945 continue;
580 }
581 1706 ▶ if (c == ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == ')') { 1689 17 1706
582 1689 ▶ if (frx_dtd_cur_seq(l) != l->cm_frame[sp - 1]) frx_dtd_pe_nest(l, open);
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_cur_seq(l) != l->cm_frame[sp - 1]) frx_dtd_pe_nest(l, open); 3 1686 1689
583 1689 l->pos++;
584 1689 ▶ if (!frx_dtd_cm_push(l, ')', NULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_push(l, ')', NULL)) return 0; 0 1689 1689
585 1689 sp--;
586 1689 ▶ if (!frx_dtd_cm_modifier(l)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_cm_modifier(l)) return 0; 0 1689 1689
587 1689 continue;
588 }
589 17 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected |, a comma or ) in a content model");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected |, a comma or ) in a content model"); 0 17 17
590 }
591 1163 *content = FRX_CONTENT_CHILDREN;
592 }
593  
594 {
595 3477 size_t n = l->tmp.len / sizeof(frx_cm_tok);
596 3477 frx_cm_tok *toks = (frx_cm_tok *)frx_arena_alloc(l->arena, n * sizeof *toks);
597 3477 ▶ if (!toks) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!toks) return frx_lex_nomem(l); 0 3477 3477
598 3477 memcpy(toks, l->tmp.p, n * sizeof *toks);
599 3477 *model = toks;
600 3477 *n_model = (int)n;
601 }
602 3477 return 1;
603 }
604  
605 /* the > that ends a declaration, after optional whitespace */
606 static int
607 15051 frx_dtd_close(frx_lex *l, size_t at, const char *unterminated)
608 {
609 size_t had;
610 15051 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 2 15049 15051
611 15049 ▶ if (l->pos >= l->len || l->in[l->pos] != '>')
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos >= l->len || l->in[l->pos] != '>') 15039 10 15049
if (l->pos >= l->len || l->in[l->pos] != '>') 58 14981 15039
612 68 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, unterminated);
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, unterminated); 1 67 68
613 /* VC: Proper Declaration/PE Nesting: the > is in the replacement
614 * text the <! was in, or in neither */
615 14981 ▶ if (frx_dtd_cur_seq(l) != l->decl_seq) frx_dtd_pe_nest(l, at);
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_cur_seq(l) != l->decl_seq) frx_dtd_pe_nest(l, at); 3 14978 14981
616 14981 l->pos++;
617 14981 return 1;
618 }
619  
620 /* <!ELEMENT S Name S contentspec S? >; pos is past the keyword */
621 static int
622 7849 frx_dtd_element_decl(frx_lex *l, size_t at)
623 {
624 frx_str name;
625 frx_eldecl *ed;
626 7849 int content = FRX_CONTENT_EMPTY;
627 7849 const frx_cm_tok *model = NULL;
628 7849 int n_model = 0;
629 7849 ▶ if (!frx_dtd_need_s(l, "expected whitespace after ELEMENT")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after ELEMENT")) return 0; 3 7846 7849
630 7846 ▶ if (!frx_lex_name(l, 1, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 1, &name)) return 0; 17 7829 7846
631 7829 ▶ if (!frx_dtd_need_s(l, "expected whitespace after the element name")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after the element name")) return 0; 10 7819 7829
632 7819 ▶ if (frx_dtd_word(l, "EMPTY")) content = FRX_CONTENT_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_word(l, "EMPTY")) content = FRX_CONTENT_EMPTY; 1871 5948 7819
633 5948 ▶ else if (frx_dtd_word(l, "ANY")) content = FRX_CONTENT_ANY;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "ANY")) content = FRX_CONTENT_ANY; 2385 3563 5948
634 3563 ▶ else if (l->pos < l->len && l->in[l->pos] == '(') {
Branch: 2 branches
BranchTrueFalseTotal
else if (l->pos < l->len && l->in[l->pos] == '(') { 3561 2 3563
else if (l->pos < l->len && l->in[l->pos] == '(') { 3535 26 3561
635 3535 ▶ if (!frx_dtd_content_model(l, &content, &model, &n_model)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_content_model(l, &content, &model, &n_model)) return 0; 58 3477 3535
636 } else {
637 28 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected EMPTY, ANY or a content model");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected EMPTY, ANY or a content model"); 1 27 28
638 }
639 7733 ▶ if (!frx_dtd_close(l, at, "unterminated ELEMENT declaration")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_close(l, at, "unterminated ELEMENT declaration")) return 0; 22 7711 7733
640 7711 ▶ ed = frx_dtd_eldecl_get(l, &name, FRX_LEX_ABS(l, at));
Branch: 1 branch
BranchTrueFalseTotal
ed = frx_dtd_eldecl_get(l, &name, FRX_LEX_ABS(l, at)); 1939 5772 7711
641 7711 ▶ if (!ed) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!ed) return frx_lex_nomem(l); 0 7711 7711
642 7711 ▶ if (!ed->declared++) { /* a second declaration is counted and dropped */
Branch: 1 branch
BranchTrueFalseTotal
if (!ed->declared++) { /* a second declaration is counted and dropped */ 7707 4 7711
643 7707 ed->content = content;
644 7707 ed->model = model;
645 7707 ed->n_model = n_model;
646 7707 ed->external = l->ext_decl;
647 }
648 7711 return 1;
649 }
650  
651 /* <!ATTLIST S Name AttDef* S? >; pos is past the keyword */
652 static int
653 4437 frx_dtd_attlist_decl(frx_lex *l, size_t at)
654 {
655 frx_str ename;
656 frx_eldecl *ed;
657 4437 ▶ if (!frx_dtd_need_s(l, "expected whitespace after ATTLIST")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after ATTLIST")) return 0; 4 4433 4437
658 4433 ▶ if (!frx_lex_name(l, 1, &ename)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 1, &ename)) return 0; 5 4428 4433
659 4428 ▶ ed = frx_dtd_eldecl_get(l, &ename, FRX_LEX_ABS(l, at));
Branch: 1 branch
BranchTrueFalseTotal
ed = frx_dtd_eldecl_get(l, &ename, FRX_LEX_ABS(l, at)); 894 3534 4428
660 4428 ▶ if (!ed) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!ed) return frx_lex_nomem(l); 4428 0 4428
661 6979 for (;;) {
662 size_t had, def_at;
663 frx_str aname, value;
664 11407 int type, def = FRX_DEF_VALUE, has_value = 0, n_enums = 0;
665 11407 const frx_str *enums = NULL;
666 11586 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 1 11406 11407
667 11406 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 7 11399 11406
668 7 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, "unterminated ATTLIST declaration");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, "unterminated ATTLIST declaration"); 0 7 7
669 11399 ▶ if (l->in[l->pos] == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos] == '>') { 4248 7151 11399
670 4248 ▶ if (frx_dtd_cur_seq(l) != l->decl_seq) frx_dtd_pe_nest(l, at);
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_cur_seq(l) != l->decl_seq) frx_dtd_pe_nest(l, at); 2 4246 4248
671 4248 l->pos++;
672 4248 break;
673 }
674 7151 ▶ if (!had)
Branch: 1 branch
BranchTrueFalseTotal
if (!had) 3 7148 7151
675 3 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace before an attribute definition");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace before an attribute definition"); 0 3 3
676 7148 def_at = l->pos;
677 7148 ▶ if (!frx_lex_name(l, 1, &aname)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 1, &aname)) return 0; 11 7137 7148
678 7137 ▶ if (!frx_dtd_need_s(l, "expected whitespace after the attribute name")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after the attribute name")) return 0; 9 7128 7137
679  
680 /* AttType */
681 7128 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 3 7125 7128
682 3 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, "unterminated ATTLIST declaration");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, "unterminated ATTLIST declaration"); 0 3 3
683 7125 ▶ if (l->in[l->pos] == '(' || frx_lex_starts(l, l->pos, "NOTATION")) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->in[l->pos] == '(' || frx_lex_starts(l, l->pos, "NOTATION")) { 6072 1053 7125
if (l->in[l->pos] == '(' || frx_lex_starts(l, l->pos, "NOTATION")) { 104 5968 6072
684 1157 int notation = l->in[l->pos] != '(';
685 size_t open;
686 1157 int n = 0;
687 1157 ▶ type = notation ? FRX_ATT_NOTATION : FRX_ATT_ENUM;
Branch: 1 branch
BranchTrueFalseTotal
type = notation ? FRX_ATT_NOTATION : FRX_ATT_ENUM; 104 1053 1157
688 1157 ▶ if (notation) {
Branch: 1 branch
BranchTrueFalseTotal
if (notation) { 104 1053 1157
689 104 l->pos += 8;
690 104 ▶ if (!frx_dtd_need_s(l, "expected whitespace after NOTATION")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after NOTATION")) return 0; 4 100 104
691 }
692 1153 ▶ if (l->pos >= l->len || l->in[l->pos] != '(')
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos >= l->len || l->in[l->pos] != '(') 1153 0 1153
if (l->pos >= l->len || l->in[l->pos] != '(') 3 1150 1153
693 3 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected ( after NOTATION");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected ( after NOTATION"); 0 3 3
694 1150 open = l->pos;
695 1150 l->pos++;
696 1150 l->tmp.len = 0;
697 1261 for (;;) {
698 frx_str tok;
699 2435 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 0 2411 2411
700 2411 ▶ if (notation ? !frx_lex_name(l, 0, &tok) : !frx_dtd_nmtoken(l, &tok)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (notation ? !frx_lex_name(l, 0, &tok) : !frx_dtd_nmtoken(l, &tok)) return 0; 155 2256 2411
if (notation ? !frx_lex_name(l, 0, &tok) : !frx_dtd_nmtoken(l, &tok)) return 0; 13 2398 2411
701 2398 frx_buf_append_n(&l->tmp, (const char *)&tok, sizeof tok);
702 2398 ▶ if (l->tmp.failed) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (l->tmp.failed) return frx_lex_nomem(l); 0 2398 2398
703 2398 n++;
704 2398 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 0 2398 2398
705 2398 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 2 2396 2398
706 2 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, open, "unterminated enumeration");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, open, "unterminated enumeration"); 0 2 2
707 2396 ▶ if (l->in[l->pos] == ')') { l->pos++; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos] == ')') { l->pos++; break; } 1126 1270 2396
708 1270 ▶ if (l->in[l->pos] != '|')
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos] != '|') 9 1261 1270
709 9 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected | or ) in an enumeration");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected | or ) in an enumeration"); 0 9 9
710 1261 l->pos++;
711 }
712 {
713 1126 frx_str *arr = (frx_str *)frx_arena_alloc(l->arena, (size_t)n * sizeof *arr);
714 1126 ▶ if (!arr) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!arr) return frx_lex_nomem(l); 0 1126 1126
715 1126 memcpy(arr, l->tmp.p, (size_t)n * sizeof *arr);
716 1126 enums = arr;
717 1126 n_enums = n;
718 }
719 }
720 5968 ▶ else if (frx_dtd_word(l, "CDATA")) type = FRX_ATT_CDATA;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "CDATA")) type = FRX_ATT_CDATA; 3428 2540 5968
721 2540 ▶ else if (frx_dtd_word(l, "IDREFS")) type = FRX_ATT_IDREFS;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "IDREFS")) type = FRX_ATT_IDREFS; 110 2430 2540
722 2430 ▶ else if (frx_dtd_word(l, "IDREF")) type = FRX_ATT_IDREF;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "IDREF")) type = FRX_ATT_IDREF; 230 2200 2430
723 2200 ▶ else if (frx_dtd_word(l, "ID")) type = FRX_ATT_ID;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "ID")) type = FRX_ATT_ID; 1279 921 2200
724 921 ▶ else if (frx_dtd_word(l, "ENTITIES")) type = FRX_ATT_ENTITIES;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "ENTITIES")) type = FRX_ATT_ENTITIES; 96 825 921
725 825 ▶ else if (frx_dtd_word(l, "ENTITY")) type = FRX_ATT_ENTITY;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "ENTITY")) type = FRX_ATT_ENTITY; 123 702 825
726 702 ▶ else if (frx_dtd_word(l, "NMTOKENS")) type = FRX_ATT_NMTOKENS;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "NMTOKENS")) type = FRX_ATT_NMTOKENS; 193 509 702
727 509 ▶ else if (frx_dtd_word(l, "NMTOKEN")) type = FRX_ATT_NMTOKEN;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "NMTOKEN")) type = FRX_ATT_NMTOKEN; 457 52 509
728 52 ▶ else return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected an attribute type");
Branch: 1 branch
BranchTrueFalseTotal
else return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected an attribute type"); 0 52 52
729  
730 7042 ▶ if (!frx_dtd_need_s(l, "expected whitespace before the default declaration")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace before the default declaration")) return 0; 13 7029 7042
731  
732 /* DefaultDecl; a default value is an AttValue, read as one: entity
733 * references included in the literal, a < refused, the type's
734 * normalisation applied; a PE reference is not recognised in it */
735 7029 ▶ if (frx_dtd_word(l, "#REQUIRED")) def = FRX_DEF_REQUIRED;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_word(l, "#REQUIRED")) def = FRX_DEF_REQUIRED; 507 6522 7029
736 6522 ▶ else if (frx_dtd_word(l, "#IMPLIED")) def = FRX_DEF_IMPLIED;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "#IMPLIED")) def = FRX_DEF_IMPLIED; 3628 2894 6522
737 else {
738 2894 ▶ if (frx_dtd_word(l, "#FIXED")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_word(l, "#FIXED")) { 159 2735 2894
739 159 def = FRX_DEF_FIXED;
740 159 ▶ if (!frx_dtd_need_s(l, "expected whitespace after #FIXED")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after #FIXED")) return 0; 3 156 159
741 }
742 2891 ▶ if (l->pos >= l->len || (l->in[l->pos] != '"' && l->in[l->pos] != '\''))
Branch: 3 branches
BranchTrueFalseTotal
if (l->pos >= l->len || (l->in[l->pos] != '"' && l->in[l->pos] != '\'')) 2888 3 2891
if (l->pos >= l->len || (l->in[l->pos] != '"' && l->in[l->pos] != '\'')) 88 2800 2888
if (l->pos >= l->len || (l->in[l->pos] != '"' && l->in[l->pos] != '\'')) 30 58 88
743 33 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected #REQUIRED, #IMPLIED, #FIXED or a quoted default value");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected #REQUIRED, #IMPLIED, #FIXED or a quoted default value"); 0 33 33
744 2858 ▶ if (!frx_lex_attr_value(l, &value, type != FRX_ATT_CDATA)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_attr_value(l, &value, type != FRX_ATT_CDATA)) return 0; 14 2844 2858
745 2844 has_value = 1;
746 }
747  
748 6979 ▶ if (!frx_dtd_attdef(ed, &aname)) { /* the first definition binds */
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_attdef(ed, &aname)) { /* the first definition binds */ 6881 98 6979
749 6881 frx_attdef *ad = (frx_attdef *)frx_arena_alloc(l->arena, sizeof *ad);
750 6881 ▶ if (!ad) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!ad) return frx_lex_nomem(l); 0 6881 6881
751 6881 memset(ad, 0, sizeof *ad);
752 6881 ad->name = aname;
753 6881 ad->type = type;
754 6881 ad->enums = enums;
755 6881 ad->n_enums = n_enums;
756 6881 ad->def = def;
757 6881 ad->has_value = has_value;
758 6881 ▶ ad->value = has_value ? value : FRX_EMPTY_STR;
Branch: 1 branch
BranchTrueFalseTotal
ad->value = has_value ? value : FRX_EMPTY_STR; 2800 4081 6881
759 6881 ad->external = l->ext_decl;
760 6881 ▶ ad->offset = FRX_LEX_ABS(l, def_at);
Branch: 1 branch
BranchTrueFalseTotal
ad->offset = FRX_LEX_ABS(l, def_at); 1870 5011 6881
761 6881 ▶ if (ed->last_attdef) ed->last_attdef->next = ad;
Branch: 1 branch
BranchTrueFalseTotal
if (ed->last_attdef) ed->last_attdef->next = ad; 2984 3897 6881
762 3897 else ed->attdefs = ad;
763 6881 ed->last_attdef = ad;
764 6881 ed->n_attdefs++;
765 }
766 }
767 4248 return 1;
768 }
769  
770 /* EntityValue, section 4.5: character references become the character,
771 * general entity references stay as written, and a parameter entity
772 * reference is refused in the internal subset and included in the literal
773 * (4.4.5) in external markup, where the literal's own quote is the only
774 * one that ends it. */
775 static int
776 5057 frx_dtd_entity_value(frx_lex *l, frx_str *out)
777 {
778 5057 unsigned char q = l->in[l->pos];
779 5057 size_t start = l->pos;
780 5057 int frame0 = l->n_frames;
781 5057 l->pos++;
782 5057 l->tmp.len = 0;
783 187600 for (;;) {
784 unsigned char c;
785 192657 ▶ if (l->pos >= l->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) { 384 192273 192657
786 384 ▶ if (l->n_frames > frame0) { if (!frx_entity_pop(l)) return 0; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (l->n_frames > frame0) { if (!frx_entity_pop(l)) return 0; continue; } 327 57 384
if (l->n_frames > frame0) { if (!frx_entity_pop(l)) return 0; continue; } 0 327 327
787 57 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, start, "unterminated entity value");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, start, "unterminated entity value"); 0 57 57
788 }
789 192273 c = l->in[l->pos];
790 192273 ▶ if (c == q && l->n_frames == frame0) { l->pos++; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == q && l->n_frames == frame0) { l->pos++; break; } 4999 187274 192273
if (c == q && l->n_frames == frame0) { l->pos++; break; } 4963 36 4999
791 187310 ▶ if (c == '%') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '%') { 337 186973 187310
792 337 size_t at = l->pos;
793 337 ▶ if (!l->ext_decl)
Branch: 1 branch
BranchTrueFalseTotal
if (!l->ext_decl) 8 329 337
794 8 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, FRX_DTD_PE_MESSAGE);
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, FRX_DTD_PE_MESSAGE); 0 8 8
795 329 l->pos++;
796 329 ▶ if (!frx_entity_pe_reference(l, at, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_entity_pe_reference(l, at, 0)) return 0; 2 327 329
797 327 continue;
798 }
799 186973 ▶ if (c == '&') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '&') { 2777 184196 186973
800 2777 ▶ if (l->pos + 1 < l->len && l->in[l->pos + 1] == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos + 1 < l->len && l->in[l->pos + 1] == '#') { 2776 1 2777
if (l->pos + 1 < l->len && l->in[l->pos + 1] == '#') { 916 1860 2776
801 916 ▶ if (!frx_lex_ref(l, 0)) return 0; /* the numeric branch */
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_ref(l, 0)) return 0; /* the numeric branch */ 13 903 916
802 } else {
803 1861 size_t s = l->pos;
804 frx_str name;
805 1861 l->pos++;
806 1863 ▶ if (!frx_lex_name(l, 0, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 0, &name)) return 0; 7 1854 1861
807 1854 ▶ if (l->pos >= l->len || l->in[l->pos] != ';')
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos >= l->len || l->in[l->pos] != ';') 1854 0 1854
if (l->pos >= l->len || l->in[l->pos] != ';') 2 1852 1854
808 2 ▶ return FRX_LEX_FAIL(l, FRX_E_REFERENCE, s, "malformed entity reference");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_REFERENCE, s, "malformed entity reference"); 0 2 2
809 1852 l->pos++;
810 1852 frx_buf_append_n(&l->tmp, (const char *)(l->in + s), l->pos - s);
811 }
812 2755 continue;
813 }
814 184196 ▶ if (!frx_lex_char(l)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_char(l)) return 0; 5 184191 184196
815 }
816 4963 return frx_lex_intern_buf(l, out);
817 }
818  
819 /* <!ENTITY S (Name S EntityDef | % S Name S PEDef) S? >; pos is past the keyword */
820 static int
821 6698 frx_dtd_entity_decl(frx_lex *l, size_t at)
822 {
823 frx_entity *e;
824 frx_str name;
825 size_t had;
826 6698 int is_pe = 0;
827 6698 ▶ if (!frx_lex_skip_s(l))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_skip_s(l)) 15 6683 6698
828 15 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace after ENTITY");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace after ENTITY"); 0 15 15
829 6683 ▶ if (l->pos < l->len && l->in[l->pos] == '%') {
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos < l->len && l->in[l->pos] == '%') { 6679 4 6683
if (l->pos < l->len && l->in[l->pos] == '%') { 1531 5148 6679
830 1531 l->pos++;
831 1531 ▶ if (l->pos < l->len && !frx_is_s(l->in[l->pos])) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos < l->len && !frx_is_s(l->in[l->pos])) { 1530 1 1531
if (l->pos < l->len && !frx_is_s(l->in[l->pos])) { 3 1527 1530
832 3 ▶ if (!l->ext_decl) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos - 1, FRX_DTD_PE_MESSAGE);
Branch: 2 branches
BranchTrueFalseTotal
if (!l->ext_decl) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos - 1, FRX_DTD_PE_MESSAGE); 3 0 3
if (!l->ext_decl) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos - 1, FRX_DTD_PE_MESSAGE); 0 3 3
833 0 l->pos--; /* external markup: a PE supplies what follows */
834 } else {
835 1528 is_pe = 1;
836 }
837 }
838 6680 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 0 6680 6680
839 6680 ▶ if (!is_pe && l->pos < l->len && l->in[l->pos] == '%') { /* "% " after a PE frame */
Branch: 3 branches
BranchTrueFalseTotal
if (!is_pe && l->pos < l->len && l->in[l->pos] == '%') { /* "% " after a PE frame */ 5152 1528 6680
if (!is_pe && l->pos < l->len && l->in[l->pos] == '%') { /* "% " after a PE frame */ 5148 4 5152
if (!is_pe && l->pos < l->len && l->in[l->pos] == '%') { /* "% " after a PE frame */ 0 5148 5148
840 0 l->pos++;
841 0 ▶ if (!frx_dtd_need_s(l, "expected whitespace after % in a parameter entity declaration")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after % in a parameter entity declaration")) return 0; 0 0 0
842 0 is_pe = 1;
843 6680 ▶ } else if (is_pe && !had) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (is_pe && !had) { 1528 5152 6680
} else if (is_pe && !had) { 1 1527 1528
844 1 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace after % in a parameter entity declaration");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace after % in a parameter entity declaration"); 0 1 1
845 }
846 6679 ▶ if (!frx_lex_name(l, 0, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 0, &name)) return 0; 17 6662 6679
847 6662 ▶ if (!frx_dtd_need_s(l, "expected whitespace after the entity name")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after the entity name")) return 0; 20 6642 6662
848  
849 6642 e = (frx_entity *)frx_arena_alloc(l->arena, sizeof *e);
850 6642 ▶ if (!e) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return frx_lex_nomem(l); 0 6642 6642
851 6642 memset(e, 0, sizeof *e);
852 6642 e->name = name;
853 6642 e->is_pe = is_pe;
854 6642 ▶ e->offset = FRX_LEX_ABS(l, at);
Branch: 1 branch
BranchTrueFalseTotal
e->offset = FRX_LEX_ABS(l, at); 1161 5481 6642
855 6642 e->external_decl = l->ext_decl;
856 6642 ▶ e->direct = !l->ext_decl && !l->in_dtd;
Branch: 2 branches
BranchTrueFalseTotal
e->direct = !l->ext_decl && !l->in_dtd; 5482 1160 6642
e->direct = !l->ext_decl && !l->in_dtd; 5016 466 5482
857 6642 e->base = l->cur_base;
858 6642 e->flat = l->flat;
859 6642 e->public_id = e->system_id = e->ndata = e->text = e->padded = e->uri = FRX_EMPTY_STR;
860  
861 6642 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 4 6638 6642
862 4 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, "unterminated ENTITY declaration");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, "unterminated ENTITY declaration"); 0 4 4
863 6638 ▶ if (l->in[l->pos] == '"' || l->in[l->pos] == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (l->in[l->pos] == '"' || l->in[l->pos] == '\'') { 2205 4433 6638
if (l->in[l->pos] == '"' || l->in[l->pos] == '\'') { 624 1581 2205
864 5057 ▶ e->text_offset = FRX_LEX_ABS(l, l->pos + 1);
Branch: 1 branch
BranchTrueFalseTotal
e->text_offset = FRX_LEX_ABS(l, l->pos + 1); 991 4066 5057
865 5057 ▶ if (!frx_dtd_entity_value(l, &e->text)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_entity_value(l, &e->text)) return 0; 94 4963 5057
866 } else {
867 size_t sys_at;
868 1593 ▶ if (!frx_dtd_external_id(l, &e->public_id, &e->system_id, 0, &sys_at)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_external_id(l, &e->public_id, &e->system_id, 0, &sys_at)) return 0; 76 1505 1581
869 1505 e->external = 1;
870 1505 ▶ if (!is_pe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_pe) { 1394 111 1505
871 1394 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 0 1394 1394
872 1394 ▶ if (frx_lex_starts(l, l->pos, "NDATA")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "NDATA")) { 562 832 1394
873 562 ▶ if (!had) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace before NDATA");
Branch: 2 branches
BranchTrueFalseTotal
if (!had) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace before NDATA"); 4 558 562
if (!had) return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace before NDATA"); 0 4 4
874 558 l->pos += 5;
875 558 ▶ if (!frx_dtd_need_s(l, "expected whitespace after NDATA")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after NDATA")) return 0; 5 553 558
876 553 ▶ if (!frx_lex_name(l, 0, &e->ndata)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 0, &e->ndata)) return 0; 3 550 553
877 550 e->unparsed = 1;
878 }
879 }
880 }
881 6456 ▶ if (!frx_dtd_close(l, at, "unterminated ENTITY declaration")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_close(l, at, "unterminated ENTITY declaration")) return 0; 43 6413 6456
882 6413 ▶ if (!frx_dtd_entity(l->dtd, &name, is_pe)) { /* the first declaration binds */
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_entity(l->dtd, &name, is_pe)) { /* the first declaration binds */ 6321 92 6413
883 6321 ▶ if (!frx_dtd_map_add(l->arena, is_pe ? &l->dtd->params : &l->dtd->general, &name, e))
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_dtd_map_add(l->arena, is_pe ? &l->dtd->params : &l->dtd->general, &name, e)) 1451 4870 6321
if (!frx_dtd_map_add(l->arena, is_pe ? &l->dtd->params : &l->dtd->general, &name, e)) 0 6321 6321
884 0 return frx_lex_nomem(l);
885 }
886 6413 return 1;
887 }
888  
889 /* <!NOTATION S Name S (ExternalID | PublicID) S? >; pos is past the keyword */
890 static int
891 919 frx_dtd_notation_decl(frx_lex *l, size_t at)
892 {
893 frx_notation *n;
894 frx_str name;
895 size_t sys_at;
896 919 ▶ if (!frx_dtd_need_s(l, "expected whitespace after NOTATION")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after NOTATION")) return 0; 4 915 919
897 915 ▶ if (!frx_lex_name(l, 0, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 0, &name)) return 0; 2 913 915
898 913 ▶ if (!frx_dtd_need_s(l, "expected whitespace after the notation name")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_need_s(l, "expected whitespace after the notation name")) return 0; 1 912 913
899 912 n = (frx_notation *)frx_arena_alloc(l->arena, sizeof *n);
900 912 ▶ if (!n) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return frx_lex_nomem(l); 0 912 912
901 912 n->name = name;
902 912 ▶ n->offset = FRX_LEX_ABS(l, at);
Branch: 1 branch
BranchTrueFalseTotal
n->offset = FRX_LEX_ABS(l, at); 186 726 912
903 912 ▶ if (!frx_dtd_external_id(l, &n->public_id, &n->system_id, 1, &sys_at)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_external_id(l, &n->public_id, &n->system_id, 1, &sys_at)) return 0; 50 862 912
904 862 ▶ if (!frx_dtd_close(l, at, "unterminated NOTATION declaration")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_close(l, at, "unterminated NOTATION declaration")) return 0; 5 857 862
905 857 ▶ if (!frx_dtd_map_find(&l->dtd->notations, &name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_map_find(&l->dtd->notations, &name)) { 857 0 857
906 857 ▶ if (!frx_dtd_map_add(l->arena, &l->dtd->notations, &name, n)) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_map_add(l->arena, &l->dtd->notations, &name, n)) return frx_lex_nomem(l); 0 857 857
907 }
908 857 return 1;
909 }
910  
911 /* <![ S? INCLUDE S? [ ... ]]> opens a section the loop keeps reading;
912 * <![ S? IGNORE S? [ ... ]]> is skipped here with its nested sections.
913 * pos is past the <![. External markup only. */
914 static int
915 356 frx_dtd_conditional(frx_lex *l, size_t at)
916 {
917 size_t had;
918 int include;
919 356 int open_frame = l->decl_seq; /* the text the <![ stood in */
920 356 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 0 356 356
921 356 ▶ if (frx_dtd_word(l, "INCLUDE")) include = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_word(l, "INCLUDE")) include = 1; 153 203 356
922 203 ▶ else if (frx_dtd_word(l, "IGNORE")) include = 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (frx_dtd_word(l, "IGNORE")) include = 0; 188 15 203
923 15 ▶ else return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected INCLUDE or IGNORE in a conditional section");
Branch: 1 branch
BranchTrueFalseTotal
else return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected INCLUDE or IGNORE in a conditional section"); 14 1 15
924 341 ▶ if (!frx_dtd_ws(l, &had)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_ws(l, &had)) return 0; 0 341 341
925 341 ▶ if (l->pos >= l->len || l->in[l->pos] != '[')
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos >= l->len || l->in[l->pos] != '[') 341 0 341
if (l->pos >= l->len || l->in[l->pos] != '[') 3 338 341
926 3 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected [ after INCLUDE or IGNORE");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected [ after INCLUDE or IGNORE"); 3 0 3
927 /* VC: Proper Conditional Section/PE Nesting */
928 338 ▶ if (frx_dtd_cur_seq(l) != open_frame) frx_dtd_pe_nest(l, at);
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_cur_seq(l) != open_frame) frx_dtd_pe_nest(l, at); 2 336 338
929 338 l->pos++;
930 338 ▶ if (include) {
Branch: 1 branch
BranchTrueFalseTotal
if (include) { 151 187 338
931 151 ▶ if (l->cond_depth == l->cond_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->cond_depth == l->cond_cap) { 121 30 151
932 121 ▶ int ncap = l->cond_cap ? l->cond_cap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = l->cond_cap ? l->cond_cap * 2 : 16; 0 121 121
933 121 int *nc = (int *)realloc(l->cond_frame, (size_t)ncap * sizeof *nc);
934 121 ▶ if (!nc) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!nc) return frx_lex_nomem(l); 0 121 121
935 121 l->cond_frame = nc;
936 121 l->cond_cap = ncap;
937 }
938 151 l->cond_frame[l->cond_depth++] = open_frame;
939 151 return 1;
940 }
941 {
942 187 int depth = 1;
943 8741 ▶ while (depth) {
Branch: 1 branch
BranchTrueFalseTotal
while (depth) { 8560 181 8741
944 8560 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 6 8554 8560
945 6 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, "unterminated IGNORE section");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, "unterminated IGNORE section"); 6 0 6
946 8554 ▶ if (frx_lex_starts(l, l->pos, "<![")) { depth++; l->pos += 3; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "<![")) { depth++; l->pos += 3; continue; } 103 8451 8554
947 8451 ▶ if (frx_lex_starts(l, l->pos, "]]>")) { depth--; l->pos += 3; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "]]>")) { depth--; l->pos += 3; continue; } 283 8168 8451
948 8168 l->pos++;
949 }
950 }
951 181 return 1;
952 }
953  
954 /* the subset loop: the internal subset from past the [ to its ] (with
955 * *close its frame-relative offset), or, with external set, the fetched
956 * text of the external subset from its first byte to its end. A frame
957 * that ends between declarations is popped; a declaration's own frames
958 * are the declaration parser's business. */
959 static int
960 6148 frx_dtd_subset(frx_lex *l, size_t open, size_t *close, int external)
961 {
962 6148 int frame0 = l->n_frames;
963 frx_tok scratch;
964 6148 l->in_subset = 1;
965 50061 for (;;) {
966 unsigned char c;
967 size_t at;
968 56209 ▶ if (l->pos >= l->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) { 1644 54565 56209
969 1644 ▶ if (l->n_frames > frame0) { if (!frx_entity_pop(l)) return 0; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (l->n_frames > frame0) { if (!frx_entity_pop(l)) return 0; continue; } 658 986 1644
if (l->n_frames > frame0) { if (!frx_entity_pop(l)) return 0; continue; } 0 658 658
970 986 ▶ if (external) break;
Branch: 1 branch
BranchTrueFalseTotal
if (external) break; 967 19 986
971 19 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, open, "unterminated internal subset");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, open, "unterminated internal subset"); 0 19 19
972 }
973 54565 c = l->in[l->pos];
974 54565 ▶ if (!external && c == ']' && l->n_frames == frame0) { *close = l->pos; l->pos++; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (!external && c == ']' && l->n_frames == frame0) { *close = l->pos; l->pos++; break; } 43200 11365 54565
if (!external && c == ']' && l->n_frames == frame0) { *close = l->pos; l->pos++; break; } 4262 38938 43200
if (!external && c == ']' && l->n_frames == frame0) { *close = l->pos; l->pos++; break; } 4260 2 4262
975 50305 ▶ if (frx_lex_skip_s(l)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_skip_s(l)) continue; 27134 23171 50305
976 23171 at = l->pos;
977 23171 l->decl_frame0 = l->n_frames;
978 23171 l->decl_seq = frx_dtd_cur_seq(l);
979 23171 ▶ if (c == '%') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '%') { 704 22467 23171
980 704 l->pos++;
981 704 ▶ if (!frx_entity_pe_reference(l, at, 1)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_entity_pe_reference(l, at, 1)) return 0; 30 674 704
982 674 continue;
983 }
984 22467 ▶ if (frx_lex_starts(l, at, "<!--")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, at, "<!--")) { 1508 20959 22467
985 1508 l->tok_start = at;
986 1508 l->pos += 4;
987 1508 ▶ if (!frx_lex_comment(l, &scratch)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_comment(l, &scratch)) return 0; 5 1503 1508
988 1503 continue;
989 }
990 20959 ▶ if (frx_lex_starts(l, at, "<?")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, at, "<?")) { 399 20560 20959
991 399 l->tok_start = at;
992 399 l->pos += 2;
993 399 ▶ if (!frx_lex_pi(l, &scratch)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_pi(l, &scratch)) return 0; 11 388 399
994 388 continue;
995 }
996 20560 ▶ if (frx_lex_starts(l, at, "<!ELEMENT")) { l->pos += 9; if (!frx_dtd_element_decl(l, at)) return 0; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (frx_lex_starts(l, at, "<!ELEMENT")) { l->pos += 9; if (!frx_dtd_element_decl(l, at)) return 0; continue; } 7849 12711 20560
if (frx_lex_starts(l, at, "<!ELEMENT")) { l->pos += 9; if (!frx_dtd_element_decl(l, at)) return 0; continue; } 138 7711 7849
997 12711 ▶ if (frx_lex_starts(l, at, "<!ATTLIST")) { l->pos += 9; if (!frx_dtd_attlist_decl(l, at)) return 0; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (frx_lex_starts(l, at, "<!ATTLIST")) { l->pos += 9; if (!frx_dtd_attlist_decl(l, at)) return 0; continue; } 4437 8274 12711
if (frx_lex_starts(l, at, "<!ATTLIST")) { l->pos += 9; if (!frx_dtd_attlist_decl(l, at)) return 0; continue; } 189 4248 4437
998 8274 ▶ if (frx_lex_starts(l, at, "<!ENTITY")) { l->pos += 8; if (!frx_dtd_entity_decl(l, at)) return 0; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (frx_lex_starts(l, at, "<!ENTITY")) { l->pos += 8; if (!frx_dtd_entity_decl(l, at)) return 0; continue; } 6698 1576 8274
if (frx_lex_starts(l, at, "<!ENTITY")) { l->pos += 8; if (!frx_dtd_entity_decl(l, at)) return 0; continue; } 285 6413 6698
999 1576 ▶ if (frx_lex_starts(l, at, "<!NOTATION")) { l->pos += 10; if (!frx_dtd_notation_decl(l, at)) return 0; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (frx_lex_starts(l, at, "<!NOTATION")) { l->pos += 10; if (!frx_dtd_notation_decl(l, at)) return 0; continue; } 919 657 1576
if (frx_lex_starts(l, at, "<!NOTATION")) { l->pos += 10; if (!frx_dtd_notation_decl(l, at)) return 0; continue; } 62 857 919
1000 657 ▶ if (frx_lex_starts(l, at, "<![")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, at, "<![")) { 359 298 657
1001 359 ▶ if (!l->ext_decl)
Branch: 1 branch
BranchTrueFalseTotal
if (!l->ext_decl) 3 356 359
1002 3 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, "a conditional section is allowed only in the external subset");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, "a conditional section is allowed only in the external subset"); 0 3 3
1003 356 l->pos += 3;
1004 356 ▶ if (!frx_dtd_conditional(l, at)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_conditional(l, at)) return 0; 24 332 356
1005 332 continue;
1006 }
1007 298 ▶ if (l->ext_decl && frx_lex_starts(l, at, "]]>")) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->ext_decl && frx_lex_starts(l, at, "]]>")) { 155 143 298
if (l->ext_decl && frx_lex_starts(l, at, "]]>")) { 145 10 155
1008 145 ▶ if (!l->cond_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (!l->cond_depth) 2 143 145
1009 2 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, "]]> outside a conditional section");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, "]]> outside a conditional section"); 2 0 2
1010 143 l->cond_depth--;
1011 143 ▶ if (frx_dtd_cur_seq(l) != l->cond_frame[l->cond_depth]) frx_dtd_pe_nest(l, at);
Branch: 1 branch
BranchTrueFalseTotal
if (frx_dtd_cur_seq(l) != l->cond_frame[l->cond_depth]) frx_dtd_pe_nest(l, at); 0 143 143
1012 143 l->pos += 3;
1013 143 continue;
1014 }
1015 153 ▶ if (c == ']')
Branch: 1 branch
BranchTrueFalseTotal
if (c == ']') 5 148 153
1016 5 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, external ? "] is not allowed in the external subset"
Branch: 2 branches
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, external ? "] is not allowed in the external subset" 4 1 5
return FRX_LEX_FAIL(l, FRX_E_DTD, at, external ? "] is not allowed in the external subset" 4 1 5
1017 : "a parameter entity's text must not close the internal subset");
1018 148 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, "expected a markup declaration in the internal subset");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, "expected a markup declaration in the internal subset"); 6 142 148
1019 }
1020 5227 ▶ if (external && l->cond_depth)
Branch: 2 branches
BranchTrueFalseTotal
if (external && l->cond_depth) 967 4260 5227
if (external && l->cond_depth) 3 964 967
1021 3 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, open, "unterminated INCLUDE section");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, open, "unterminated INCLUDE section"); 3 0 3
1022 5224 l->in_subset = 0;
1023 5224 return 1;
1024 }
1025  
1026 /* the external subset: fetched through the resolver, read as external
1027 * markup after the internal subset. sys_at is the system literal's
1028 * frame-relative offset, which every refusal inside the subset reports. */
1029 static int
1030 1048 frx_dtd_load_external(frx_lex *l, frx_doctype *dt, size_t sys_at)
1031 {
1032 frx_str text, uri;
1033 1048 size_t close = 0;
1034 1048 int version11 = 0;
1035 1048 ▶ if (!l->resolve)
Branch: 1 branch
BranchTrueFalseTotal
if (!l->resolve) 4 1044 1048
1036 4 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, sys_at,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, sys_at, 0 4 4
1037 "the external subset cannot be read without a resolver; the resolve option arrives with external entities");
1038 1044 ▶ if (!frx_entity_fetch(l, FRX_REF_SUBSET, &dt->public_id, &dt->system_id, &l->cur_base, sys_at,
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_entity_fetch(l, FRX_REF_SUBSET, &dt->public_id, &dt->system_id, &l->cur_base, sys_at, 19 1025 1044
1039 19 &text, &uri, &version11)) return 0;
1040 1025 ▶ if (version11 && !l->version11)
Branch: 2 branches
BranchTrueFalseTotal
if (version11 && !l->version11) 10 1015 1025
if (version11 && !l->version11) 2 8 10
1041 2 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, sys_at,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, sys_at, 0 2 2
1042 "an XML 1.1 external subset cannot be used by an XML 1.0 document (section 4.3.4)");
1043 1023 ▶ if (!frx_entity_push_text(l, NULL, &text, sys_at, 1, FRX_LEX_ABS(l, sys_at), &uri, 1)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_entity_push_text(l, NULL, &text, sys_at, 1, FRX_LEX_ABS(l, sys_at), &uri, 1)) return 0; 0 1023 1023
if (!frx_entity_push_text(l, NULL, &text, sys_at, 1, FRX_LEX_ABS(l, sys_at), &uri, 1)) return 0; 0 1023 1023
1044 1023 ▶ if (!frx_dtd_subset(l, 0, &close, 1)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_subset(l, 0, &close, 1)) return 0; 59 964 1023
1045 964 ▶ if (!frx_entity_pop(l)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_entity_pop(l)) return 0; 0 964 964
1046 964 l->dtd->external_read = 1;
1047 964 return 1;
1048 }
1049  
1050 /* <!DOCTYPE S Name (S ExternalID)? S? ([ intSubset ] S?)? >; pos is at
1051 * the <. One per document, before the root: no frame can be open here
1052 * because entities exist only after it, so every offset is a document
1053 * offset. The external subset, when there is one, is read after the
1054 * internal subset and before the >. */
1055 static int frx_dtd_doctype_body(frx_lex *l, frx_tok *t);
1056  
1057 /* the DTD's strings outlive any record a reader releases, so they go to
1058 * the persistent arena whatever the token arena is at the time */
1059 static int
1060 5736 frx_dtd_doctype(frx_lex *l, frx_tok *t)
1061 {
1062 5736 frx_arena *saved = l->tok_arena;
1063 int ok;
1064 5736 l->tok_arena = l->arena;
1065 5736 ok = frx_dtd_doctype_body(l, t);
1066 5736 l->tok_arena = saved;
1067 5736 return ok;
1068 }
1069  
1070 static int
1071 5736 frx_dtd_doctype_body(frx_lex *l, frx_tok *t)
1072 {
1073 5736 size_t at = l->pos;
1074 5736 size_t sys_at = 0;
1075 frx_doctype *dt;
1076 5736 ▶ if (l->dtd || l->n_frames)
Branch: 2 branches
BranchTrueFalseTotal
if (l->dtd || l->n_frames) 5735 1 5736
if (l->dtd || l->n_frames) 0 5735 5735
1077 1 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, "a document has one DOCTYPE, before the root element");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, "a document has one DOCTYPE, before the root element"); 0 1 1
1078 5735 l->pos += 9;
1079 5735 ▶ if (!frx_lex_skip_s(l))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_skip_s(l)) 2 5733 5735
1080 2 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace after DOCTYPE");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, l->pos, "expected whitespace after DOCTYPE"); 0 2 2
1081 5733 dt = (frx_doctype *)frx_arena_alloc(l->arena, sizeof *dt);
1082 5733 ▶ if (!dt) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!dt) return frx_lex_nomem(l); 0 5733 5733
1083 5733 memset(dt, 0, sizeof *dt);
1084 5733 dt->public_id = dt->system_id = dt->subset = FRX_EMPTY_STR;
1085 5733 dt->offset = at;
1086 5733 ▶ if (!frx_lex_name(l, 1, &dt->name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 1, &dt->name)) return 0; 68 5665 5733
1087 5665 l->dtd = frx_dtd_new(l->arena);
1088 5665 ▶ if (!l->dtd) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!l->dtd) return frx_lex_nomem(l); 0 5665 5665
1089 5665 l->decl_frame0 = 0;
1090 5665 frx_lex_skip_s(l);
1091 5665 ▶ if (l->pos < l->len && (l->in[l->pos] == 'S' || l->in[l->pos] == 'P')) {
Branch: 3 branches
BranchTrueFalseTotal
if (l->pos < l->len && (l->in[l->pos] == 'S' || l->in[l->pos] == 'P')) { 5663 2 5665
if (l->pos < l->len && (l->in[l->pos] == 'S' || l->in[l->pos] == 'P')) { 4177 1486 5663
if (l->pos < l->len && (l->in[l->pos] == 'S' || l->in[l->pos] == 'P')) { 44 4133 4177
1092 1530 ▶ if (!frx_dtd_external_id(l, &dt->public_id, &dt->system_id, 0, &sys_at)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_external_id(l, &dt->public_id, &dt->system_id, 0, &sys_at)) return 0; 29 1501 1530
1093 1501 l->dtd->external_subset = 1;
1094 1501 frx_lex_skip_s(l);
1095 }
1096 5636 ▶ if (l->pos < l->len && l->in[l->pos] == '[') {
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos < l->len && l->in[l->pos] == '[') { 5632 4 5636
if (l->pos < l->len && l->in[l->pos] == '[') { 5125 507 5632
1097 5125 size_t open = l->pos, close = 0;
1098 5125 l->pos++;
1099 5125 ▶ if (!frx_dtd_subset(l, open, &close, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_subset(l, open, &close, 0)) return 0; 865 4260 5125
1100 4260 dt->subset = frx_arena_strndup(l->arena, (const char *)(l->in + open + 1), close - open - 1);
1101 4260 ▶ if (!dt->subset.p) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!dt->subset.p) return frx_lex_nomem(l); 0 4260 4260
1102 4260 dt->has_subset = 1;
1103 4260 frx_lex_skip_s(l);
1104 }
1105 4771 ▶ if (l->dtd->external_subset) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->dtd->external_subset) { 1048 3723 4771
1106 1048 l->doctype = dt;
1107 1048 ▶ if (!frx_dtd_load_external(l, dt, sys_at)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_dtd_load_external(l, dt, sys_at)) return 0; 84 964 1048
1108 }
1109 4687 ▶ if (l->pos >= l->len || l->in[l->pos] != '>')
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos >= l->len || l->in[l->pos] != '>') 4681 6 4687
if (l->pos >= l->len || l->in[l->pos] != '>') 80 4601 4681
1110 86 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at, "unterminated DOCTYPE");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, "unterminated DOCTYPE"); 0 86 86
1111 4601 l->pos++;
1112 4601 l->doctype = dt;
1113 4601 t->kind = FRX_TOK_DOCTYPE;
1114 4601 t->name = dt->name;
1115 4601 return 1;
1116 }
1117  
1118 #endif /* FRX_DTD_H */
« include/frx/frx_xpath.h ↑ Summary include/frx/frx_write.h »