include/frx/frx_lex.h

filter: Statementstmt 97.3% 556 / 571 Branchbran 83.6% 739 / 884 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.0% 1295 / 1455 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/document.xs ↑ Summary include/frx/frx_validate.h »
1 #ifndef FRX_LEX_H
2 #define FRX_LEX_H
3  
4 /* Bytes in, tokens out, every refusal with an offset.
5 *
6 * This is the whole of the attacker-facing surface: everything after it
7 * operates on validated names and decoded strings already in the arena. A
8 * pull tokeniser rather than a lexer folded into the parser, so that every
9 * refusal is testable through the _lex accessor without a tree, and so that
10 * the every-offset test can hit the lexer alone.
11 *
12 * What it accepts: XML 1.0 with the five predefined entities and numeric
13 * character references, in UTF-8. What it refuses, each with the offset it
14 * was found at: any DOCTYPE (and any <! that is not a comment or CDATA -
15 * one rule removes entity expansion, external entities, the billion laughs
16 * and XXE as a class, with no option to allow them); UTF-16 and UTF-32 by
17 * BOM or by first pair; a declared encoding other than utf-8; version 1.1;
18 * a <?xml declaration anywhere but offset 0; a named reference other than
19 * the five; a reference to a non-Char; ]]> in content; a literal < in an
20 * attribute value; -- inside a comment; a PI whose target is xml in any
21 * case; a Name that is not a QName (at most one colon, both parts
22 * non-empty); anything not well-formed.
23 *
24 * Normalisation, in this order and with this word: line ends first (XML
25 * 1.0 section 2.11, CRLF and CR become LF, in text and in attribute
26 * values), then attribute values (section 3.3.3: with no DTD every
27 * attribute is CDATA, so each LITERAL TAB, LF or CR becomes a space). A
28 * character reference to one of those - &#x9; &#xA; &#xD; - stays the
29 * character. Canonical XML escapes TAB, LF and CR in attribute values, and
30 * the only way such a character reaches the serialiser is through a
31 * reference, because the literal form was already a space. A lexer that
32 * normalises after expanding references produces canonical bytes that
33 * differ from every other implementation's.
34 *
35 * Every string a token carries is in the arena, NUL-terminated. CDATA is a
36 * TEXT token with the cdata flag, so the parser can merge it with its
37 * neighbours; there is no CDATA node kind. The attribute list of a START or
38 * EMPTY token is borrowed from the lexer's scratch and valid until the next
39 * frx_lex_next.
40 *
41 * THE FULL PROFILE reads a DOCTYPE and expands the internal subset's
42 * entities. An entity is an input frame, not a string substitution: the
43 * lexer's in/len/pos describe the frame being read, a reference saves them
44 * and points them at the replacement text, and reaching the end of that
45 * text restores them. Every token-level function reads through in/len/pos
46 * unchanged; the frame boundary is the one place the rules of section 4.4
47 * are enforced (an element, comment, PI, CDATA section or attribute value
48 * that opened inside a frame must close inside it; an end tag inside a
49 * frame must close an element opened inside it), and that is the whole of
50 * the well-formedness of a parsed entity. Offsets: `base` is the document
51 * offset the current frame's byte 0 corresponds to, 0 for the document
52 * itself and the entity literal's first byte otherwise, so a refusal
53 * inside an entity points into its declaration; FRX_LEX_FAIL takes a
54 * frame-relative offset and adds base, FRX_LEX_FAIL_ABS takes a document
55 * offset. Attribute types come from the DTD before an element is lexed
56 * (the DTD is complete before the root starts), which is what lets the
57 * 3.3.3 normalisation by type happen here, in the one place the literal
58 * whitespace rule already lives. The declaration parser is frx_dtd.h and
59 * the frame stack's push, pop and budgets are frx_entity.h; both need this
60 * file's primitives, so they follow it and are declared below.
61 *
62 * Needs frx_err.h, frx_arena.h, frx_buf.h, frx_utf8.h, frx_enc.h,
63 * frx_tree.h. */
64  
65 typedef struct frx_eldecl frx_eldecl; /* frx_dtd.h; frx_dtd and frx_doctype are frx_tree.h's */
66 typedef struct frx_attdef frx_attdef; /* frx_dtd.h */
67 typedef struct frx_entity frx_entity; /* frx_dtd.h */
68  
69 /* what an entity reference saves: the frame it interrupted */
70 typedef struct frx_frame_in {
71 const unsigned char *in; /* the interrupted frame's bytes */
72 size_t len, pos, base;
73 frx_str cur_base; /* its base URI */
74 int flat; /* its offsets were flat */
75 int ext_decl; /* it allowed PE references inside declarations */
76 int in_dtd; /* it was inside a parameter entity or the external subset */
77 frx_entity *entity; /* the entity whose text is now current; NULL for fetched text with none */
78 int depth_at_push; /* open elements when pushed; must match at pop */
79 int seq; /* which replacement text this is: unique for the parse, so
80 * that two references to different entities at one depth
81 * are told apart (VC: Proper Declaration/PE Nesting) */
82 size_t ref_offset; /* document offset of the reference, for messages */
83 } frx_frame_in;
84  
85 /* the per-parse cache of fetched text, keyed by resolved identifier */
86 typedef struct frx_fetch_entry {
87 frx_str uri;
88 frx_str text; /* UTF-8, the text declaration stripped, in the arena */
89 int version11;
90 struct frx_fetch_entry *next;
91 } frx_fetch_entry;
92  
93 #define FRX_DEFAULT_MAX_FETCHES 32
94  
95 #define FRX_DEFAULT_MAX_ENTITY_DEPTH 16
96 #define FRX_DEFAULT_MAX_EXPANSION_BYTES (16u * 1024u * 1024u)
97 #define FRX_DEFAULT_MAX_EXPANSION_RATIO 100
98  
99 enum {
100 FRX_TOK_EOF = 0,
101 FRX_TOK_START, /* <name attrs> */
102 FRX_TOK_END, /* </name> */
103 FRX_TOK_EMPTY, /* <name attrs/> */
104 FRX_TOK_TEXT, /* character data, or CDATA with cdata set */
105 FRX_TOK_COMMENT, /* <!-- value --> */
106 FRX_TOK_PI, /* <?name value?> */
107 FRX_TOK_DOCTYPE, /* full: <!DOCTYPE ...>, its subset already read into l->dtd */
108 FRX_TOK_MORE /* the input ended inside a token and eof is not set: feed more and call again */
109 };
110  
111 #define FRX_DEFAULT_MAX_TOKEN_BYTES (16u * 1024u * 1024u)
112  
113 typedef struct frx_lex_attr {
114 frx_str name;
115 frx_str value;
116 size_t offset; /* document offset; a default's is its declaration's */
117 int defaulted; /* full: from an ATTLIST default, not written */
118 } frx_lex_attr;
119  
120 typedef struct frx_tok {
121 int kind;
122 size_t offset;
123 frx_str name; /* START/END/EMPTY: the qname; PI: the target */
124 frx_str value; /* TEXT, COMMENT: the body; PI: the data */
125 int cdata; /* TEXT from a CDATA section */
126 int text_flags; /* TEXT: FRX_TEXT_REF_* below */
127 const frx_lex_attr *attrs; /* START/EMPTY; borrowed until the next call */
128 int n_attrs;
129 frx_str entity; /* the entity whose text the token came from; "" for the document */
130 } frx_tok;
131  
132 typedef struct frx_lex {
133 const unsigned char *in;
134 size_t len;
135 size_t pos;
136 frx_arena *arena;
137 frx_err err;
138 int failed;
139 int profile; /* FRX_PROFILE_*; read here and in frx_parse.h, nowhere else */
140 int version11; /* full: the declaration said 1.1; 2.11 line ends and RestrictedChar apply */
141 frx_str version; /* the declared version string; empty when there was no declaration */
142 int enc_kind; /* full: what frx_enc.h detected, FRX_ENC_* */
143 int enc_bom; /* full: a byte order mark was present */
144 int enc_override; /* full: the caller named the encoding; the declaration is not judged */
145 int standalone; /* the declaration said standalone="yes" */
146 frx_lex_attr *attrs; /* scratch, grows with realloc */
147 int n_attrs;
148 int cap_attrs;
149 frx_buf tmp; /* decoding scratch */
150  
151 /* the full profile: the DTD and the entity frames */
152 frx_dtd *dtd; /* what the DOCTYPE declared; NULL without one */
153 frx_doctype *doctype; /* the DOCTYPE's names and subset text; NULL without one */
154 frx_frame_in *frames; /* the interrupted frames, malloc'd; the document is below them all */
155 int n_frames;
156 int cap_frames;
157 size_t base; /* document offset of the current frame's byte 0 */
158 size_t tok_start; /* frame-relative start of the token being lexed */
159 int tag_depth; /* START tokens minus END tokens so far */
160 int in_subset; /* lexing the internal subset */
161 size_t doc_len; /* the document's bytes, for the ratio budget */
162 size_t pushed; /* replacement bytes pushed so far, every frame counted */
163 size_t max_expansion_bytes;
164 int max_entity_depth;
165 int max_expansion_ratio;
166 int *cm_stack; /* content model group stack, malloc'd; frx_dtd.h */
167 int *cm_frame; /* and the entity frame each of those groups opened in */
168 int cm_cap;
169 int *cond_frame; /* the frame each open conditional section opened in */
170 int cond_cap;
171 int frame_seq; /* the last serial handed to a frame */
172 int decl_seq; /* the replacement text the current declaration began in */
173 int decl_frame0; /* frames open when the current declaration began */
174 int cond_depth; /* open INCLUDE sections of the external subset */
175 int last_collapse_changed; /* the last typed attribute value was changed by 3.3.3 */
176  
177 /* the full profile: fetching. A fetched frame is "flat":
178 * its bytes are not in the document, so every offset inside it
179 * reports the reference that pulled it in. */
180 frx_resolve_fn resolve; /* NULL: every external reference is refused */
181 void *resolve_ud;
182 int max_fetches;
183 int n_fetches;
184 size_t max_bytes; /* applied to each fetched text */
185 int validate; /* the standalone validity constraints are applied */
186 frx_str cur_base; /* base URI of the current frame; "" when none */
187 int flat; /* current frame is fetched text */
188 int ext_decl; /* current frame is external markup: PE references inside declarations */
189 int in_dtd; /* current frame is inside a parameter entity or the external subset */
190 frx_fetch_entry *fetches; /* the cache, in the arena */
191  
192 /* streaming: with eof clear, the document's bytes may grow.
193 * A token the input ends inside is not an error but FRX_TOK_MORE: the
194 * lexer rewinds to its start and the caller feeds more and calls
195 * again. Token strings go to tok_arena, which a reader may release
196 * between records; the DTD and the entities stay in arena. */
197 /* WFC: Entity Declared exempts an undeclared general entity when the
198 * processor may not have seen every declaration - there is an external
199 * subset, or the internal subset made a parameter entity reference -
200 * and the document is not standalone. The reference is then not a
201 * well-formedness error but a validity error, so it produces nothing
202 * and its offset is kept here for the validate pass. */
203 size_t *undecl; /* offsets, in document order; malloc'd */
204 int n_undecl, cap_undecl;
205  
206 int eof; /* the input is complete */
207 int need_more; /* set by a token function that ran out of input */
208 int prolog_done;
209 size_t prolog_max_bytes; /* the max_bytes the prolog applies */
210 size_t max_token_bytes; /* a token longer than this is refused, not retried */
211 frx_arena *tok_arena; /* where token strings go; arena unless a reader says otherwise */
212 } frx_lex;
213  
214 /* a frame-relative offset as a document offset */
215 #define FRX_LEX_ABS(l, off) ((l)->flat ? (l)->base : (l)->base + (off))
216  
217 #define FRX_LEX_FAIL(l, c, off, what) \
218 (frx_err_set(&(l)->err, (c), FRX_LEX_ABS((l), (off)), (what)), (l)->failed = 1, 0)
219 #define FRX_LEX_FAIL_ABS(l, c, off, what) \
220 (frx_err_set(&(l)->err, (c), (off), (what)), (l)->failed = 1, 0)
221 /* the input ended inside a token: a refusal when it is complete, a
222 * request for more bytes when the document itself may still grow */
223 #define FRX_LEX_NEED(l, c, off, what) \
224 ((!(l)->eof && (l)->n_frames == 0) ? ((l)->need_more = 1, 0) : FRX_LEX_FAIL((l), (c), (off), (what)))
225  
226 /* defined in frx_dtd.h and frx_entity.h, which need the primitives here */
227 static int frx_dtd_doctype(frx_lex *l, frx_tok *t);
228 static const frx_eldecl *frx_dtd_element(const frx_dtd *dtd, const frx_str *name);
229 static const frx_attdef *frx_dtd_attdef(const frx_eldecl *ed, const frx_str *name);
230 static int frx_dtd_attdef_collapses(const frx_attdef *ad);
231 static int frx_dtd_attdef_external(const frx_attdef *ad);
232 static int frx_dtd_apply_defaults(frx_lex *l, const frx_eldecl *ed);
233 static int frx_entity_reference(frx_lex *l, size_t start, int in_attr);
234 static int frx_entity_pop(frx_lex *l);
235 static const frx_str *frx_entity_name_of(const frx_entity *e);
236  
237 static int
238 0 frx_lex_nomem(frx_lex *l)
239 {
240 0 ▶ return FRX_LEX_FAIL(l, FRX_E_NOMEM, l->pos, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_NOMEM, l->pos, "out of memory"); 0 0 0
241 }
242  
243 /* n bytes of s into the token arena; 0 on failure */
244 static int
245 19833257 frx_lex_intern(frx_lex *l, const unsigned char *s, size_t n, frx_str *out)
246 {
247 19833257 *out = frx_arena_strndup(l->tok_arena, (const char *)s, n);
248 19833257 ▶ return out->p ? 1 : frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
return out->p ? 1 : frx_lex_nomem(l); 0 19833257 19833257
249 }
250  
251 static int
252 9108038 frx_lex_intern_buf(frx_lex *l, frx_str *out)
253 {
254 9108038 ▶ if (l->tmp.failed) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (l->tmp.failed) return frx_lex_nomem(l); 0 9108038 9108038
255 9108038 ▶ return frx_lex_intern(l, (const unsigned char *)(l->tmp.p ? l->tmp.p : ""),
Branch: 1 branch
BranchTrueFalseTotal
return frx_lex_intern(l, (const unsigned char *)(l->tmp.p ? l->tmp.p : ""), 9108030 8 9108038
256 l->tmp.len, out);
257 }
258  
259 static int
260 31894514 frx_lex_starts(const frx_lex *l, size_t at, const char *lit)
261 {
262 31894514 size_t n = strlen(lit);
263 31894514 ▶ return l->len - at >= n && memcmp(l->in + at, lit, n) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return l->len - at >= n && memcmp(l->in + at, lit, n) == 0; 31486298 408216 31894514
return l->len - at >= n && memcmp(l->in + at, lit, n) == 0; 3173985 28312313 31486298
264 }
265  
266 /* the input ends inside lit: what is there is a proper prefix of it */
267 static int
268 4916 frx_lex_short(const frx_lex *l, size_t at, const char *lit)
269 {
270 4916 size_t n = strlen(lit), have = l->len - at;
271 4916 ▶ return have < n && memcmp(l->in + at, lit, have) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return have < n && memcmp(l->in + at, lit, have) == 0; 382 4534 4916
return have < n && memcmp(l->in + at, lit, have) == 0; 168 214 382
272 }
273  
274 /* XML 1.1 section 2.11: is a NEL (C2 85) or an LS (E2 80 A8) at pos?
275 * Returns its byte length, 0 when neither. */
276 static size_t
277 28523 frx_lex_nel_or_ls(const frx_lex *l, size_t pos)
278 {
279 28523 ▶ if (l->len - pos >= 2 && l->in[pos] == 0xC2 && l->in[pos + 1] == 0x85) return 2;
Branch: 3 branches
BranchTrueFalseTotal
if (l->len - pos >= 2 && l->in[pos] == 0xC2 && l->in[pos + 1] == 0x85) return 2; 28384 139 28523
if (l->len - pos >= 2 && l->in[pos] == 0xC2 && l->in[pos + 1] == 0x85) return 2; 76 28308 28384
if (l->len - pos >= 2 && l->in[pos] == 0xC2 && l->in[pos + 1] == 0x85) return 2; 21 55 76
280 28502 ▶ if (l->len - pos >= 3 && l->in[pos] == 0xE2 && l->in[pos + 1] == 0x80 && l->in[pos + 2] == 0xA8) return 3;
Branch: 4 branches
BranchTrueFalseTotal
if (l->len - pos >= 3 && l->in[pos] == 0xE2 && l->in[pos + 1] == 0x80 && l->in[pos + 2] == 0xA8) return 3; 28304 198 28502
if (l->len - pos >= 3 && l->in[pos] == 0xE2 && l->in[pos + 1] == 0x80 && l->in[pos + 2] == 0xA8) return 3; 132 28172 28304
if (l->len - pos >= 3 && l->in[pos] == 0xE2 && l->in[pos + 1] == 0x80 && l->in[pos + 2] == 0xA8) return 3; 52 80 132
if (l->len - pos >= 3 && l->in[pos] == 0xE2 && l->in[pos + 1] == 0x80 && l->in[pos + 2] == 0xA8) return 3; 17 35 52
281 28485 return 0;
282 }
283  
284 /* S; under XML 1.1 a NEL or LS is a line end wherever it stands (section
285 * 2.11 normalises before parsing), so between attributes and between
286 * declarations it is whitespace too */
287 static size_t
288 12818586 frx_lex_skip_s(frx_lex *l)
289 {
290 12818586 size_t start = l->pos;
291 2328298 for (;;) {
292 size_t n;
293 15146884 ▶ if (l->pos >= l->len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) break; 8384 15138500 15146884
294 15138500 ▶ if (frx_is_s(l->in[l->pos])) { l->pos++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_is_s(l->in[l->pos])) { l->pos++; continue; } 2328293 12810207 15138500
295 12810207 ▶ if (l->version11 && (n = frx_lex_nel_or_ls(l, l->pos)) != 0) { l->pos += n; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (l->version11 && (n = frx_lex_nel_or_ls(l, l->pos)) != 0) { l->pos += n; continue; } 6183 12804024 12810207
if (l->version11 && (n = frx_lex_nel_or_ls(l, l->pos)) != 0) { l->pos += n; continue; } 5 6178 6183
296 12810202 break;
297 }
298 12818586 return l->pos - start;
299 }
300  
301 /* does the input end inside what may be a UTF-8 sequence, with more to come? */
302 static int
303 243 frx_lex_utf8_cut(const frx_lex *l)
304 {
305 243 unsigned char c = l->in[l->pos];
306 243 ▶ return !l->eof && !l->n_frames && l->len - l->pos < 4 && c >= 0xC2 && c <= 0xF4;
Branch: 5 branches
BranchTrueFalseTotal
return !l->eof && !l->n_frames && l->len - l->pos < 4 && c >= 0xC2 && c <= 0xF4; 8 235 243
return !l->eof && !l->n_frames && l->len - l->pos < 4 && c >= 0xC2 && c <= 0xF4; 8 0 8
return !l->eof && !l->n_frames && l->len - l->pos < 4 && c >= 0xC2 && c <= 0xF4; 8 0 8
return !l->eof && !l->n_frames && l->len - l->pos < 4 && c >= 0xC2 && c <= 0xF4; 8 0 8
return !l->eof && !l->n_frames && l->len - l->pos < 4 && c >= 0xC2 && c <= 0xF4; 8 0 8
307 }
308  
309 /* One character of content at pos: validated as UTF-8 and as Char,
310 * appended to tmp with line ends normalised. Returns 0 on refusal.
311 *
312 * Under XML 1.1 (full profile, a 1.1 declaration) section 2.11 also turns
313 * NEL, CR NEL and LS into LF, and production [1] refuses a literal
314 * RestrictedChar. The 1.0 path is 0.01's, untouched.
315 *
316 * Inside an entity frame the LINE ENDS do not apply: the entity's literal
317 * was normalised when it was declared, so a CR, NEL or LS in its
318 * replacement text came from a character reference and is the character,
319 * as it would be written in content by reference. Section 3.3.3's table
320 * (two entities of &#xD; and &#xA; giving two spaces, not one) is the
321 * case that tells the two apart.
322 *
323 * RESTRICTED CHARACTERS APPLY IN AN EXTERNAL FRAME AND NOT AN INTERNAL
324 * ONE, which is the grammar and not a guess. Two productions subtract
325 * them, and only two:
326 *
327 * [1] document ::= ( prolog element Misc* ) - ( Char* RestrictedChar Char* )
328 * [78] extParsedEnt ::= ( TextDecl? content ) - ( Char* RestrictedChar Char* )
329 *
330 * so the document entity and an external parsed entity are subject to it
331 * and an internal entity's replacement text is subject to nothing: a
332 * character reference in an entity's literal, which 4.4.5 Included in
333 * Literal expanded when the entity was declared, may put a restricted
334 * character into content. eduni rmt-054 is that case and the suite marks
335 * it valid, saying in its own comment that the CR grammar made it illegal
336 * and "this is probably not intended". IBM's ibm02n13 is the same
337 * construct marked not-wf; it was written against that draft and the
338 * Recommendation contradicts it. t/xmlconf/expected-fail.txt carries it.
339 *
340 * The version that decides is the DOCUMENT's, never the frame's, because
341 * 4.3.4 says an XML 1.1 document may invoke XML 1.0 external entities
342 * and "in such a case the rules of XML 1.1 are applied to the entire
343 * document". So a version='1.0' external entity, whose own version admits
344 * #x7F to #x9F as ordinary characters, may not carry one into a 1.1
345 * document. l->flat is the discriminator: it is set from the entity's own
346 * external-ness as each frame is pushed.
347 *
348 * The line ends do not conflict with any of this: none of CR, NEL and LS
349 * is a RestrictedChar. */
350 static int
351 159623678 frx_lex_char(frx_lex *l)
352 {
353 159623678 unsigned char c = l->in[l->pos];
354 159623678 ▶ if (l->n_frames) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->n_frames) { 78316585 81307093 159623678
355 78316585 ▶ if (c < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { 78305083 11502 78316585
356 78305083 ▶ if (c == 0) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "control character is not allowed");
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "control character is not allowed"); 0 78305083 78305083
if (c == 0) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "control character is not allowed"); 0 0 0
357 78305083 ▶ if (l->version11 && l->flat && frx_is_restricted11(c))
Branch: 3 branches
BranchTrueFalseTotal
if (l->version11 && l->flat && frx_is_restricted11(c)) 369 78304714 78305083
if (l->version11 && l->flat && frx_is_restricted11(c)) 329 40 369
if (l->version11 && l->flat && frx_is_restricted11(c)) 6 323 329
358 6 ▶ return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "a restricted character may appear only as a character reference in XML 1.1");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "a restricted character may appear only as a character reference in XML 1.1"); 6 0 6
359 78305077 frx_buf_append_ch(&l->tmp, (char)c);
360 78305077 l->pos++;
361 78305077 return 1;
362 } else {
363 unsigned long cp;
364 11502 size_t n = frx_utf8_decode(l->in + l->pos, l->len - l->pos, &cp);
365 11502 ▶ 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 11502 11502
if (!n) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); 0 0 0
366 11502 ▶ if (l->version11 && l->flat && frx_is_restricted11(cp))
Branch: 3 branches
BranchTrueFalseTotal
if (l->version11 && l->flat && frx_is_restricted11(cp)) 18 11484 11502
if (l->version11 && l->flat && frx_is_restricted11(cp)) 13 5 18
if (l->version11 && l->flat && frx_is_restricted11(cp)) 13 0 13
367 13 ▶ return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "a restricted character may appear only as a character reference in XML 1.1");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "a restricted character may appear only as a character reference in XML 1.1"); 13 0 13
368 11489 frx_buf_append_n(&l->tmp, (const char *)(l->in + l->pos), n);
369 11489 l->pos += n;
370 11489 return 1;
371 }
372 }
373 81307093 ▶ if (l->version11) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->version11) { 22073 81285020 81307093
374 size_t n;
375 22073 ▶ if (c == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\r') { 198 21875 22073
376 198 frx_buf_append_ch(&l->tmp, '\n');
377 198 l->pos++;
378 198 ▶ if (l->pos < l->len && l->in[l->pos] == '\n') l->pos++;
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos < l->len && l->in[l->pos] == '\n') l->pos++; 198 0 198
if (l->pos < l->len && l->in[l->pos] == '\n') l->pos++; 190 8 198
379 8 ▶ else if ((n = frx_lex_nel_or_ls(l, l->pos)) == 2) l->pos += 2; /* CR NEL */
Branch: 1 branch
BranchTrueFalseTotal
else if ((n = frx_lex_nel_or_ls(l, l->pos)) == 2) l->pos += 2; /* CR NEL */ 4 4 8
380 198 return 1;
381 }
382 21875 ▶ if ((n = frx_lex_nel_or_ls(l, l->pos)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((n = frx_lex_nel_or_ls(l, l->pos)) != 0) { 19 21856 21875
383 19 frx_buf_append_ch(&l->tmp, '\n');
384 19 l->pos += n;
385 19 return 1;
386 }
387 21856 ▶ if (c < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { 21509 347 21856
388 21509 ▶ if (frx_is_restricted11(c))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_is_restricted11(c)) 43 21466 21509
389 43 ▶ return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "a restricted character may appear only as a character reference in XML 1.1");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "a restricted character may appear only as a character reference in XML 1.1"); 0 43 43
390 21466 ▶ if (c == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c == 0) 2 21464 21466
391 2 ▶ return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "control character is not allowed");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "control character is not allowed"); 0 2 2
392 21464 frx_buf_append_ch(&l->tmp, (char)c);
393 21464 l->pos++;
394 21464 return 1;
395 } else {
396 unsigned long cp;
397 347 n = frx_utf8_decode(l->in + l->pos, l->len - l->pos, &cp);
398 347 ▶ if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); }
Branch: 3 branches
BranchTrueFalseTotal
if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); } 1 346 347
if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); } 0 1 1
if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); } 0 1 1
399 346 ▶ if (frx_is_restricted11(cp))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_is_restricted11(cp)) 37 309 346
400 37 ▶ return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "a restricted character may appear only as a character reference in XML 1.1");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "a restricted character may appear only as a character reference in XML 1.1"); 0 37 37
401 309 ▶ if (!frx_is_char11(cp)) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not an XML character");
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_is_char11(cp)) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not an XML character"); 2 307 309
if (!frx_is_char11(cp)) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not an XML character"); 0 2 2
402 307 frx_buf_append_n(&l->tmp, (const char *)(l->in + l->pos), n);
403 307 l->pos += n;
404 307 return 1;
405 }
406 }
407 81285020 ▶ if (c < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { 81201174 83846 81285020
408 81201174 ▶ if (c == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\r') { 10472 81190702 81201174
409 10472 frx_buf_append_ch(&l->tmp, '\n');
410 10472 l->pos++;
411 10472 ▶ if (l->pos < l->len && l->in[l->pos] == '\n') l->pos++;
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos < l->len && l->in[l->pos] == '\n') l->pos++; 10469 3 10472
if (l->pos < l->len && l->in[l->pos] == '\n') l->pos++; 10436 33 10469
412 10472 return 1;
413 }
414 81190702 ▶ if (c < 0x20 && c != '\t' && c != '\n')
Branch: 3 branches
BranchTrueFalseTotal
if (c < 0x20 && c != '\t' && c != '\n') 968840 80221862 81190702
if (c < 0x20 && c != '\t' && c != '\n') 967961 879 968840
if (c < 0x20 && c != '\t' && c != '\n') 95 967866 967961
415 95 ▶ return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "control character is not allowed");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "control character is not allowed"); 0 95 95
416 81190607 frx_buf_append_ch(&l->tmp, (char)c);
417 81190607 l->pos++;
418 81190607 return 1;
419 } else {
420 unsigned long cp;
421 83846 size_t n = frx_utf8_decode(l->in + l->pos, l->len - l->pos, &cp);
422 83846 ▶ if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); }
Branch: 3 branches
BranchTrueFalseTotal
if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); } 145 83701 83846
if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); } 8 137 145
if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); } 0 137 137
423 83701 ▶ if (!frx_is_char(cp)) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not an XML character");
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_is_char(cp)) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not an XML character"); 13 83688 83701
if (!frx_is_char(cp)) return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not an XML character"); 0 13 13
424 83688 frx_buf_append_n(&l->tmp, (const char *)(l->in + l->pos), n);
425 83688 l->pos += n;
426 83688 return 1;
427 }
428 }
429  
430 /* A reference at pos (the &): the five entities, &#N; and &#xN;, and under
431 * the full profile a declared general entity, whose replacement text
432 * becomes the current frame (frx_entity.h) for the caller to read on
433 * through. A character's bytes go to tmp as UTF-8. Returns 0 on refusal.
434 * in_attr says the reference stands in an attribute value, where an
435 * external entity is refused outright (WFC: No External Entity References). */
436 static int
437 2206449 frx_lex_ref(frx_lex *l, int in_attr)
438 {
439 2206449 size_t start = l->pos;
440 2206449 size_t i = start + 1;
441 2206449 unsigned long cp = 0;
442 unsigned char enc[4];
443 size_t n;
444  
445 2206449 ▶ if (i < l->len && l->in[i] == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < l->len && l->in[i] == '#') { 2206359 90 2206449
if (i < l->len && l->in[i] == '#') { 11102 2195257 2206359
446 11102 int hex = 0, any = 0;
447 11102 i++;
448 11102 ▶ if (i < l->len && l->in[i] == 'x') { hex = 1; i++; }
Branch: 2 branches
BranchTrueFalseTotal
if (i < l->len && l->in[i] == 'x') { hex = 1; i++; } 11075 27 11102
if (i < l->len && l->in[i] == 'x') { hex = 1; i++; } 6811 4264 11075
449 31970 ▶ for (; i < l->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < l->len; i++) { 31873 97 31970
450 31873 unsigned char c = l->in[i];
451 unsigned d;
452 31873 ▶ if (c >= '0' && c <= '9') d = c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') d = c - '0'; 31851 22 31873
if (c >= '0' && c <= '9') d = c - '0'; 15642 16209 31851
453 16231 ▶ else if (hex && c >= 'a' && c <= 'f') d = c - 'a' + 10;
Branch: 3 branches
BranchTrueFalseTotal
else if (hex && c >= 'a' && c <= 'f') d = c - 'a' + 10; 11982 4249 16231
else if (hex && c >= 'a' && c <= 'f') d = c - 'a' + 10; 914 11068 11982
else if (hex && c >= 'a' && c <= 'f') d = c - 'a' + 10; 913 1 914
454 15318 ▶ else if (hex && c >= 'A' && c <= 'F') d = c - 'A' + 10;
Branch: 3 branches
BranchTrueFalseTotal
else if (hex && c >= 'A' && c <= 'F') d = c - 'A' + 10; 11069 4249 15318
else if (hex && c >= 'A' && c <= 'F') d = c - 'A' + 10; 4317 6752 11069
else if (hex && c >= 'A' && c <= 'F') d = c - 'A' + 10; 4313 4 4317
455 else break;
456 20868 ▶ cp = cp * (hex ? 16 : 10) + d;
Branch: 1 branch
BranchTrueFalseTotal
cp = cp * (hex ? 16 : 10) + d; 11768 9100 20868
457 20868 ▶ if (cp > 0x10FFFF) cp = 0x110000; /* clamp; refused below */
Branch: 1 branch
BranchTrueFalseTotal
if (cp > 0x10FFFF) cp = 0x110000; /* clamp; refused below */ 36 20832 20868
458 20868 any = 1;
459 }
460 11102 ▶ if (i >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (i >= l->len) 97 11005 11102
461 97 ▶ return FRX_LEX_NEED(l, FRX_E_REFERENCE, start, "malformed character reference");
Branch: 3 branches
BranchTrueFalseTotal
return FRX_LEX_NEED(l, FRX_E_REFERENCE, start, "malformed character reference"); 0 97 97
return FRX_LEX_NEED(l, FRX_E_REFERENCE, start, "malformed character reference"); 0 0 0
return FRX_LEX_NEED(l, FRX_E_REFERENCE, start, "malformed character reference"); 0 97 97
462 11005 ▶ if (!any || l->in[i] != ';')
Branch: 2 branches
BranchTrueFalseTotal
if (!any || l->in[i] != ';') 10990 15 11005
if (!any || l->in[i] != ';') 40 10950 10990
463 55 ▶ return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, "malformed character reference");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, "malformed character reference"); 0 55 55
464 /* 1.1's Char admits the restricted characters by reference; #x0 is
465 * a Char in neither version */
466 10950 ▶ if (l->version11 ? !frx_is_char11(cp) : !frx_is_char(cp))
Branch: 2 branches
BranchTrueFalseTotal
if (l->version11 ? !frx_is_char11(cp) : !frx_is_char(cp)) 251 10699 10950
if (l->version11 ? !frx_is_char11(cp) : !frx_is_char(cp)) 51 10899 10950
467 51 ▶ return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, "character reference to a non-XML character");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, "character reference to a non-XML character"); 0 51 51
468 10899 l->pos = i + 1;
469 } else {
470 static const struct { const char *name; size_t n; unsigned long cp; } ent[] = {
471 { "lt;", 3, '<' }, { "gt;", 3, '>' }, { "amp;", 4, '&' },
472 { "apos;", 5, '\'' }, { "quot;", 5, '"' }
473 };
474 size_t k;
475 13119457 ▶ for (k = 0; k < sizeof ent / sizeof ent[0]; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < sizeof ent / sizeof ent[0]; k++) { 10941963 2177494 13119457
476 10941963 ▶ if (frx_lex_starts(l, i, ent[k].name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, i, ent[k].name)) { 17853 10924110 10941963
477 17853 cp = ent[k].cp;
478 17853 l->pos = i + ent[k].n;
479 17853 break;
480 }
481 }
482 2195347 ▶ if (k == sizeof ent / sizeof ent[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (k == sizeof ent / sizeof ent[0]) { 2177494 17853 2195347
483 2177494 ▶ if (!l->eof && !l->n_frames) {
Branch: 2 branches
BranchTrueFalseTotal
if (!l->eof && !l->n_frames) { 18 2177476 2177494
if (!l->eof && !l->n_frames) { 18 0 18
484 90 ▶ for (k = 0; k < sizeof ent / sizeof ent[0]; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < sizeof ent / sizeof ent[0]; k++) 76 14 90
485 76 ▶ if (frx_lex_short(l, i, ent[k].name)) { l->need_more = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_short(l, i, ent[k].name)) { l->need_more = 1; return 0; } 4 72 76
486 }
487 2177490 ▶ if (l->profile == FRX_PROFILE_FULL)
Branch: 1 branch
BranchTrueFalseTotal
if (l->profile == FRX_PROFILE_FULL) 2177149 341 2177490
488 2177149 return frx_entity_reference(l, start, in_attr);
489 341 ▶ return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_REFERENCE, start, 0 341 341
490 "undeclared entity (no DOCTYPE is accepted, so only the five predefined exist)");
491 }
492 }
493 28752 n = frx_utf8_encode(cp, enc);
494 28752 frx_buf_append_n(&l->tmp, (const char *)enc, n);
495 28752 return 1;
496 }
497  
498 /* the frame the current one interrupted, or NULL at the document */
499 static const frx_frame_in *
500 2977381 frx_lex_top_frame(const frx_lex *l)
501 {
502 2977381 ▶ return l->n_frames ? &l->frames[l->n_frames - 1] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return l->n_frames ? &l->frames[l->n_frames - 1] : NULL; 120 2977261 2977381
503 }
504  
505 /* A Name at pos into the arena. With qname, the Namespaces rule: at most
506 * one colon, neither part empty. */
507 static int
508 10714756 frx_lex_name(frx_lex *l, int qname, frx_str *out)
509 {
510 10714756 size_t start = l->pos;
511 10714756 size_t colons = 0;
512 10714756 int first = 1;
513 53523918 ▶ while (l->pos < l->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (l->pos < l->len) { 53516225 7693 53523918
514 53516225 unsigned char c = l->in[l->pos];
515 unsigned long cp;
516 size_t n;
517 53516225 ▶ if (c < 0x80) { cp = c; n = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { cp = c; n = 1; } 53506189 10036 53516225
518 else {
519 10036 n = frx_utf8_decode(l->in + l->pos, l->len - l->pos, &cp);
520 10074 ▶ if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); }
Branch: 3 branches
BranchTrueFalseTotal
if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); } 97 9939 10036
if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); } 0 97 97
if (!n) { if (frx_lex_utf8_cut(l)) { l->need_more = 1; return 0; } return FRX_LEX_FAIL(l, FRX_E_UTF8, l->pos, "not UTF-8"); } 0 97 97
521 }
522 53516128 ▶ if (cp == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (cp == ':') { 1093154 52422974 53516128
523 1093154 ▶ if (!qname || first || colons)
Branch: 3 branches
BranchTrueFalseTotal
if (!qname || first || colons) 1093151 3 1093154
if (!qname || first || colons) 1093133 18 1093151
if (!qname || first || colons) 17 1093116 1093133
524 38 ▶ return FRX_LEX_FAIL(l, FRX_E_NAME, l->pos, "a name may carry one colon, with a part on each side");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_NAME, l->pos, "a name may carry one colon, with a part on each side"); 0 38 38
525 1093116 colons++;
526 1093116 l->pos++;
527 1093116 first = 1; /* the local part must start a name too */
528 1093116 continue;
529 }
530 52422974 ▶ if (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp)) break;
Branch: 2 branches
BranchTrueFalseTotal
if (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp)) break; 11805673 40617301 52422974
if (first ? !frx_is_name_start(cp) : !frx_is_name_char(cp)) break; 10706928 41716046 52422974
531 41716046 first = 0;
532 41716046 l->pos += n;
533 }
534 10714621 ▶ if (l->pos >= l->len && !l->eof && !l->n_frames) { l->need_more = 1; return 0; } /* it may go on */
Branch: 3 branches
BranchTrueFalseTotal
if (l->pos >= l->len && !l->eof && !l->n_frames) { l->need_more = 1; return 0; } /* it may go on */ 7693 10706928 10714621
if (l->pos >= l->len && !l->eof && !l->n_frames) { l->need_more = 1; return 0; } /* it may go on */ 856 6837 7693
if (l->pos >= l->len && !l->eof && !l->n_frames) { l->need_more = 1; return 0; } /* it may go on */ 856 0 856
535 10713765 ▶ if (l->pos == start)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos == start) 2323 10711442 10713765
536 2323 ▶ return FRX_LEX_FAIL(l, FRX_E_NAME, start, "expected a name");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_NAME, start, "expected a name"); 7 2316 2323
537 10711442 ▶ if (first) /* ended on a colon, or empty local part */
Branch: 1 branch
BranchTrueFalseTotal
if (first) /* ended on a colon, or empty local part */ 295 10711147 10711442
538 295 ▶ return FRX_LEX_FAIL(l, FRX_E_NAME, start, "a name may carry one colon, with a part on each side");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_NAME, start, "a name may carry one colon, with a part on each side"); 0 295 295
539 10711147 return frx_lex_intern(l, l->in + start, l->pos - start, out);
540 }
541  
542 /* Section 3.3.3 for a type other than CDATA: leading and trailing spaces
543 * (#x20 only) dropped, runs of them made one. tmp in place; returns
544 * whether anything changed, which the standalone constraint asks. */
545 static int
546 4559 frx_lex_collapse(frx_buf *b)
547 {
548 4559 size_t r, w = 0;
549 4559 int pending = 0;
550 4559 ▶ if (!b->p) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b->p) return 0; 0 4559 4559
551 38894 ▶ for (r = 0; r < b->len; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->len; r++) { 34335 4559 38894
552 34335 ▶ if (b->p[r] == ' ') { pending = w > 0; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->p[r] == ' ') { pending = w > 0; continue; } 740 33595 34335
553 33595 ▶ if (pending) { b->p[w++] = ' '; pending = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (pending) { b->p[w++] = ' '; pending = 0; } 384 33211 33595
554 33595 b->p[w++] = b->p[r];
555 }
556 4559 ▶ if (w == b->len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (w == b->len) return 0; 4479 80 4559
557 80 b->len = w;
558 80 b->p[w] = '\0';
559 80 return 1;
560 }
561  
562 /* An attribute value at pos (the quote). Section 2.11 then 3.3.3, on
563 * literal whitespace only; references exempt; a literal < refused. Under
564 * the full profile an entity reference pushes a frame the loop reads on
565 * through: its quote does not close the value (4.4.5, Included in
566 * Literal), its < is refused (WFC: No < in Attribute Values), and it must
567 * end before the value does. collapse is the further 3.3.3 rule for a
568 * declared type other than CDATA. */
569 static int
570 2038496 frx_lex_attr_value(frx_lex *l, frx_str *out, int collapse)
571 {
572 2038496 unsigned char q = l->in[l->pos];
573 2038496 size_t start = l->pos;
574 2038496 int frame0 = l->n_frames;
575 2038496 ▶ if (q != '"' && q != '\'')
Branch: 2 branches
BranchTrueFalseTotal
if (q != '"' && q != '\'') 3916 2034580 2038496
if (q != '"' && q != '\'') 139 3777 3916
576 139 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected a quoted attribute value");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected a quoted attribute value"); 0 139 139
577 2038357 l->pos++;
578 2038357 l->tmp.len = 0;
579 15175736 for (;;) {
580 unsigned char c;
581 17214093 ▶ if (l->pos >= l->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) { 3515 17210578 17214093
582 3515 ▶ 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; } 359 3156 3515
if (l->n_frames > frame0) { if (!frx_entity_pop(l)) return 0; continue; } 0 359 359
583 3156 ▶ return FRX_LEX_NEED(l, FRX_E_SYNTAX, start, "unterminated attribute value");
Branch: 3 branches
BranchTrueFalseTotal
return FRX_LEX_NEED(l, FRX_E_SYNTAX, start, "unterminated attribute value"); 202 2954 3156
return FRX_LEX_NEED(l, FRX_E_SYNTAX, start, "unterminated attribute value"); 202 0 202
return FRX_LEX_NEED(l, FRX_E_SYNTAX, start, "unterminated attribute value"); 0 2954 2954
584 }
585 17210578 c = l->in[l->pos];
586 17210578 ▶ if (c == q && l->n_frames == frame0) { l->pos++; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == q && l->n_frames == frame0) { l->pos++; break; } 2034747 15175831 17210578
if (c == q && l->n_frames == frame0) { l->pos++; break; } 2034740 7 2034747
587 15175838 ▶ if (c == '<')
Branch: 1 branch
BranchTrueFalseTotal
if (c == '<') 108 15175730 15175838
588 108 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos,
Branch: 2 branches
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, 4 104 108
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, 0 108 108
589 l->n_frames > frame0
590 ? "an entity referenced in an attribute value must not contain <"
591 : "a literal < is not allowed in an attribute value");
592 15175730 ▶ if (c == '&') { if (!frx_lex_ref(l, 1)) return 0; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '&') { if (!frx_lex_ref(l, 1)) return 0; continue; } 15555 15160175 15175730
if (c == '&') { if (!frx_lex_ref(l, 1)) return 0; continue; } 331 15224 15555
593 15160175 ▶ if (c == '\r') { /* 2.11, then 3.3.3 */
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\r') { /* 2.11, then 3.3.3 */ 23 15160152 15160175
594 23 frx_buf_append_ch(&l->tmp, ' ');
595 23 l->pos++;
596 23 ▶ if (l->n_frames) continue; /* replacement text: each character one space */
Branch: 1 branch
BranchTrueFalseTotal
if (l->n_frames) continue; /* replacement text: each character one space */ 12 11 23
597 11 ▶ if (l->pos < l->len && l->in[l->pos] == '\n') l->pos++;
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos < l->len && l->in[l->pos] == '\n') l->pos++; 11 0 11
if (l->pos < l->len && l->in[l->pos] == '\n') l->pos++; 9 2 11
598 2 ▶ else if (l->version11 && frx_lex_nel_or_ls(l, l->pos) == 2) l->pos += 2; /* 1.1: CR NEL */
Branch: 2 branches
BranchTrueFalseTotal
else if (l->version11 && frx_lex_nel_or_ls(l, l->pos) == 2) l->pos += 2; /* 1.1: CR NEL */ 1 1 2
else if (l->version11 && frx_lex_nel_or_ls(l, l->pos) == 2) l->pos += 2; /* 1.1: CR NEL */ 1 0 1
599 11 continue;
600 }
601 15160152 ▶ if (c == '\n' || c == '\t') { /* 3.3.3 */
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\n' || c == '\t') { /* 3.3.3 */ 15160079 73 15160152
if (c == '\n' || c == '\t') { /* 3.3.3 */ 49 15160030 15160079
602 122 frx_buf_append_ch(&l->tmp, ' ');
603 122 l->pos++;
604 122 continue;
605 }
606 15160030 ▶ if (l->version11 && !l->n_frames) { /* 1.1: NEL and LS are line ends, so spaces */
Branch: 2 branches
BranchTrueFalseTotal
if (l->version11 && !l->n_frames) { /* 1.1: NEL and LS are line ends, so spaces */ 475 15159555 15160030
if (l->version11 && !l->n_frames) { /* 1.1: NEL and LS are line ends, so spaces */ 456 19 475
607 456 size_t n = frx_lex_nel_or_ls(l, l->pos);
608 456 ▶ if (n) { frx_buf_append_ch(&l->tmp, ' '); l->pos += n; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n) { frx_buf_append_ch(&l->tmp, ' '); l->pos += n; continue; } 7 449 456
609 }
610 15160023 ▶ if (!frx_lex_char(l)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_char(l)) return 0; 22 15160001 15160023
611 }
612 2034740 ▶ l->last_collapse_changed = collapse ? frx_lex_collapse(&l->tmp) : 0;
Branch: 1 branch
BranchTrueFalseTotal
l->last_collapse_changed = collapse ? frx_lex_collapse(&l->tmp) : 0; 4559 2030181 2034740
613 2034740 return frx_lex_intern_buf(l, out);
614 }
615  
616 /* offset is a document offset */
617 static int
618 2033543 frx_lex_push_attr(frx_lex *l, const frx_str *name, const frx_str *value, size_t offset, int defaulted)
619 {
620 int i;
621 53012728 ▶ for (i = 0; i < l->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l->n_attrs; i++) { 50979195 2033533 53012728
622 50979195 ▶ if (l->attrs[i].name.len == name->len
Branch: 1 branch
BranchTrueFalseTotal
if (l->attrs[i].name.len == name->len 40930541 10048654 50979195
623 40930541 ▶ && memcmp(l->attrs[i].name.p, name->p, name->len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(l->attrs[i].name.p, name->p, name->len) == 0) 10 40930531 40930541
624 10 return FRX_LEX_FAIL_ABS(l, FRX_E_DUP_ATTR, offset, "attribute given twice");
625 }
626 2033533 ▶ if (l->n_attrs == l->cap_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->n_attrs == l->cap_attrs) { 21765 2011768 2033533
627 21765 ▶ int ncap = l->cap_attrs ? l->cap_attrs * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = l->cap_attrs ? l->cap_attrs * 2 : 8; 53 21712 21765
628 21765 frx_lex_attr *na = (frx_lex_attr *)realloc(l->attrs, (size_t)ncap * sizeof *na);
629 21765 ▶ if (!na) return frx_lex_nomem(l);
Branch: 1 branch
BranchTrueFalseTotal
if (!na) return frx_lex_nomem(l); 0 21765 21765
630 21765 l->attrs = na;
631 21765 l->cap_attrs = ncap;
632 }
633 2033533 l->attrs[l->n_attrs].name = *name;
634 2033533 l->attrs[l->n_attrs].value = *value;
635 2033533 l->attrs[l->n_attrs].offset = offset;
636 2033533 l->attrs[l->n_attrs].defaulted = defaulted;
637 2033533 l->n_attrs++;
638 2033533 return 1;
639 }
640  
641 /* <name attrs> or <name attrs/>; pos is just past the <. With a DTD, each
642 * attribute's declared type selects the 3.3.3 normalisation before its
643 * value is read, and the declared defaults the tag lacks are appended
644 * after the written attributes, in declaration order. */
645 static int
646 3475411 frx_lex_start_tag(frx_lex *l, frx_tok *t)
647 {
648 const frx_eldecl *ed;
649 3475411 ▶ if (!frx_lex_name(l, 1, &t->name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 1, &t->name)) return 0; 1871 3473540 3475411
650 3473540 ▶ ed = l->dtd ? frx_dtd_element(l->dtd, &t->name) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ed = l->dtd ? frx_dtd_element(l->dtd, &t->name) : NULL; 13372 3460168 3473540
651 3473540 l->n_attrs = 0;
652 2031874 for (;;) {
653 5505414 size_t had_s = frx_lex_skip_s(l);
654 unsigned char c;
655 5505414 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 2688 5502726 5505414
656 2688 ▶ return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->tok_start, "unterminated start tag");
Branch: 3 branches
BranchTrueFalseTotal
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->tok_start, "unterminated start tag"); 78 2610 2688
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->tok_start, "unterminated start tag"); 78 0 78
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->tok_start, "unterminated start tag"); 0 2610 2610
657 5502726 c = l->in[l->pos];
658 5502726 ▶ if (c == '>') { l->pos++; t->kind = FRX_TOK_START; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == '>') { l->pos++; t->kind = FRX_TOK_START; break; } 3048552 2454174 5502726
659 2454174 ▶ if (c == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '/') { 416141 2038033 2454174
660 416141 ▶ if (l->pos + 1 >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos + 1 >= l->len) 150 415991 416141
661 150 ▶ return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "expected /> to close an empty element");
Branch: 3 branches
BranchTrueFalseTotal
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "expected /> to close an empty element"); 4 146 150
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "expected /> to close an empty element"); 4 0 4
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "expected /> to close an empty element"); 0 146 146
662 415991 ▶ if (l->in[l->pos + 1] != '>')
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos + 1] != '>') 150 415841 415991
663 150 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected /> to close an empty element");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected /> to close an empty element"); 0 150 150
664 415841 l->pos += 2;
665 415841 t->kind = FRX_TOK_EMPTY;
666 415841 break;
667 }
668 2038033 ▶ if (!had_s)
Branch: 1 branch
BranchTrueFalseTotal
if (!had_s) 329 2037704 2038033
669 329 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected whitespace before an attribute");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected whitespace before an attribute"); 1 328 329
670 {
671 frx_str name, value;
672 2037704 size_t at = l->pos;
673 const frx_attdef *ad;
674 2043172 ▶ if (!frx_lex_name(l, 1, &name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 1, &name)) return 0; 362 2037342 2037704
675 2037342 ▶ ad = ed ? frx_dtd_attdef(ed, &name) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ad = ed ? frx_dtd_attdef(ed, &name) : NULL; 6024 2031318 2037342
676 2037342 frx_lex_skip_s(l);
677 2037342 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 1216 2036126 2037342
678 1216 ▶ return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "expected = after an attribute name");
Branch: 3 branches
BranchTrueFalseTotal
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "expected = after an attribute name"); 0 1216 1216
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "expected = after an attribute name"); 0 0 0
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "expected = after an attribute name"); 0 1216 1216
679 2036126 ▶ if (l->in[l->pos] != '=')
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos] != '=') 159 2035967 2036126
680 159 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected = after an attribute name");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected = after an attribute name"); 0 159 159
681 2035967 l->pos++;
682 2035967 frx_lex_skip_s(l);
683 2035967 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 329 2035638 2035967
684 329 ▶ return FRX_LEX_NEED(l, FRX_E_SYNTAX, at, "unterminated attribute");
Branch: 3 branches
BranchTrueFalseTotal
return FRX_LEX_NEED(l, FRX_E_SYNTAX, at, "unterminated attribute"); 53 276 329
return FRX_LEX_NEED(l, FRX_E_SYNTAX, at, "unterminated attribute"); 53 0 53
return FRX_LEX_NEED(l, FRX_E_SYNTAX, at, "unterminated attribute"); 0 276 276
685 2035638 ▶ if (!frx_lex_attr_value(l, &value, ad ? frx_dtd_attdef_collapses(ad) : 0)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_lex_attr_value(l, &value, ad ? frx_dtd_attdef_collapses(ad) : 0)) return 0; 5425 2030213 2035638
if (!frx_lex_attr_value(l, &value, ad ? frx_dtd_attdef_collapses(ad) : 0)) return 0; 3742 2031896 2035638
686 2031896 ▶ if (ad && l->last_collapse_changed && l->standalone && l->validate && frx_dtd_attdef_external(ad))
Branch: 5 branches
BranchTrueFalseTotal
if (ad && l->last_collapse_changed && l->standalone && l->validate && frx_dtd_attdef_external(ad)) 5392 2026504 2031896
if (ad && l->last_collapse_changed && l->standalone && l->validate && frx_dtd_attdef_external(ad)) 69 5323 5392
if (ad && l->last_collapse_changed && l->standalone && l->validate && frx_dtd_attdef_external(ad)) 29 40 69
if (ad && l->last_collapse_changed && l->standalone && l->validate && frx_dtd_attdef_external(ad)) 20 9 29
if (ad && l->last_collapse_changed && l->standalone && l->validate && frx_dtd_attdef_external(ad)) 12 8 20
687 12 ▶ return FRX_LEX_FAIL(l, FRX_E_DTD, at,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DTD, at, 0 12 12
688 "standalone=\"yes\" but an attribute type declared in the external subset changed this value (VC: Standalone Document Declaration)");
689 2031884 ▶ if (!frx_lex_push_attr(l, &name, &value, FRX_LEX_ABS(l, at), 0)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_lex_push_attr(l, &name, &value, FRX_LEX_ABS(l, at), 0)) return 0; 0 2031884 2031884
if (!frx_lex_push_attr(l, &name, &value, FRX_LEX_ABS(l, at), 0)) return 0; 10 2031874 2031884
690 }
691 }
692 3464393 ▶ if (ed && !frx_dtd_apply_defaults(l, ed)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (ed && !frx_dtd_apply_defaults(l, ed)) return 0; 12680 3451713 3464393
if (ed && !frx_dtd_apply_defaults(l, ed)) return 0; 3 12677 12680
693 3464390 t->attrs = l->attrs;
694 3464390 t->n_attrs = l->n_attrs;
695 3464390 return 1;
696 }
697  
698 /* </name>; pos is just past the </ */
699 static int
700 2977381 frx_lex_end_tag(frx_lex *l, frx_tok *t)
701 {
702 2977381 ▶ if (!frx_lex_name(l, 1, &t->name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 1, &t->name)) return 0; 1133 2976248 2977381
703 2976248 frx_lex_skip_s(l);
704 2976248 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 1637 2974611 2976248
705 1637 ▶ return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->tok_start, "unterminated end tag");
Branch: 3 branches
BranchTrueFalseTotal
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->tok_start, "unterminated end tag"); 0 1637 1637
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->tok_start, "unterminated end tag"); 0 0 0
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->tok_start, "unterminated end tag"); 0 1637 1637
706 2974611 ▶ if (l->in[l->pos] != '>')
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos] != '>') 163 2974448 2974611
707 163 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->tok_start, "unterminated end tag");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->tok_start, "unterminated end tag"); 0 163 163
708 2974448 l->pos++;
709 2974448 t->kind = FRX_TOK_END;
710 2974448 return 1;
711 }
712  
713 /* content up to the terminator lit, validated and line-end normalised
714 * into tmp; start is frame-relative; pos ends past lit. The end of the
715 * frame is the end of the input here: a comment, PI or CDATA section that
716 * opened inside an entity must close inside it. */
717 static int
718 64022 frx_lex_run(frx_lex *l, const char *lit, size_t start, const char *unterminated,
719 int comment)
720 {
721 64022 size_t n = strlen(lit);
722 64022 l->tmp.len = 0;
723 for (;;) {
724 555198 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 528 554670 555198
725 528 ▶ return FRX_LEX_NEED(l, FRX_E_SYNTAX, start, unterminated);
Branch: 3 branches
BranchTrueFalseTotal
return FRX_LEX_NEED(l, FRX_E_SYNTAX, start, unterminated); 104 424 528
return FRX_LEX_NEED(l, FRX_E_SYNTAX, start, unterminated); 104 0 104
return FRX_LEX_NEED(l, FRX_E_SYNTAX, start, unterminated); 1 423 424
726 554670 ▶ if (l->len - l->pos >= n && memcmp(l->in + l->pos, lit, n) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->len - l->pos >= n && memcmp(l->in + l->pos, lit, n) == 0) { 553840 830 554670
if (l->len - l->pos >= n && memcmp(l->in + l->pos, lit, n) == 0) { 63312 490528 553840
727 63312 l->pos += n;
728 63312 return 1;
729 }
730 491358 ▶ if (!l->eof && !l->n_frames && frx_lex_short(l, l->pos, lit)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!l->eof && !l->n_frames && frx_lex_short(l, l->pos, lit)) { 736 490622 491358
if (!l->eof && !l->n_frames && frx_lex_short(l, l->pos, lit)) { 736 0 736
if (!l->eof && !l->n_frames && frx_lex_short(l, l->pos, lit)) { 28 708 736
731 28 l->need_more = 1; /* the terminator may be cut */
732 28 return 0;
733 }
734 491330 ▶ if (comment && l->in[l->pos] == '-' && l->pos + 1 < l->len && l->in[l->pos + 1] == '-')
Branch: 4 branches
BranchTrueFalseTotal
if (comment && l->in[l->pos] == '-' && l->pos + 1 < l->len && l->in[l->pos + 1] == '-') 334816 156514 491330
if (comment && l->in[l->pos] == '-' && l->pos + 1 < l->len && l->in[l->pos + 1] == '-') 1933 332883 334816
if (comment && l->in[l->pos] == '-' && l->pos + 1 < l->len && l->in[l->pos + 1] == '-') 1919 14 1933
if (comment && l->in[l->pos] == '-' && l->pos + 1 < l->len && l->in[l->pos + 1] == '-') 38 1881 1919
735 38 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "-- is not allowed inside a comment");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "-- is not allowed inside a comment"); 0 38 38
736 491292 ▶ if (!frx_lex_char(l)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_char(l)) return 0; 116 491176 491292
737 }
738 }
739  
740 /* <!-- ... -->; pos is just past the <!-- */
741 static int
742 7736 frx_lex_comment(frx_lex *l, frx_tok *t)
743 {
744 7736 ▶ if (!frx_lex_run(l, "-->", l->tok_start, "unterminated comment", 1)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_run(l, "-->", l->tok_start, "unterminated comment", 1)) return 0; 418 7318 7736
745 7318 t->kind = FRX_TOK_COMMENT;
746 7318 return frx_lex_intern_buf(l, &t->value);
747 }
748  
749 /* <![CDATA[ ... ]]>; pos is just past the <![CDATA[ */
750 static int
751 52165 frx_lex_cdata(frx_lex *l, frx_tok *t)
752 {
753 52165 ▶ if (!frx_lex_run(l, "]]>", l->tok_start, "unterminated CDATA section", 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_run(l, "]]>", l->tok_start, "unterminated CDATA section", 0)) return 0; 120 52045 52165
754 52045 t->kind = FRX_TOK_TEXT;
755 52045 t->cdata = 1;
756 52045 return frx_lex_intern_buf(l, &t->value);
757 }
758  
759 static int
760 5740 frx_lex_target_is_xml(const frx_str *s)
761 {
762 5740 return s->len == 3
763 598 ▶ && (s->p[0] == 'x' || s->p[0] == 'X')
Branch: 2 branches
BranchTrueFalseTotal
&& (s->p[0] == 'x' || s->p[0] == 'X') 559 39 598
&& (s->p[0] == 'x' || s->p[0] == 'X') 7 552 559
764 46 ▶ && (s->p[1] == 'm' || s->p[1] == 'M')
Branch: 2 branches
BranchTrueFalseTotal
&& (s->p[1] == 'm' || s->p[1] == 'M') 10 36 46
&& (s->p[1] == 'm' || s->p[1] == 'M') 10 0 10
765 6338 ▶ && (s->p[2] == 'l' || s->p[2] == 'L');
Branch: 3 branches
BranchTrueFalseTotal
&& (s->p[2] == 'l' || s->p[2] == 'L'); 598 5142 5740
&& (s->p[2] == 'l' || s->p[2] == 'L'); 11 35 46
&& (s->p[2] == 'l' || s->p[2] == 'L'); 11 0 11
766 }
767  
768 /* <?target data?>; pos is just past the <? */
769 static int
770 5643 frx_lex_pi(frx_lex *l, frx_tok *t)
771 {
772 5643 size_t name_at = l->pos;
773 5643 ▶ if (!frx_lex_name(l, 0, &t->name)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_name(l, 0, &t->name)) return 0; 47 5596 5643
774 5596 ▶ if (frx_lex_target_is_xml(&t->name))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_target_is_xml(&t->name)) 45 5551 5596
775 45 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, name_at,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, name_at, 7 38 45
776 "the xml processing instruction target is reserved (a declaration is accepted only at offset 0)");
777 5551 ▶ if (frx_lex_starts(l, l->pos, "?>")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "?>")) { 1321 4230 5551
778 1321 l->pos += 2;
779 1321 t->value.p = "";
780 1321 t->value.len = 0;
781 1321 t->kind = FRX_TOK_PI;
782 1321 return 1;
783 }
784 4230 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 88 4142 4230
785 88 ▶ return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "unterminated processing instruction");
Branch: 3 branches
BranchTrueFalseTotal
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "unterminated processing instruction"); 0 88 88
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "unterminated processing instruction"); 0 0 0
return FRX_LEX_NEED(l, FRX_E_SYNTAX, l->pos, "unterminated processing instruction"); 0 88 88
786 4142 ▶ if (!frx_lex_skip_s(l))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_skip_s(l)) 21 4121 4142
787 21 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected whitespace after a processing instruction target");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected whitespace after a processing instruction target"); 0 21 21
788 4121 ▶ if (!frx_lex_run(l, "?>", l->tok_start, "unterminated processing instruction", 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_run(l, "?>", l->tok_start, "unterminated processing instruction", 0)) return 0; 172 3949 4121
789 3949 t->kind = FRX_TOK_PI;
790 3949 return frx_lex_intern_buf(l, &t->value);
791 }
792  
793 /* character data up to the next < or the end of the frame; pos is at a
794 * non-< byte. An entity reference switches frames mid-run and the loop
795 * reads on; the frame's end ends the token, and the next call pops. */
796 static int
797 7002960 frx_lex_text(frx_lex *l, frx_tok *t)
798 {
799 7002960 int frames0 = l->n_frames;
800 7002960 l->tmp.len = 0;
801 150774112 ▶ while (l->pos < l->len && l->in[l->pos] != '<') {
Branch: 2 branches
BranchTrueFalseTotal
while (l->pos < l->len && l->in[l->pos] != '<') { 148806743 1967369 150774112
while (l->pos < l->len && l->in[l->pos] != '<') { 145948342 2858401 148806743
802 145948342 unsigned char c = l->in[l->pos];
803 145948342 ▶ if (c == '&') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '&') { 2189987 143758355 145948342
804 /* outside the root only Misc may stand (section 2.1, [1] and
805 * [27]): a reference there is not well-formed even when it
806 * would produce whitespace or the root element itself */
807 2189987 ▶ if (!l->tag_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (!l->tag_depth) 9 2189978 2189987
808 9 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "a reference is not allowed outside the root element");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "a reference is not allowed outside the root element"); 0 9 9
809 2189978 ▶ if (!frx_lex_ref(l, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_ref(l, 0)) return 0; 338 2189640 2189978
810 /* an entity's text is its own token: the boundary is what a
811 * reader reports, and the tree merges the pieces anyway */
812 2189640 ▶ if (l->n_frames > frames0) { t->text_flags |= FRX_TEXT_REF_HERE; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (l->n_frames > frames0) { t->text_flags |= FRX_TEXT_REF_HERE; break; } 2176620 13020 2189640
813 /* Second edition erratum E15: white space in element content
814 * must be characters that stand in the document, not ones a
815 * reference produced. This branch is a character or a
816 * predefined-entity reference, which expands here rather than
817 * pushing a frame, so its characters are not written directly. */
818 13020 t->text_flags |= FRX_TEXT_REF_CHARS | FRX_TEXT_REF_HERE;
819 13020 continue;
820 }
821 143758355 ▶ if (c == ']' && frx_lex_starts(l, l->pos, "]]>"))
Branch: 2 branches
BranchTrueFalseTotal
if (c == ']' && frx_lex_starts(l, l->pos, "]]>")) 1600 143756755 143758355
if (c == ']' && frx_lex_starts(l, l->pos, "]]>")) 9 1591 1600
822 9 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "]]> is not allowed in content");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "]]> is not allowed in content"); 0 9 9
823 143758346 ▶ if (!frx_lex_char(l)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_char(l)) return 0; 214 143758132 143758346
824 }
825 /* a run the input ends inside may go on in the next chunk: one text
826 * token per run, whatever the chunking, is what a reader promises */
827 7002390 ▶ if (l->pos >= l->len && l->n_frames == frames0 && !l->eof && !l->n_frames) { l->need_more = 1; return 0; }
Branch: 4 branches
BranchTrueFalseTotal
if (l->pos >= l->len && l->n_frames == frames0 && !l->eof && !l->n_frames) { l->need_more = 1; return 0; } 1967393 5034997 7002390
if (l->pos >= l->len && l->n_frames == frames0 && !l->eof && !l->n_frames) { l->need_more = 1; return 0; } 1967369 24 1967393
if (l->pos >= l->len && l->n_frames == frames0 && !l->eof && !l->n_frames) { l->need_more = 1; return 0; } 1440 1965929 1967369
if (l->pos >= l->len && l->n_frames == frames0 && !l->eof && !l->n_frames) { l->need_more = 1; return 0; } 1440 0 1440
828 7000950 t->kind = FRX_TOK_TEXT;
829 7000950 return frx_lex_intern_buf(l, &t->value);
830 }
831  
832 /* Is the DOCTYPE at pos complete in the input, its > found at bracket
833 * depth zero outside quotes, comments and processing instructions? With
834 * eof clear a DOCTYPE the input ends inside is FRX_TOK_MORE before its
835 * parser runs, so that parser needs no retry logic of its own. */
836 static int
837 966 frx_lex_doctype_complete(const frx_lex *l)
838 {
839 966 size_t i = l->pos;
840 966 int depth = 0;
841 966 char q = 0;
842 78846 ▶ while (i < l->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < l->len) { 78688 158 78846
843 78688 unsigned char c = l->in[i];
844 78688 ▶ if (q) { if (c == (unsigned char)q) q = 0; i++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (q) { if (c == (unsigned char)q) q = 0; i++; continue; } 21368 57320 78688
if (q) { if (c == (unsigned char)q) q = 0; i++; continue; } 1692 19676 21368
845 57320 ▶ if (c == '"' || c == '\'') { q = (char)c; i++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '"' || c == '\'') { q = (char)c; i++; continue; } 55580 1740 57320
if (c == '"' || c == '\'') { q = (char)c; i++; continue; } 0 55580 55580
846 55580 ▶ if (frx_lex_starts(l, i, "<!--")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, i, "<!--")) { 0 55580 55580
847 0 i += 4;
848 0 ▶ while (i < l->len && !frx_lex_starts(l, i, "-->")) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < l->len && !frx_lex_starts(l, i, "-->")) i++; 0 0 0
while (i < l->len && !frx_lex_starts(l, i, "-->")) i++; 0 0 0
849 0 ▶ if (i >= l->len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= l->len) return 0; 0 0 0
850 0 i += 3;
851 0 continue;
852 }
853 55580 ▶ if (frx_lex_starts(l, i, "<?")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, i, "<?")) { 0 55580 55580
854 0 i += 2;
855 0 ▶ while (i < l->len && !frx_lex_starts(l, i, "?>")) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < l->len && !frx_lex_starts(l, i, "?>")) i++; 0 0 0
while (i < l->len && !frx_lex_starts(l, i, "?>")) i++; 0 0 0
856 0 ▶ if (i >= l->len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= l->len) return 0; 0 0 0
857 0 i += 2;
858 0 continue;
859 }
860 55580 ▶ if (c == '[') depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '[') depth++; 954 54626 55580
861 54626 ▶ else if (c == ']') { if (depth) depth--; }
Branch: 2 branches
BranchTrueFalseTotal
else if (c == ']') { if (depth) depth--; } 810 53816 54626
else if (c == ']') { if (depth) depth--; } 810 0 810
862 53816 ▶ else if (c == '>' && depth == 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
else if (c == '>' && depth == 0) return 1; 2496 51320 53816
else if (c == '>' && depth == 0) return 1; 808 1688 2496
863 54772 i++;
864 }
865 158 return 0;
866 }
867  
868 /* a pseudo-attribute of the XML declaration: name S? = S? quoted; the
869 * value is returned as a slice of the input */
870 static int
871 22833 frx_lex_pseudo(frx_lex *l, const char *name, const unsigned char **v, size_t *vlen)
872 {
873 unsigned char q;
874 size_t start;
875 22833 ▶ if (!frx_lex_starts(l, l->pos, name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_starts(l, l->pos, name)) { 110 22723 22833
876 110 ▶ if (!l->eof && frx_lex_short(l, l->pos, name)) l->need_more = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!l->eof && frx_lex_short(l, l->pos, name)) l->need_more = 1; 42 68 110
if (!l->eof && frx_lex_short(l, l->pos, name)) l->need_more = 1; 42 0 42
877 110 return 0;
878 }
879 22723 l->pos += strlen(name);
880 22723 frx_lex_skip_s(l);
881 22723 ▶ if (l->pos >= l->len) { if (!l->eof) l->need_more = 1; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos >= l->len) { if (!l->eof) l->need_more = 1; return 0; } 19 22704 22723
if (l->pos >= l->len) { if (!l->eof) l->need_more = 1; return 0; } 10 9 19
882 22704 ▶ if (l->in[l->pos] != '=') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos] != '=') return 0; 16 22688 22704
883 22688 l->pos++;
884 22688 frx_lex_skip_s(l);
885 22688 ▶ if (l->pos >= l->len) { if (!l->eof) l->need_more = 1; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos >= l->len) { if (!l->eof) l->need_more = 1; return 0; } 19 22669 22688
if (l->pos >= l->len) { if (!l->eof) l->need_more = 1; return 0; } 10 9 19
886 22669 q = l->in[l->pos];
887 22669 ▶ if (q != '"' && q != '\'') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (q != '"' && q != '\'') return 0; 237 22432 22669
if (q != '"' && q != '\'') return 0; 13 224 237
888 22656 start = ++l->pos;
889 112501 ▶ while (l->pos < l->len && l->in[l->pos] != q) l->pos++;
Branch: 2 branches
BranchTrueFalseTotal
while (l->pos < l->len && l->in[l->pos] != q) l->pos++; 112399 102 112501
while (l->pos < l->len && l->in[l->pos] != q) l->pos++; 89845 22554 112399
890 22656 ▶ if (l->pos >= l->len) { if (!l->eof) l->need_more = 1; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (l->pos >= l->len) { if (!l->eof) l->need_more = 1; return 0; } 102 22554 22656
if (l->pos >= l->len) { if (!l->eof) l->need_more = 1; return 0; } 52 50 102
891 22554 *v = l->in + start;
892 22554 *vlen = l->pos - start;
893 22554 l->pos++;
894 22554 return 1;
895 }
896  
897 static int
898 7113 frx_lex_ieq(const unsigned char *v, size_t n, const char *lit)
899 {
900 size_t i;
901 7113 ▶ if (n != strlen(lit)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n != strlen(lit)) return 0; 22 7091 7113
902 42546 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 35455 7091 42546
903 35455 unsigned char a = v[i], b = (unsigned char)lit[i];
904 35455 ▶ if (a >= 'A' && a <= 'Z') a = (unsigned char)(a - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (a >= 'A' && a <= 'Z') a = (unsigned char)(a - 'A' + 'a'); 21273 14182 35455
if (a >= 'A' && a <= 'Z') a = (unsigned char)(a - 'A' + 'a'); 19371 1902 21273
905 35455 ▶ if (a != b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a != b) return 0; 0 35455 35455
906 }
907 7091 return 1;
908 }
909  
910 /* the prolog: max_bytes, the BOMs, the first pair, the declaration. With
911 * eof clear it asks for more bytes wherever the declaration may go on. */
912 #define FRX_PROLOG_NEED(l, what) \
913 (((l)->need_more || ((l)->pos >= (l)->len && !(l)->eof)) ? ((l)->need_more = 1, 0) : FRX_LEX_FAIL((l), FRX_E_SYNTAX, at, (what)))
914  
915 static int
916 29379 frx_lex_prolog(frx_lex *l)
917 {
918 29379 const unsigned char *in = l->in;
919 29379 size_t len = l->len;
920 const unsigned char *v;
921 size_t vlen, had_s;
922  
923 29379 ▶ if (l->prolog_max_bytes && len > l->prolog_max_bytes)
Branch: 2 branches
BranchTrueFalseTotal
if (l->prolog_max_bytes && len > l->prolog_max_bytes) 5 29374 29379
if (l->prolog_max_bytes && len > l->prolog_max_bytes) 3 2 5
924 3 ▶ return FRX_LEX_FAIL(l, FRX_E_TOO_LARGE, l->prolog_max_bytes, "input exceeds max_bytes");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_TOO_LARGE, l->prolog_max_bytes, "input exceeds max_bytes"); 0 3 3
925 /* the marks below need four bytes, and a declaration is judged whole */
926 29376 ▶ if (!l->eof && (len < 4 || frx_lex_short(l, 0, "<?xml "))) { l->need_more = 1; return 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (!l->eof && (len < 4 || frx_lex_short(l, 0, "<?xml "))) { l->need_more = 1; return 0; } 1035 28341 29376
if (!l->eof && (len < 4 || frx_lex_short(l, 0, "<?xml "))) { l->need_more = 1; return 0; } 1023 12 1035
if (!l->eof && (len < 4 || frx_lex_short(l, 0, "<?xml "))) { l->need_more = 1; return 0; } 12 1011 1023
927  
928 29352 ▶ if (len >= 2 && ((in[0] == 0xFE && in[1] == 0xFF) || (in[0] == 0xFF && in[1] == 0xFE)))
Branch: 5 branches
BranchTrueFalseTotal
if (len >= 2 && ((in[0] == 0xFE && in[1] == 0xFF) || (in[0] == 0xFF && in[1] == 0xFE))) 29300 52 29352
if (len >= 2 && ((in[0] == 0xFE && in[1] == 0xFF) || (in[0] == 0xFF && in[1] == 0xFE))) 2 29298 29300
if (len >= 2 && ((in[0] == 0xFE && in[1] == 0xFF) || (in[0] == 0xFF && in[1] == 0xFE))) 0 2 2
if (len >= 2 && ((in[0] == 0xFE && in[1] == 0xFF) || (in[0] == 0xFF && in[1] == 0xFE))) 2 29296 29298
if (len >= 2 && ((in[0] == 0xFE && in[1] == 0xFF) || (in[0] == 0xFF && in[1] == 0xFE))) 2 0 2
929 4 return FRX_LEX_FAIL(l, FRX_E_ENCODING, 0, "UTF-16 or UTF-32 byte order mark; only UTF-8 is accepted");
930 29348 ▶ if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF)
Branch: 5 branches
BranchTrueFalseTotal
if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) 29242 106 29348
if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) 4 29238 29242
if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) 1 3 4
if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) 1 0 1
if (len >= 4 && in[0] == 0 && in[1] == 0 && in[2] == 0xFE && in[3] == 0xFF) 1 0 1
931 1 return FRX_LEX_FAIL(l, FRX_E_ENCODING, 0, "UTF-32 byte order mark; only UTF-8 is accepted");
932 29347 ▶ if (len >= 2 && ((in[0] == '<' && in[1] == 0) || (in[0] == 0 && in[1] == '<')))
Branch: 5 branches
BranchTrueFalseTotal
if (len >= 2 && ((in[0] == '<' && in[1] == 0) || (in[0] == 0 && in[1] == '<'))) 29295 52 29347
if (len >= 2 && ((in[0] == '<' && in[1] == 0) || (in[0] == 0 && in[1] == '<'))) 27466 1829 29295
if (len >= 2 && ((in[0] == '<' && in[1] == 0) || (in[0] == 0 && in[1] == '<'))) 27464 2 27466
if (len >= 2 && ((in[0] == '<' && in[1] == 0) || (in[0] == 0 && in[1] == '<'))) 3 29290 29293
if (len >= 2 && ((in[0] == '<' && in[1] == 0) || (in[0] == 0 && in[1] == '<'))) 3 0 3
933 5 return FRX_LEX_FAIL(l, FRX_E_ENCODING, 0, "UTF-16 without a byte order mark; only UTF-8 is accepted");
934 29342 ▶ if (len >= 3 && in[0] == 0xEF && in[1] == 0xBB && in[2] == 0xBF)
Branch: 4 branches
BranchTrueFalseTotal
if (len >= 3 && in[0] == 0xEF && in[1] == 0xBB && in[2] == 0xBF) 29265 77 29342
if (len >= 3 && in[0] == 0xEF && in[1] == 0xBB && in[2] == 0xBF) 709 28556 29265
if (len >= 3 && in[0] == 0xEF && in[1] == 0xBB && in[2] == 0xBF) 708 1 709
if (len >= 3 && in[0] == 0xEF && in[1] == 0xBB && in[2] == 0xBF) 707 1 708
935 707 l->pos = 3;
936  
937 /* the declaration: <?xml S version S? = S? "1.0" (S encoding ...)? (S standalone ...)? S? ?> */
938 29342 ▶ if (frx_lex_starts(l, l->pos, "<?xml") && l->pos + 5 < len && frx_is_s(in[l->pos + 5])) {
Branch: 3 branches
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "<?xml") && l->pos + 5 < len && frx_is_s(in[l->pos + 5])) { 12317 17025 29342
if (frx_lex_starts(l, l->pos, "<?xml") && l->pos + 5 < len && frx_is_s(in[l->pos + 5])) { 12312 5 12317
if (frx_lex_starts(l, l->pos, "<?xml") && l->pos + 5 < len && frx_is_s(in[l->pos + 5])) { 12039 273 12312
939 12039 size_t at = l->pos;
940 12039 l->pos += 5;
941 12039 frx_lex_skip_s(l);
942 12039 ▶ if (!frx_lex_pseudo(l, "version", &v, &vlen))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_pseudo(l, "version", &v, &vlen)) 188 11851 12039
943 188 ▶ return FRX_PROLOG_NEED(l, "the XML declaration must start with version");
Branch: 4 branches
BranchTrueFalseTotal
return FRX_PROLOG_NEED(l, "the XML declaration must start with version"); 110 78 188
return FRX_PROLOG_NEED(l, "the XML declaration must start with version"); 32 78 110
return FRX_PROLOG_NEED(l, "the XML declaration must start with version"); 0 32 32
return FRX_PROLOG_NEED(l, "the XML declaration must start with version"); 0 110 110
944 11851 ▶ if (vlen == 3 && memcmp(v, "1.1", 3) == 0 && l->profile == FRX_PROFILE_FULL) {
Branch: 3 branches
BranchTrueFalseTotal
if (vlen == 3 && memcmp(v, "1.1", 3) == 0 && l->profile == FRX_PROFILE_FULL) { 11829 22 11851
if (vlen == 3 && memcmp(v, "1.1", 3) == 0 && l->profile == FRX_PROFILE_FULL) { 356 11473 11829
if (vlen == 3 && memcmp(v, "1.1", 3) == 0 && l->profile == FRX_PROFILE_FULL) { 354 2 356
945 354 l->version11 = 1; /* full only; strict keeps 0.01's refusal */
946 11497 ▶ } else if (vlen == 3 && memcmp(v, "1.0", 3) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (vlen == 3 && memcmp(v, "1.0", 3) == 0) { 11475 22 11497
} else if (vlen == 3 && memcmp(v, "1.0", 3) == 0) { 11468 7 11475
947 /* the one version both profiles know */
948 31 ▶ } else if (l->profile == FRX_PROFILE_FULL && vlen > 2 && v[0] == '1' && v[1] == '.') {
Branch: 4 branches
BranchTrueFalseTotal
} else if (l->profile == FRX_PROFILE_FULL && vlen > 2 && v[0] == '1' && v[1] == '.') { 14 15 29
} else if (l->profile == FRX_PROFILE_FULL && vlen > 2 && v[0] == '1' && v[1] == '.') { 11 3 14
} else if (l->profile == FRX_PROFILE_FULL && vlen > 2 && v[0] == '1' && v[1] == '.') { 8 3 11
} else if (l->profile == FRX_PROFILE_FULL && vlen > 2 && v[0] == '1' && v[1] == '.') { 8 0 8
949 /* XML 1.0 fifth edition section 2.8: a 1.x this processor does
950 * not know is processed as 1.0; the declared string is kept */
951 size_t k;
952 15 ▶ for (k = 2; k < vlen; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 2; k < vlen; k++) 13 2 15
953 13 ▶ if (v[k] < '0' || v[k] > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (v[k] < '0' || v[k] > '9') 10 3 13
if (v[k] < '0' || v[k] > '9') 3 7 10
954 6 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, at, "only XML version 1.0 is accepted");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, at, "only XML version 1.0 is accepted"); 0 6 6
955 } else {
956 21 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, at, "only XML version 1.0 is accepted");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, at, "only XML version 1.0 is accepted"); 0 21 21
957 }
958 11824 ▶ if (!frx_lex_intern(l, v, vlen, &l->version)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_intern(l, v, vlen, &l->version)) return 0; 0 11824 11824
959 11824 had_s = frx_lex_skip_s(l);
960 11824 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 18 11806 11824
961 18 ▶ return FRX_PROLOG_NEED(l, "unterminated XML declaration");
Branch: 4 branches
BranchTrueFalseTotal
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 18 0 18
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 18 0 18
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 10 8 18
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 0 8 8
962 11806 ▶ if (frx_lex_starts(l, l->pos, "encoding")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "encoding")) { 10034 1772 11806
963 10034 ▶ if (!had_s)
Branch: 1 branch
BranchTrueFalseTotal
if (!had_s) 6 10028 10034
964 6 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected whitespace before encoding in the XML declaration");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected whitespace before encoding in the XML declaration"); 0 6 6
965 10028 ▶ if (!frx_lex_pseudo(l, "encoding", &v, &vlen))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_pseudo(l, "encoding", &v, &vlen)) 78 9950 10028
966 78 ▶ return FRX_PROLOG_NEED(l, "malformed encoding declaration");
Branch: 4 branches
BranchTrueFalseTotal
return FRX_PROLOG_NEED(l, "malformed encoding declaration"); 42 36 78
return FRX_PROLOG_NEED(l, "malformed encoding declaration"); 32 10 42
return FRX_PROLOG_NEED(l, "malformed encoding declaration"); 0 32 32
return FRX_PROLOG_NEED(l, "malformed encoding declaration"); 0 42 42
967 9950 ▶ if (l->profile == FRX_PROFILE_FULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->profile == FRX_PROFILE_FULL) { 2837 7113 9950
968 /* XML 1.0 4.3.3: the declaration must name what the
969 * document arrived in. What arrived was detected by
970 * frx_enc.h and the bytes here are already UTF-8. */
971 2837 ▶ if (!l->enc_override) {
Branch: 1 branch
BranchTrueFalseTotal
if (!l->enc_override) { 2836 1 2837
972 2836 ▶ if (frx_enc_kind_by_name((const char *)v, vlen) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (frx_enc_kind_by_name((const char *)v, vlen) < 0) 28 2808 2836
973 28 ▶ return FRX_LEX_FAIL(l, FRX_E_ENCODING, at,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENCODING, at, 0 28 28
974 "the declared encoding is not one this parser supports "
975 "(UTF-8, UTF-16, ISO-8859-1, US-ASCII)");
976 2808 ▶ if (!frx_enc_declared_matches(l->enc_kind, (const char *)v, vlen))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_enc_declared_matches(l->enc_kind, (const char *)v, vlen)) 7 2801 2808
977 7 ▶ return FRX_LEX_FAIL(l, FRX_E_ENCODING, at,
Branch: 2 branches
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENCODING, at, 5 2 7
return FRX_LEX_FAIL(l, FRX_E_ENCODING, at, 0 7 7
978 l->enc_bom ? "the encoding declaration contradicts the byte order mark"
979 : "the encoding declaration contradicts the encoding the document arrived in");
980 }
981 7113 ▶ } else if (!frx_lex_ieq(v, vlen, "utf-8")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!frx_lex_ieq(v, vlen, "utf-8")) { 22 7091 7113
982 22 ▶ return FRX_LEX_FAIL(l, FRX_E_ENCODING, at, "only the UTF-8 encoding is accepted");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENCODING, at, "only the UTF-8 encoding is accepted"); 0 22 22
983 }
984 9893 had_s = frx_lex_skip_s(l);
985 9893 ▶ if (l->pos >= l->len)
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) 9 9884 9893
986 9 ▶ return FRX_PROLOG_NEED(l, "unterminated XML declaration");
Branch: 4 branches
BranchTrueFalseTotal
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 9 0 9
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 9 0 9
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 4 5 9
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 0 5 5
987 }
988 11656 ▶ if (frx_lex_starts(l, l->pos, "standalone")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "standalone")) { 769 10887 11656
989 769 ▶ if (!had_s)
Branch: 1 branch
BranchTrueFalseTotal
if (!had_s) 3 766 769
990 3 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected whitespace before standalone in the XML declaration");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, "expected whitespace before standalone in the XML declaration"); 0 3 3
991 766 ▶ if (!frx_lex_pseudo(l, "standalone", &v, &vlen))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_pseudo(l, "standalone", &v, &vlen)) 13 753 766
992 13 ▶ return FRX_PROLOG_NEED(l, "malformed standalone declaration");
Branch: 4 branches
BranchTrueFalseTotal
return FRX_PROLOG_NEED(l, "malformed standalone declaration"); 13 0 13
return FRX_PROLOG_NEED(l, "malformed standalone declaration"); 8 5 13
return FRX_PROLOG_NEED(l, "malformed standalone declaration"); 0 8 8
return FRX_PROLOG_NEED(l, "malformed standalone declaration"); 0 13 13
993 753 ▶ if (!((vlen == 3 && memcmp(v, "yes", 3) == 0) || (vlen == 2 && memcmp(v, "no", 2) == 0)))
Branch: 4 branches
BranchTrueFalseTotal
if (!((vlen == 3 && memcmp(v, "yes", 3) == 0) || (vlen == 2 && memcmp(v, "no", 2) == 0))) 719 34 753
if (!((vlen == 3 && memcmp(v, "yes", 3) == 0) || (vlen == 2 && memcmp(v, "no", 2) == 0))) 4 715 719
if (!((vlen == 3 && memcmp(v, "yes", 3) == 0) || (vlen == 2 && memcmp(v, "no", 2) == 0))) 32 6 38
if (!((vlen == 3 && memcmp(v, "yes", 3) == 0) || (vlen == 2 && memcmp(v, "no", 2) == 0))) 5 27 32
994 11 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, at, "malformed standalone declaration");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, at, "malformed standalone declaration"); 0 11 11
995 742 l->standalone = vlen == 3;
996 742 frx_lex_skip_s(l);
997 }
998 11629 ▶ if (!frx_lex_starts(l, l->pos, "?>")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_starts(l, l->pos, "?>")) { 139 11490 11629
999 /* a keyword or the close cut by the end of the input so far */
1000 139 ▶ if (!l->eof && (frx_lex_short(l, l->pos, "encoding") || frx_lex_short(l, l->pos, "standalone")
Branch: 3 branches
BranchTrueFalseTotal
if (!l->eof && (frx_lex_short(l, l->pos, "encoding") || frx_lex_short(l, l->pos, "standalone") 34 105 139
if (!l->eof && (frx_lex_short(l, l->pos, "encoding") || frx_lex_short(l, l->pos, "standalone") 6 28 34
if (!l->eof && (frx_lex_short(l, l->pos, "encoding") || frx_lex_short(l, l->pos, "standalone") 6 0 6
1001 6 ▶ || frx_lex_short(l, l->pos, "?>")))
Branch: 1 branch
BranchTrueFalseTotal
|| frx_lex_short(l, l->pos, "?>"))) 6 0 6
1002 34 l->need_more = 1;
1003 139 ▶ return FRX_PROLOG_NEED(l, "unterminated XML declaration");
Branch: 4 branches
BranchTrueFalseTotal
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 105 34 139
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 1 104 105
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 0 1 1
return FRX_PROLOG_NEED(l, "unterminated XML declaration"); 0 105 105
1004 }
1005 11490 l->pos += 2;
1006 17303 ▶ } else if (!l->eof && frx_lex_short(l, l->pos, "<?xml ")) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (!l->eof && frx_lex_short(l, l->pos, "<?xml ")) { 11 17292 17303
} else if (!l->eof && frx_lex_short(l, l->pos, "<?xml ")) { 0 11 11
1007 0 l->need_more = 1;
1008 0 return 0;
1009 }
1010 28793 return 1;
1011 }
1012  
1013 /* the full profile's form: the transcoder's findings go in before the
1014 * prolog is judged. 0 on refusal, with err set. profile is FRX_PROFILE_*:
1015 * under strict the prolog's checks are 0.01's, byte for byte. The
1016 * expansion budgets start at their defaults; the parse overrides them
1017 * from the extended options after this returns. */
1018 /* the fields every form shares; the input, eof and the prolog are the
1019 * caller's next step */
1020 static void
1021 29193 frx_lex_setup(frx_lex *l, const frx_opts *o, int profile, frx_arena *arena,
1022 int enc_kind, int enc_bom, int enc_override)
1023 {
1024 29193 memset(l, 0, sizeof *l);
1025 29193 l->arena = arena;
1026 29193 l->tok_arena = arena;
1027 29193 l->profile = profile;
1028 29193 l->enc_kind = enc_kind;
1029 29193 l->enc_bom = enc_bom;
1030 29193 l->enc_override = enc_override;
1031 29193 ▶ l->prolog_max_bytes = o ? o->max_bytes : 0;
Branch: 1 branch
BranchTrueFalseTotal
l->prolog_max_bytes = o ? o->max_bytes : 0; 29193 0 29193
1032 29193 l->max_expansion_bytes = FRX_DEFAULT_MAX_EXPANSION_BYTES;
1033 29193 l->max_entity_depth = FRX_DEFAULT_MAX_ENTITY_DEPTH;
1034 29193 l->max_expansion_ratio = FRX_DEFAULT_MAX_EXPANSION_RATIO;
1035 29193 l->max_fetches = FRX_DEFAULT_MAX_FETCHES;
1036 29193 l->max_token_bytes = FRX_DEFAULT_MAX_TOKEN_BYTES;
1037 29193 l->cur_base.p = "";
1038 29193 l->cur_base.len = 0;
1039 29193 frx_err_set(&l->err, FRX_OK, 0, NULL);
1040 29193 frx_buf_init(&l->tmp);
1041 29193 }
1042  
1043 static int
1044 124 frx_lex_init_enc(frx_lex *l, const unsigned char *in, size_t len,
1045 const frx_opts *o, int profile, frx_arena *arena,
1046 int enc_kind, int enc_bom, int enc_override)
1047 {
1048 124 frx_lex_setup(l, o, profile, arena, enc_kind, enc_bom, enc_override);
1049 124 l->in = in;
1050 124 l->len = len;
1051 124 l->doc_len = len;
1052 124 l->eof = 1;
1053 124 ▶ if (!frx_lex_prolog(l)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_prolog(l)) return 0; 12 112 124
1054 112 l->prolog_done = 1;
1055 112 return 1;
1056 }
1057  
1058 /* the strict form, and the full form over bytes already known to be UTF-8 */
1059 static int
1060 124 frx_lex_init(frx_lex *l, const unsigned char *in, size_t len,
1061 const frx_opts *o, int profile, frx_arena *arena)
1062 {
1063 124 return frx_lex_init_enc(l, in, len, o, profile, arena, FRX_ENC_UTF8, 0, 0);
1064 }
1065  
1066 /* the streaming form: no bytes yet, eof clear, the prolog run by the
1067 * first frx_lex_next that has enough of them; tok_arena is the reader's */
1068 static void
1069 881 frx_lex_init_stream(frx_lex *l, const frx_opts *o, int profile, frx_arena *arena,
1070 frx_arena *tok_arena, int enc_kind, int enc_bom, int enc_override)
1071 {
1072 881 frx_lex_setup(l, o, profile, arena, enc_kind, enc_bom, enc_override);
1073 881 l->tok_arena = tok_arena;
1074 881 l->in = (const unsigned char *)"";
1075 881 l->len = 0;
1076 881 l->eof = 0;
1077 881 }
1078  
1079 static void
1080 29208 frx_lex_free(frx_lex *l)
1081 {
1082 29208 free(l->attrs);
1083 29208 l->attrs = NULL;
1084 29208 l->n_attrs = l->cap_attrs = 0;
1085 29208 free(l->frames);
1086 29208 l->frames = NULL;
1087 29208 l->n_frames = l->cap_frames = 0;
1088 29208 free(l->undecl);
1089 29208 l->undecl = NULL;
1090 29208 l->n_undecl = l->cap_undecl = 0;
1091 29208 free(l->cm_stack);
1092 29208 free(l->cm_frame);
1093 29208 free(l->cond_frame);
1094 29208 l->cm_stack = NULL;
1095 29208 l->cm_frame = NULL;
1096 29208 l->cond_frame = NULL;
1097 29208 l->cm_cap = 0;
1098 29208 l->cond_cap = 0;
1099 29208 frx_buf_free(&l->tmp);
1100 29208 }
1101  
1102 /* one token at pos, which holds a < ; 1 with *t filled */
1103 static int
1104 6522525 frx_lex_markup(frx_lex *l, frx_tok *t)
1105 {
1106 6522525 ▶ if (frx_lex_starts(l, l->pos, "</")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "</")) { 2977381 3545144 6522525
1107 2977381 const frx_frame_in *f = frx_lex_top_frame(l);
1108 2977381 l->pos += 2;
1109 2977381 ▶ if (!frx_lex_end_tag(l, t)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_end_tag(l, t)) return 0; 2933 2974448 2977381
1110 2974448 ▶ if (f && l->tag_depth <= f->depth_at_push)
Branch: 2 branches
BranchTrueFalseTotal
if (f && l->tag_depth <= f->depth_at_push) 120 2974328 2974448
if (f && l->tag_depth <= f->depth_at_push) 3 117 120
1111 3 ▶ return FRX_LEX_FAIL(l, FRX_E_ENTITY, l->tok_start,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_ENTITY, l->tok_start, 1 2 3
1112 "an entity's replacement text must not close an element opened outside it");
1113 2974445 l->tag_depth--;
1114 2974445 return 1;
1115 }
1116 3545144 ▶ if (frx_lex_starts(l, l->pos, "<!--")) { l->pos += 4; return frx_lex_comment(l, t); }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "<!--")) { l->pos += 4; return frx_lex_comment(l, t); } 6228 3538916 3545144
1117 3538916 ▶ if (frx_lex_starts(l, l->pos, "<![CDATA[")) { l->pos += 9; return frx_lex_cdata(l, t); }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "<![CDATA[")) { l->pos += 9; return frx_lex_cdata(l, t); } 52165 3486751 3538916
1118 3486751 ▶ if (frx_lex_starts(l, l->pos, "<!")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "<!")) { 6096 3480655 3486751
1119 6096 ▶ if (!l->eof && !l->n_frames
Branch: 2 branches
BranchTrueFalseTotal
if (!l->eof && !l->n_frames 1014 5082 6096
if (!l->eof && !l->n_frames 1014 0 1014
1120 1014 ▶ && (frx_lex_short(l, l->pos, "<!--") || frx_lex_short(l, l->pos, "<![CDATA[")
Branch: 2 branches
BranchTrueFalseTotal
&& (frx_lex_short(l, l->pos, "<!--") || frx_lex_short(l, l->pos, "<![CDATA[") 990 24 1014
&& (frx_lex_short(l, l->pos, "<!--") || frx_lex_short(l, l->pos, "<![CDATA[") 978 12 990
1121 978 ▶ || frx_lex_short(l, l->pos, "<!DOCTYPE"))) {
Branch: 1 branch
BranchTrueFalseTotal
|| frx_lex_short(l, l->pos, "<!DOCTYPE"))) { 12 966 978
1122 48 l->need_more = 1; /* the input ends inside the mark */
1123 48 return 0;
1124 }
1125 6048 ▶ if (l->profile == FRX_PROFILE_FULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->profile == FRX_PROFILE_FULL) { 5937 111 6048
1126 5937 ▶ if (frx_lex_starts(l, l->pos, "<!DOCTYPE")) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "<!DOCTYPE")) { 5894 43 5937
1127 5894 ▶ if (!l->eof && !l->n_frames && !frx_lex_doctype_complete(l)) { l->need_more = 1; return 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (!l->eof && !l->n_frames && !frx_lex_doctype_complete(l)) { l->need_more = 1; return 0; } 966 4928 5894
if (!l->eof && !l->n_frames && !frx_lex_doctype_complete(l)) { l->need_more = 1; return 0; } 966 0 966
if (!l->eof && !l->n_frames && !frx_lex_doctype_complete(l)) { l->need_more = 1; return 0; } 158 808 966
1128 5736 return frx_dtd_doctype(l, t);
1129 }
1130 43 ▶ return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_SYNTAX, l->pos, 0 43 43
1131 "only a DOCTYPE, a comment or CDATA may follow <! here");
1132 }
1133 111 ▶ return FRX_LEX_FAIL(l, FRX_E_DOCTYPE, l->pos,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_DOCTYPE, l->pos, 0 111 111
1134 "DOCTYPE and every other declaration are refused; only comments and CDATA may follow <!");
1135 }
1136 3480655 ▶ if (frx_lex_starts(l, l->pos, "<?")) { l->pos += 2; return frx_lex_pi(l, t); }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_lex_starts(l, l->pos, "<?")) { l->pos += 2; return frx_lex_pi(l, t); } 5244 3475411 3480655
1137 3475411 l->pos += 1;
1138 3475411 ▶ if (!frx_lex_start_tag(l, t)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_start_tag(l, t)) return 0; 11021 3464390 3475411
1139 3464390 ▶ if (t->kind == FRX_TOK_START) l->tag_depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (t->kind == FRX_TOK_START) l->tag_depth++; 3048552 415838 3464390
1140 3464390 return 1;
1141 }
1142  
1143 /* the next token: 1 with *t filled, 0 at EOF (t->kind is FRX_TOK_EOF),
1144 * when more input is needed (FRX_TOK_MORE; the position is back at the
1145 * token's start, so the caller appends bytes and calls again) or on
1146 * refusal (l->failed, l->err). A frame that ends between tokens is popped
1147 * here, and an empty text run (an entity whose text opens with markup)
1148 * is skipped rather than reported. */
1149 static int
1150 13536304 frx_lex_next(frx_lex *l, frx_tok *t)
1151 {
1152 2176517 for (;;) {
1153 size_t start;
1154 int ok;
1155 15712821 memset(t, 0, sizeof *t);
1156 15712821 ▶ if (l->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (l->failed) return 0; 0 15712821 15712821
1157 15712821 ▶ if (!l->prolog_done) {
Branch: 1 branch
BranchTrueFalseTotal
if (!l->prolog_done) { 1067 15711754 15712821
1158 1067 l->need_more = 0;
1159 1067 ▶ if (!frx_lex_prolog(l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_prolog(l)) { 186 881 1067
1160 186 ▶ if (l->need_more && !l->failed) { l->pos = 0; t->kind = FRX_TOK_MORE; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (l->need_more && !l->failed) { l->pos = 0; t->kind = FRX_TOK_MORE; return 0; } 186 0 186
if (l->need_more && !l->failed) { l->pos = 0; t->kind = FRX_TOK_MORE; return 0; } 186 0 186
1161 0 return 0;
1162 }
1163 881 l->prolog_done = 1;
1164 }
1165 15712635 ▶ if (l->pos >= l->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->pos >= l->len) { 2187150 13525485 15712635
1166 2187150 ▶ if (l->n_frames) { if (!frx_entity_pop(l)) return 0; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (l->n_frames) { if (!frx_entity_pop(l)) return 0; continue; } 2176521 10629 2187150
if (l->n_frames) { if (!frx_entity_pop(l)) return 0; continue; } 4 2176517 2176521
1167 10629 ▶ if (!l->eof) { t->kind = FRX_TOK_MORE; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!l->eof) { t->kind = FRX_TOK_MORE; return 0; } 185 10444 10629
1168 10444 t->offset = l->base + l->pos;
1169 10444 t->kind = FRX_TOK_EOF;
1170 10444 return 0;
1171 }
1172 13525485 l->tok_start = l->pos;
1173 13525485 l->need_more = 0;
1174 13525485 start = l->pos;
1175 13525485 ▶ t->offset = FRX_LEX_ABS(l, l->pos);
Branch: 1 branch
BranchTrueFalseTotal
t->offset = FRX_LEX_ABS(l, l->pos); 686 13524799 13525485
1176 13525485 ▶ if (l->n_frames && l->frames[l->n_frames - 1].entity)
Branch: 2 branches
BranchTrueFalseTotal
if (l->n_frames && l->frames[l->n_frames - 1].entity) 4130537 9394948 13525485
if (l->n_frames && l->frames[l->n_frames - 1].entity) 4130537 0 4130537
1177 4130537 t->entity = *frx_entity_name_of(l->frames[l->n_frames - 1].entity);
1178 else
1179 9394948 t->entity = FRX_EMPTY_STR;
1180  
1181 13525485 ▶ if (l->in[l->pos] != '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (l->in[l->pos] != '<') { 7002960 6522525 13525485
1182 7002960 ok = frx_lex_text(l, t);
1183 /* a run with no characters is not reported, unless a
1184 * reference stood in it: an entity whose replacement text is
1185 * empty leaves nothing behind, and an element declared EMPTY
1186 * may not hold even that (erratum E15, case E15a) */
1187 7002960 ▶ if (ok && t->value.len == 0 && !(t->text_flags & FRX_TEXT_REF_HERE)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (ok && t->value.len == 0 && !(t->text_flags & FRX_TEXT_REF_HERE)) continue; 7000950 2010 7002960
if (ok && t->value.len == 0 && !(t->text_flags & FRX_TEXT_REF_HERE)) continue; 2173337 4827613 7000950
if (ok && t->value.len == 0 && !(t->text_flags & FRX_TEXT_REF_HERE)) continue; 0 2173337 2173337
1188 } else {
1189 6522525 ok = frx_lex_markup(l, t);
1190 }
1191 13525485 ▶ if (ok) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ok) return 1; 13507128 18357 13525485
1192 18357 ▶ if (l->need_more && !l->failed) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->need_more && !l->failed) { 2983 15374 18357
if (l->need_more && !l->failed) { 2983 0 2983
1193 2983 ▶ if (l->len - start > l->max_token_bytes)
Branch: 1 branch
BranchTrueFalseTotal
if (l->len - start > l->max_token_bytes) 2 2981 2983
1194 2 ▶ return FRX_LEX_FAIL(l, FRX_E_TOO_LARGE, start, "a token longer than max_token_bytes");
Branch: 1 branch
BranchTrueFalseTotal
return FRX_LEX_FAIL(l, FRX_E_TOO_LARGE, start, "a token longer than max_token_bytes"); 0 2 2
1195 2981 l->pos = start; /* retry it whole when more has arrived */
1196 2981 l->n_attrs = 0;
1197 2981 t->kind = FRX_TOK_MORE;
1198 }
1199 18355 return 0;
1200 }
1201 }
1202  
1203 #endif /* FRX_LEX_H */
« xs/document.xs ↑ Summary include/frx/frx_validate.h »