include/frx/frx_parse.h

filter: Statementstmt 98.9% 364 / 368 Branchbran 89.0% 269 / 302 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.4% 633 / 670 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_ns.h ↑ Summary include/frx/frx_tree.h »
1 #ifndef FRX_PARSE_H
2 #define FRX_PARSE_H
3  
4 /* Tokens become events; a sink makes them a tree, or a stream.
5 *
6 * An iterative driver with an explicit open-element stack (rule 2): each
7 * open element is a record in a malloc'd array, so depth costs no C
8 * stack. Depth is the stack height; the root is depth 1, an opening tag
9 * at max_depth + 1 is refused, and the default of 256 applies when
10 * max_depth is 0.
11 *
12 * The driver resolves names and namespaces (frx_ns.h: the scope chain,
13 * interning, the rules), enforces the well-formedness constraints (one
14 * root, matching end tags, duplicate attributes, unbound prefixes), and
15 * merges text: character data accumulates until a non-text token
16 * arrives, then goes to the sink as one text event, which is how adjacent
17 * text and CDATA merge in O(n). Whitespace outside the root is dropped;
18 * anything else outside the root is refused.
19 *
20 * What it produces goes to a sink (frx_sink.h): the tree sink the codec
21 * uses, the reader's queue, or a subtree capture. The driver allocates an
22 * event's arrays in tok_arena and interns namespace URIs in arena; in
23 * tree mode both are the document's, so the tree sink keeps every
24 * pointer, and 0.01's tree is what it always was (t/30 pins its bytes).
25 * A reader gives a token arena it releases per record.
26 *
27 * The driver is stepped: frx_parse_step reads one token and calls the
28 * sink for what it means, and says whether the input ended, ended
29 * inside a token (more bytes wanted, the lexer having rewound), or was
30 * refused. The whole-document parse is that step in a loop.
31 *
32 * Under the full profile the lexer hands over a DOCTYPE token after it
33 * has read the subset; the driver's part is that it stands before the
34 * root, and it passes the records on. Defaulted attributes arrive in the
35 * token's attribute list like written ones, flagged, so a defaulted
36 * xmlns is a declaration here like any other.
37 *
38 * The walkers the object layer wraps - find, descendants, text - live
39 * here, iterative, so that the XS above is wrappers and nothing else.
40 *
41 * Needs frx_err.h, frx_arena.h, frx_buf.h, frx_utf8.h, frx_tree.h,
42 * frx_lex.h, frx_dtd.h, frx_entity.h, frx_ns.h; frx_sink.h is included
43 * from here, after the types it needs. */
44  
45 #define FRX_DEFAULT_MAX_DEPTH 256
46  
47 /* the tree sink's open-element frame, in the document's arena */
48 typedef struct frx_frame {
49 frx_node *node;
50 struct frx_frame *up;
51 } frx_frame;
52  
53 /* a start event: the element with its names resolved */
54 typedef struct frx_ev_start {
55 frx_str qname;
56 frx_str prefix;
57 frx_str local;
58 frx_str ns;
59 const frx_attr *attrs; /* document order, namespaces resolved; never xmlns */
60 int n_attrs;
61 const frx_nsdecl *decls;
62 int n_decls;
63 const frx_scope *scope; /* what binds prefixes for it and its descendants */
64 size_t offset;
65 int empty; /* <a/>: no end event follows */
66 int xmlns_xml; /* an xmlns:xml declaration was written and dropped */
67 } frx_ev_start;
68  
69 typedef struct frx_parse frx_parse;
70  
71 typedef struct frx_sink {
72 int (*start)(frx_parse *p, void *ud, const frx_ev_start *ev);
73 int (*end)(frx_parse *p, void *ud, const frx_str *qname, size_t offset);
74 int (*text)(frx_parse *p, void *ud, const char *s, size_t n,
75 const frx_span *spans, int n_spans, size_t offset, const frx_str *entity,
76 int text_flags);
77 int (*comment)(frx_parse *p, void *ud, const frx_str *value, size_t offset);
78 int (*pi)(frx_parse *p, void *ud, const frx_str *target, const frx_str *data, size_t offset);
79 int (*doctype)(frx_parse *p, void *ud, const frx_doctype *dt, const frx_dtd *dtd);
80 int (*finish)(frx_parse *p, void *ud); /* at a successful end of input; may be NULL */
81 } frx_sink;
82  
83 /* an open element */
84 typedef struct frx_open {
85 frx_str qname;
86 const frx_scope *scope;
87 size_t offset;
88 } frx_open;
89  
90 struct frx_parse {
91 frx_lex lex;
92 frx_err err;
93 int failed;
94 int profile; /* FRX_PROFILE_*; read here and in frx_lex.h, nowhere else */
95 int validate; /* full: the root element name must match the DOCTYPE (a VC) */
96 const frx_sink *sink;
97 void *ud;
98 frx_arena *arena; /* interned namespace URIs: for the parse's life */
99 frx_arena *tok_arena; /* an event's arrays: a reader may release them per record */
100 frx_open *stack; /* the open elements, malloc'd */
101 int depth;
102 int cap_stack;
103 int max_depth;
104 int seen_root;
105 const frx_doctype *doctype;
106 frx_intern intern;
107 frx_buf text; /* pending character data */
108 int have_text;
109 size_t text_offset;
110 frx_str text_entity; /* the entity the pending text came from */
111 int text_flags; /* FRX_TEXT_REF_*, ored over the pending run */
112 int split_entities; /* a text event per entity boundary (the reader); else merged */
113 frx_span *spans; /* full: the CDATA sections in the pending text; malloc'd */
114 int n_spans;
115 int cap_spans;
116 };
117  
118 enum { FRX_STEP_FAIL = 0, FRX_STEP_OK = 1, FRX_STEP_MORE = 2, FRX_STEP_EOF = 3 };
119  
120 /* A size-aware read of a consumer's frx_opts_ex: the defaults, then as
121 * many bytes as the consumer's struct holds (never more than ours), so a
122 * struct from an older or a newer header is read whole and never past its
123 * end. Every reader of the extended options goes through this. */
124 static void
125 29084 frx_opts_ex_norm(const frx_opts_ex *in, frx_opts_ex *out)
126 {
127 29084 memset(out, 0, sizeof *out);
128 29084 out->size = sizeof *out;
129 29084 out->profile = FRX_PROFILE_STRICT;
130 29084 ▶ if (in) {
Branch: 1 branch
BranchTrueFalseTotal
if (in) { 29084 0 29084
131 29084 size_t n = in->size < sizeof *out ? in->size : sizeof *out;
132 29084 ▶ if (n > sizeof(size_t)) memcpy((char *)out + sizeof(size_t),
Branch: 1 branch
BranchTrueFalseTotal
if (n > sizeof(size_t)) memcpy((char *)out + sizeof(size_t), 29084 0 29084
133 (const char *)in + sizeof(size_t),
134 n - sizeof(size_t));
135 29084 out->size = sizeof *out;
136 }
137 29084 }
138  
139 /* frx_validate.h, which follows this file because it walks the finished
140 * tree with the walkers defined at the end of it */
141 static int frx_validate_doc(frx_doc *doc, int collect, frx_err *err);
142 static int frx_xinclude_doc(frx_doc *doc, const frx_opts_ex *oe, frx_err *err);
143  
144 #define FRX_PARSE_FAIL(p, c, off, what) \
145 (frx_err_set(&(p)->err, (c), (off), (what)), (p)->failed = 1, 0)
146  
147 static int
148 0 frx_parse_nomem(frx_parse *p, size_t off)
149 {
150 0 return FRX_PARSE_FAIL(p, FRX_E_NOMEM, off, "out of memory");
151 }
152  
153 #include "frx_sink.h"
154  
155 /* ---- the driver ------------------------------------------------------- */
156  
157 /* the driver over options already normalised; the input and the lexer
158 * are set separately, because a reader has no bytes yet */
159 static void
160 29084 frx_parse_init(frx_parse *p, const frx_opts_ex *oe, frx_arena *arena, frx_arena *tok_arena,
161 const frx_sink *sink, void *ud)
162 {
163 29084 memset(p, 0, sizeof *p);
164 29084 p->profile = oe->profile == FRX_PROFILE_FULL ? FRX_PROFILE_FULL : FRX_PROFILE_STRICT;
165 29084 p->validate = oe->validate != 0;
166 29084 ▶ p->max_depth = (oe->base.max_depth > 0) ? oe->base.max_depth : FRX_DEFAULT_MAX_DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
p->max_depth = (oe->base.max_depth > 0) ? oe->base.max_depth : FRX_DEFAULT_MAX_DEPTH; 8 29076 29084
167 29084 p->sink = sink;
168 29084 p->ud = ud;
169 29084 p->arena = arena;
170 29084 p->tok_arena = tok_arena;
171 29084 p->text_entity = FRX_EMPTY_STR;
172 29084 frx_intern_init(&p->intern);
173 29084 frx_buf_init(&p->text);
174 29084 }
175  
176 /* what the extended options say to the lexer, once it exists */
177 static void
178 29069 frx_parse_wire_lex(frx_parse *p, const frx_opts_ex *oe)
179 {
180 29069 frx_lex *l = &p->lex;
181 29069 ▶ if (oe->max_expansion_bytes) l->max_expansion_bytes = oe->max_expansion_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if (oe->max_expansion_bytes) l->max_expansion_bytes = oe->max_expansion_bytes; 4 29065 29069
182 29069 ▶ if (oe->max_entity_depth) l->max_entity_depth = oe->max_entity_depth;
Branch: 1 branch
BranchTrueFalseTotal
if (oe->max_entity_depth) l->max_entity_depth = oe->max_entity_depth; 4 29065 29069
183 29069 ▶ if (oe->max_expansion_ratio) l->max_expansion_ratio = oe->max_expansion_ratio;
Branch: 1 branch
BranchTrueFalseTotal
if (oe->max_expansion_ratio) l->max_expansion_ratio = oe->max_expansion_ratio; 4 29065 29069
184 29069 ▶ if (oe->max_fetches) l->max_fetches = oe->max_fetches;
Branch: 1 branch
BranchTrueFalseTotal
if (oe->max_fetches) l->max_fetches = oe->max_fetches; 7 29062 29069
185 29069 ▶ if (oe->max_token_bytes) l->max_token_bytes = oe->max_token_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if (oe->max_token_bytes) l->max_token_bytes = oe->max_token_bytes; 2 29067 29069
186 29069 l->resolve = oe->resolve;
187 29069 l->resolve_ud = oe->resolve_ud;
188 29069 l->max_bytes = oe->base.max_bytes;
189 29069 l->validate = p->validate;
190 29069 ▶ if (oe->base_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (oe->base_uri) { 5580 23489 29069
191 5580 l->cur_base.p = oe->base_uri;
192 5580 l->cur_base.len = strlen(oe->base_uri);
193 }
194 29069 }
195  
196 static void
197 29084 frx_parse_free(frx_parse *p)
198 {
199 29084 frx_lex_free(&p->lex);
200 29084 frx_intern_free(&p->intern);
201 29084 frx_buf_free(&p->text);
202 29084 free(p->spans);
203 29084 free(p->stack);
204 29084 p->spans = NULL;
205 29084 p->stack = NULL;
206 29084 }
207  
208 static const frx_scope *
209 3464300 frx_parse_scope(const frx_parse *p)
210 {
211 3464300 ▶ return p->depth ? p->stack[p->depth - 1].scope : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return p->depth ? p->stack[p->depth - 1].scope : NULL; 3439726 24574 3464300
212 }
213  
214 /* the pending text goes to the sink as one event */
215 static int
216 6447767 frx_parse_flush_text(frx_parse *p)
217 {
218 int ok;
219 6447767 ▶ if (!p->have_text) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!p->have_text) return 1; 3617972 2829795 6447767
220 2829795 p->have_text = 0;
221 2829795 ▶ if (p->text.failed) return frx_parse_nomem(p, p->text_offset);
Branch: 1 branch
BranchTrueFalseTotal
if (p->text.failed) return frx_parse_nomem(p, p->text_offset); 0 2829795 2829795
222 2829795 ok = p->sink->text(p, p->ud, p->text.p ? p->text.p : "", p->text.len,
223 2829795 ▶ p->spans, p->n_spans, p->text_offset, &p->text_entity,
Branch: 1 branch
BranchTrueFalseTotal
p->spans, p->n_spans, p->text_offset, &p->text_entity, 2829795 0 2829795
224 p->text_flags);
225 2829795 p->n_spans = 0;
226 2829795 p->text_flags = 0;
227 2829795 return ok;
228 }
229  
230 static int
231 7052975 frx_parse_text(frx_parse *p, const frx_tok *t)
232 {
233 7052975 ▶ if (!p->depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->depth) { 27950 7025025 7052975
234 27950 ▶ if (t->cdata) /* not Misc, whatever it holds */
Branch: 1 branch
BranchTrueFalseTotal
if (t->cdata) /* not Misc, whatever it holds */ 4 27946 27950
235 4 return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, t->offset, "a CDATA section is not allowed outside the root element");
236 27946 ▶ if (frx_str_is_s(&t->value)) return 1; /* whitespace outside the root */
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_is_s(&t->value)) return 1; /* whitespace outside the root */ 27914 32 27946
237 32 ▶ return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, t->offset,
Branch: 1 branch
BranchTrueFalseTotal
return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, t->offset, 9 23 32
238 p->seen_root ? "content after the root element"
239 : "content before the root element");
240 }
241 7025025 ▶ if (p->have_text && p->split_entities && !frx_str_eq2(&p->text_entity, &t->entity)) {
Branch: 3 branches
BranchTrueFalseTotal
if (p->have_text && p->split_entities && !frx_str_eq2(&p->text_entity, &t->entity)) { 4186159 2838866 7025025
if (p->have_text && p->split_entities && !frx_str_eq2(&p->text_entity, &t->entity)) { 56 4186103 4186159
if (p->have_text && p->split_entities && !frx_str_eq2(&p->text_entity, &t->entity)) { 20 36 56
242 20 ▶ if (!frx_parse_flush_text(p)) return 0; /* a reader wants the entity boundary */
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_parse_flush_text(p)) return 0; /* a reader wants the entity boundary */ 0 20 20
243 }
244 7025025 ▶ if (!p->have_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->have_text) { 2838886 4186139 7025025
245 2838886 p->have_text = 1;
246 2838886 p->text_offset = t->offset;
247 2838886 p->text_entity = t->entity;
248 2838886 p->text.len = 0;
249 2838886 p->n_spans = 0;
250 2838886 p->text_flags = 0;
251 }
252 /* E15, and only this far: a reference expanded while content was being
253 * read taints the run. An entity's replacement text does not, because
254 * the entity is a storage unit and its text stands directly where the
255 * reference was - which is why the suite calls E15e and E15f valid and
256 * E15g and E15h invalid, the difference between them being whether the
257 * character reference was expanded into the entity's literal value at
258 * declaration (section 4.5) or read as a reference here. */
259 7025025 p->text_flags |= t->text_flags;
260 7025025 ▶ if (t->cdata && p->profile == FRX_PROFILE_FULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (t->cdata && p->profile == FRX_PROFILE_FULL) { 52036 6972989 7025025
if (t->cdata && p->profile == FRX_PROFILE_FULL) { 209 51827 52036
261 209 ▶ if (p->n_spans == p->cap_spans) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->n_spans == p->cap_spans) { 148 61 209
262 148 ▶ int ncap = p->cap_spans ? p->cap_spans * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = p->cap_spans ? p->cap_spans * 2 : 4; 0 148 148
263 148 frx_span *ns = (frx_span *)realloc(p->spans, (size_t)ncap * sizeof *ns);
264 148 ▶ if (!ns) return frx_parse_nomem(p, t->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!ns) return frx_parse_nomem(p, t->offset); 0 148 148
265 148 p->spans = ns; p->cap_spans = ncap;
266 }
267 209 p->spans[p->n_spans].offset = p->text.len;
268 209 p->spans[p->n_spans].len = t->value.len;
269 209 p->n_spans++;
270 }
271 7025025 frx_buf_append_n(&p->text, t->value.p, t->value.len);
272 7025025 return 1;
273 }
274  
275 /* a START or EMPTY token: declarations, the scope, the element, its
276 * attributes with namespaces resolved, the duplicate checks; then the
277 * sink */
278 static int
279 3464300 frx_parse_element(frx_parse *p, const frx_tok *t)
280 {
281 frx_ev_start ev;
282 3464300 frx_nsdecl *decls = NULL;
283 3464300 frx_attr *attrs = NULL;
284 3464300 frx_scope *scope = NULL;
285 3464300 const frx_scope *parent_scope = frx_parse_scope(p);
286 3464300 int n_decls = 0, n_attrs = 0, i, j;
287  
288 3464300 ▶ if (!frx_parse_flush_text(p)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_parse_flush_text(p)) return 0; 0 3464300 3464300
289  
290 3464300 ▶ if (!p->depth && p->seen_root)
Branch: 2 branches
BranchTrueFalseTotal
if (!p->depth && p->seen_root) 24574 3439726 3464300
if (!p->depth && p->seen_root) 10 24564 24574
291 10 return FRX_PARSE_FAIL(p, FRX_E_ROOT, t->offset, "more than one root element");
292 /* VC: Root Element Type is frx_validate.h's, with every other
293 * validity constraint, so that `collect` can gather it too */
294 3464290 ▶ if (p->depth + 1 > p->max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (p->depth + 1 > p->max_depth) 5 3464285 3464290
295 5 return FRX_PARSE_FAIL(p, FRX_E_TOO_DEEP, t->offset, "nesting deeper than max_depth");
296  
297 3464285 memset(&ev, 0, sizeof ev);
298 3464285 ev.qname = t->name;
299 3464285 ev.offset = t->offset;
300 3464285 ev.empty = t->kind == FRX_TOK_EMPTY;
301  
302 /* declarations first: they bind the element's own prefix */
303 5492507 ▶ for (i = 0; i < t->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n_attrs; i++) { 2028222 3464285 5492507
304 frx_str prefix;
305 2028222 ▶ if (frx_ns_is_decl(&t->attrs[i].name, &prefix)) n_decls++;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_ns_is_decl(&t->attrs[i].name, &prefix)) n_decls++; 54281 1973941 2028222
306 }
307 3464285 ▶ if (n_decls) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_decls) { 35448 3428837 3464285
308 35448 decls = (frx_nsdecl *)frx_arena_alloc(p->tok_arena, (size_t)n_decls * sizeof *decls);
309 35448 scope = (frx_scope *)frx_arena_alloc(p->tok_arena, sizeof *scope);
310 35448 ▶ if (!decls || !scope) return frx_parse_nomem(p, t->offset);
Branch: 2 branches
BranchTrueFalseTotal
if (!decls || !scope) return frx_parse_nomem(p, t->offset); 35448 0 35448
if (!decls || !scope) return frx_parse_nomem(p, t->offset); 0 35448 35448
311 35448 n_decls = 0;
312 149703 ▶ for (i = 0; i < t->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n_attrs; i++) { 114343 35360 149703
313 114343 const frx_lex_attr *a = &t->attrs[i];
314 frx_str prefix;
315 114343 const frx_str *v = &a->value;
316 114466 ▶ if (!frx_ns_is_decl(&a->name, &prefix)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_ns_is_decl(&a->name, &prefix)) continue; 60072 54271 114343
317 54271 ▶ if (frx_str_eq(&prefix, "xmlns", 5))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(&prefix, "xmlns", 5)) 4 54267 54271
318 88 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset, "the xmlns prefix cannot be declared");
319 54267 ▶ if (frx_str_eq(v, FRX_XMLNS_NS, sizeof FRX_XMLNS_NS - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(v, FRX_XMLNS_NS, sizeof FRX_XMLNS_NS - 1)) 3 54264 54267
320 3 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset, "the xmlns namespace name cannot be bound");
321 54264 ▶ if (frx_str_eq(&prefix, "xml", 3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(&prefix, "xml", 3)) { 162 54102 54264
322 162 ▶ if (!frx_str_eq(v, FRX_XML_NS, sizeof FRX_XML_NS - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_str_eq(v, FRX_XML_NS, sizeof FRX_XML_NS - 1)) 39 123 162
323 39 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset, "the xml prefix is bound to its own namespace and no other");
324 123 ev.xmlns_xml = 1; /* validation still has to see it */
325 123 continue; /* accepted, and dropped */
326 }
327 54102 ▶ if (frx_str_eq(v, FRX_XML_NS, sizeof FRX_XML_NS - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(v, FRX_XML_NS, sizeof FRX_XML_NS - 1)) 6 54096 54102
328 6 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset, "only the xml prefix may be bound to the xml namespace");
329 /* Namespaces 1.1 section 5 allows xmlns:p="" and Namespaces
330 * 1.0 does not. Which one applies is decided by the document's
331 * XML version, because Namespaces 1.1 is the companion of XML
332 * 1.1 and says so; a 1.0 document undeclaring a prefix is
333 * still refused, and so is every strict-profile document,
334 * which never sets version11. */
335 54096 ▶ if (prefix.len && !v->len && !p->lex.version11)
Branch: 3 branches
BranchTrueFalseTotal
if (prefix.len && !v->len && !p->lex.version11) 47056 7040 54096
if (prefix.len && !v->len && !p->lex.version11) 5 47051 47056
if (prefix.len && !v->len && !p->lex.version11) 2 3 5
336 2 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset,
337 "a prefix cannot be undeclared; that is Namespaces 1.1, which needs an XML 1.1 document");
338 54094 ▶ if (!frx_ns_is_absolute(v->p, v->len))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_ns_is_absolute(v->p, v->len)) 34 54060 54094
339 34 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset, "a relative namespace URI is refused; canonicalisation is undefined over it");
340 79340 ▶ for (j = 0; j < n_decls; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n_decls; j++) 25280 54060 79340
341 25280 ▶ if (frx_str_eq2(&decls[j].prefix, &prefix))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&decls[j].prefix, &prefix)) 0 25280 25280
342 0 return FRX_PARSE_FAIL(p, FRX_E_DUP_ATTR, a->offset, "prefix declared twice on one element");
343 54060 decls[n_decls].prefix = prefix;
344 54060 decls[n_decls].uri = frx_intern_uri(&p->intern, p->arena, v->p, v->len);
345 54060 ▶ if (v->len && !decls[n_decls].uri.p) return frx_parse_nomem(p, a->offset);
Branch: 2 branches
BranchTrueFalseTotal
if (v->len && !decls[n_decls].uri.p) return frx_parse_nomem(p, a->offset); 50874 3186 54060
if (v->len && !decls[n_decls].uri.p) return frx_parse_nomem(p, a->offset); 0 50874 50874
346 54060 n_decls++;
347 }
348 }
349 3464197 ▶ if (n_decls) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_decls) { 35237 3428960 3464197
350 35237 scope->up = parent_scope;
351 35237 scope->decls = decls;
352 35237 scope->n_decls = n_decls;
353 35237 ev.decls = decls;
354 35237 ev.n_decls = n_decls;
355 35237 ev.scope = scope;
356 } else {
357 3428960 ev.scope = parent_scope; /* no declarations, or only xmlns:xml */
358 }
359  
360 /* the element's own name */
361 {
362 const frx_str *uri;
363 3464197 frx_ns_split(&ev.qname, &ev.prefix, &ev.local);
364 3464197 uri = frx_ns_lookup(ev.scope, ev.prefix.p, ev.prefix.len);
365 3464197 ▶ if (!uri)
Branch: 1 branch
BranchTrueFalseTotal
if (!uri) 184 3464013 3464197
366 184 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, t->offset, "element prefix is not bound to a namespace");
367 3464013 ev.ns = *uri;
368 }
369  
370 /* the attributes: everything that was not a declaration */
371 5491756 ▶ for (i = 0; i < t->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n_attrs; i++) { 2027743 3464013 5491756
372 frx_str prefix;
373 2027743 ▶ if (!frx_ns_is_decl(&t->attrs[i].name, &prefix)) n_attrs++;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_ns_is_decl(&t->attrs[i].name, &prefix)) n_attrs++; 1973650 54093 2027743
374 }
375 3464013 ▶ if (n_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_attrs) { 1173610 2290403 3464013
376 1173610 attrs = (frx_attr *)frx_arena_alloc(p->tok_arena, (size_t)n_attrs * sizeof *attrs);
377 1173610 ▶ if (!attrs) return frx_parse_nomem(p, t->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!attrs) return frx_parse_nomem(p, t->offset); 0 1173610 1173610
378 1173610 n_attrs = 0;
379 3187754 ▶ for (i = 0; i < t->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n_attrs; i++) { 2014294 1173460 3187754
380 2014294 const frx_lex_attr *la = &t->attrs[i];
381 2014294 frx_attr *a = &attrs[n_attrs];
382 frx_str prefix;
383 2014294 ▶ if (frx_ns_is_decl(&la->name, &prefix)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_ns_is_decl(&la->name, &prefix)) continue; 40805 1973489 2014294
384 1973489 a->qname = la->name;
385 1973489 a->value = la->value;
386 1973489 a->offset = la->offset;
387 1973489 a->defaulted = la->defaulted;
388 1973489 frx_ns_split(&a->qname, &a->prefix, &a->local);
389 1973489 ▶ if (a->prefix.len) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->prefix.len) { 733939 1239550 1973489
390 733939 const frx_str *uri = frx_ns_lookup(ev.scope, a->prefix.p, a->prefix.len);
391 733939 ▶ if (!uri)
Branch: 1 branch
BranchTrueFalseTotal
if (!uri) 144 733795 733939
392 150 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset, "attribute prefix is not bound to a namespace");
393 733795 a->ns = *uri;
394 } else {
395 1239550 a->ns = FRX_EMPTY_STR; /* never the default */
396 }
397 52804618 ▶ for (j = 0; j < n_attrs; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n_attrs; j++) 50831279 1973339 52804618
398 50831279 ▶ if (frx_str_eq2(&attrs[j].local, &a->local) && frx_str_eq2(&attrs[j].ns, &a->ns))
Branch: 2 branches
BranchTrueFalseTotal
if (frx_str_eq2(&attrs[j].local, &a->local) && frx_str_eq2(&attrs[j].ns, &a->ns)) 2576 50828703 50831279
if (frx_str_eq2(&attrs[j].local, &a->local) && frx_str_eq2(&attrs[j].ns, &a->ns)) 6 2570 2576
399 6 return FRX_PARSE_FAIL(p, FRX_E_DUP_ATTR, a->offset,
400 "two attributes with one namespace and local name");
401 1973339 n_attrs++;
402 }
403 1173460 ev.attrs = attrs;
404 1173460 ev.n_attrs = n_attrs;
405 }
406  
407 3463863 ▶ if (!p->depth) p->seen_root = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!p->depth) p->seen_root = 1; 24438 3439425 3463863
408 3463863 ▶ if (!p->sink->start(p, p->ud, &ev)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!p->sink->start(p, p->ud, &ev)) return 0; 3 3463860 3463863
409  
410 3463860 ▶ if (!ev.empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ev.empty) { 3048210 415650 3463860
411 3048210 ▶ if (p->depth == p->cap_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->depth == p->cap_stack) { 24419 3023791 3048210
412 24419 ▶ int ncap = p->cap_stack ? p->cap_stack * 2 : 32;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = p->cap_stack ? p->cap_stack * 2 : 32; 1460 22959 24419
413 24419 frx_open *ns = (frx_open *)realloc(p->stack, (size_t)ncap * sizeof *ns);
414 24419 ▶ if (!ns) return frx_parse_nomem(p, t->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!ns) return frx_parse_nomem(p, t->offset); 0 24419 24419
415 24419 p->stack = ns;
416 24419 p->cap_stack = ncap;
417 }
418 3048210 p->stack[p->depth].qname = ev.qname;
419 3048210 p->stack[p->depth].scope = ev.scope;
420 3048210 p->stack[p->depth].offset = ev.offset;
421 3048210 p->depth++;
422 }
423 3463860 return 1;
424 }
425  
426 /* the DOCTYPE token: its declarations are already in the lexer's DTD and
427 * applied from there; the driver's part is where it may stand */
428 static int
429 4601 frx_parse_doctype(frx_parse *p, const frx_tok *t)
430 {
431 4601 ▶ if (p->depth || p->seen_root)
Branch: 2 branches
BranchTrueFalseTotal
if (p->depth || p->seen_root) 4601 0 4601
if (p->depth || p->seen_root) 1 4600 4601
432 1 return FRX_PARSE_FAIL(p, FRX_E_DTD, t->offset, "the DOCTYPE must precede the root element");
433 4600 p->doctype = p->lex.doctype;
434 4600 return p->sink->doctype(p, p->ud, p->lex.doctype, p->lex.dtd);
435 }
436  
437 static int
438 2974420 frx_parse_end(frx_parse *p, const frx_tok *t)
439 {
440 frx_str qname;
441 2974420 ▶ if (!p->depth)
Branch: 1 branch
BranchTrueFalseTotal
if (!p->depth) 4 2974416 2974420
442 4 return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, t->offset, "end tag with no open element");
443 2974416 ▶ if (!frx_str_eq2(&p->stack[p->depth - 1].qname, &t->name))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_str_eq2(&p->stack[p->depth - 1].qname, &t->name)) 1655 2972761 2974416
444 1655 return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, t->offset, "end tag does not match the open element");
445 2972761 ▶ if (!frx_parse_flush_text(p)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_parse_flush_text(p)) return 0; 0 2972761 2972761
446 2972761 qname = p->stack[p->depth - 1].qname;
447 2972761 p->depth--;
448 2972761 return p->sink->end(p, p->ud, &qname, t->offset);
449 }
450  
451 /* one token to the sink: FRX_STEP_OK, or MORE (the input ended inside a
452 * token and may grow), EOF (the input is finished; call frx_parse_finish)
453 * or FAIL (p->err) */
454 static int
455 13536046 frx_parse_step(frx_parse *p)
456 {
457 frx_tok t;
458 int ok;
459 13536046 ▶ if (p->failed) return FRX_STEP_FAIL;
Branch: 1 branch
BranchTrueFalseTotal
if (p->failed) return FRX_STEP_FAIL; 0 13536046 13536046
460 13536046 ▶ if (!frx_lex_next(&p->lex, &t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_next(&p->lex, &t)) { 29064 13506982 13536046
461 29064 ▶ if (p->lex.failed) { p->err = p->lex.err; p->failed = 1; return FRX_STEP_FAIL; }
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.failed) { p->err = p->lex.err; p->failed = 1; return FRX_STEP_FAIL; } 15311 13753 29064
462 13753 ▶ if (t.kind == FRX_TOK_MORE) return FRX_STEP_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (t.kind == FRX_TOK_MORE) return FRX_STEP_MORE; 3352 10401 13753
463 10401 return FRX_STEP_EOF;
464 }
465 13506982 switch (t.kind) {
466 3464300 case FRX_TOK_START:
467 3464300 case FRX_TOK_EMPTY: ok = frx_parse_element(p, &t); break;
468 2974420 case FRX_TOK_END: ok = frx_parse_end(p, &t); break;
469 7052975 case FRX_TOK_TEXT: ok = frx_parse_text(p, &t); break;
470 5810 ▶ case FRX_TOK_COMMENT: ok = frx_parse_flush_text(p) && p->sink->comment(p, p->ud, &t.value, t.offset); break;
Branch: 2 branches
BranchTrueFalseTotal
case FRX_TOK_COMMENT: ok = frx_parse_flush_text(p) && p->sink->comment(p, p->ud, &t.value, t.offset); break; 5810 0 5810
case FRX_TOK_COMMENT: ok = frx_parse_flush_text(p) && p->sink->comment(p, p->ud, &t.value, t.offset); break; 5810 0 5810
471 4876 ▶ case FRX_TOK_PI: ok = frx_parse_flush_text(p) && p->sink->pi(p, p->ud, &t.name, &t.value, t.offset); break;
Branch: 2 branches
BranchTrueFalseTotal
case FRX_TOK_PI: ok = frx_parse_flush_text(p) && p->sink->pi(p, p->ud, &t.name, &t.value, t.offset); break; 4876 0 4876
case FRX_TOK_PI: ok = frx_parse_flush_text(p) && p->sink->pi(p, p->ud, &t.name, &t.value, t.offset); break; 4876 0 4876
472 4601 case FRX_TOK_DOCTYPE: ok = frx_parse_doctype(p, &t); break;
473 0 default: ok = FRX_PARSE_FAIL(p, FRX_E_SYNTAX, t.offset, "unexpected token"); break;
474 }
475 13506982 return ok ? FRX_STEP_OK : FRX_STEP_FAIL;
476 }
477  
478 /* after EOF: what must be true of a whole document, then the sink's finish */
479 static int
480 10401 frx_parse_finish(frx_parse *p, size_t end_offset)
481 {
482 10401 ▶ if (p->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p->failed) return 0; 0 10401 10401
483 10401 ▶ if (p->depth)
Branch: 1 branch
BranchTrueFalseTotal
if (p->depth) 3032 7369 10401
484 3032 return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, p->stack[p->depth - 1].offset, "element is never closed");
485 7369 ▶ if (!p->seen_root)
Branch: 1 branch
BranchTrueFalseTotal
if (!p->seen_root) 64 7305 7369
486 64 return FRX_PARSE_FAIL(p, FRX_E_ROOT, end_offset, "no root element");
487 7305 ▶ if (p->sink->finish && !p->sink->finish(p, p->ud)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p->sink->finish && !p->sink->finish(p, p->ud)) return 0; 7265 40 7305
if (p->sink->finish && !p->sink->finish(p, p->ud)) return 0; 4 7261 7265
488 7301 return 1;
489 }
490  
491 /* the whole parse under the extended options: a document, or NULL with
492 * *err filled. frx_parse_doc, below, is the strict form the v1 ABI keeps. */
493 static frx_doc *
494 28199 frx_parse_doc_ex(const char *bytes, size_t len, const frx_opts_ex *oe_in, frx_err *err)
495 {
496 frx_parse p;
497 frx_tree_sink ts;
498 frx_doc *d;
499 frx_opts_ex oe;
500 const frx_opts *o;
501 frx_enc enc;
502 28199 const unsigned char *lex_in = NULL;
503 28199 size_t lex_len = 0;
504 int ok, step;
505 int i;
506  
507 28199 frx_opts_ex_norm(oe_in, &oe);
508 28199 o = &oe.base;
509 28199 memset(&enc, 0, sizeof enc);
510  
511 28199 frx_err_set(err, FRX_OK, 0, NULL);
512 28199 d = frx_doc_new();
513 28199 ▶ if (!d) { frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!d) { frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); return NULL; } 0 28199 28199
514  
515 28199 frx_tree_sink_init(&ts, d, 0);
516 28199 frx_parse_init(&p, &oe, &d->arena, &d->arena, &FRX_TREE_SINK, &ts);
517 28199 d->max_depth = p.max_depth;
518 28199 d->profile = p.profile;
519  
520 /* the id attribute names, copied so the caller's strings need not live */
521 28199 ▶ if (o->n_id_attrs > 0 && o->id_attrs) {
Branch: 2 branches
BranchTrueFalseTotal
if (o->n_id_attrs > 0 && o->id_attrs) { 23171 5028 28199
if (o->n_id_attrs > 0 && o->id_attrs) { 23171 0 23171
522 23171 frx_str *names = (frx_str *)frx_arena_alloc(&d->arena, (size_t)o->n_id_attrs * sizeof *names);
523 23171 ▶ if (!names) { frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); frx_parse_free(&p); frx_doc_free(d); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!names) { frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); frx_parse_free(&p); frx_doc_free(d); return NULL; } 0 23171 23171
524 46344 ▶ for (i = 0; i < o->n_id_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->n_id_attrs; i++) { 23173 23171 46344
525 23173 names[i] = frx_arena_strndup(&d->arena, o->id_attrs[i], strlen(o->id_attrs[i]));
526 23173 ▶ if (!names[i].p) { frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); frx_parse_free(&p); frx_doc_free(d); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!names[i].p) { frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); frx_parse_free(&p); frx_doc_free(d); return NULL; } 0 23173 23173
527 }
528 23171 d->id_attrs = names;
529 23171 d->n_id_attrs = o->n_id_attrs;
530 23171 d->id_source = FRX_ID_ATTRS;
531 }
532  
533 /* the document's own URI, which xml:base is resolved against and an
534 * xi:include's href is joined onto */
535 28199 ▶ if (oe.base_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (oe.base_uri) { 5581 22618 28199
536 5581 d->base_uri = frx_arena_strndup(&d->arena, oe.base_uri, strlen(oe.base_uri));
537 5581 ▶ if (!d->base_uri.p) { frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); frx_parse_free(&p); frx_doc_free(d); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!d->base_uri.p) { frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); frx_parse_free(&p); frx_doc_free(d); return NULL; } 0 5581 5581
538 }
539  
540 28199 d->document = frx_node_new(d, FRX_DOCUMENT, 0);
541 28199 ▶ if (!d->document) { frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); frx_parse_free(&p); frx_doc_free(d); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!d->document) { frx_err_set(err, FRX_E_NOMEM, 0, "out of memory"); frx_parse_free(&p); frx_doc_free(d); return NULL; } 0 28199 28199
542  
543 28199 ▶ if (p.profile == FRX_PROFILE_FULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (p.profile == FRX_PROFILE_FULL) { 5957 22242 28199
544 /* the transcoder in front of the lexer: max_bytes on the input, the
545 * lexer over the UTF-8 it produces, offsets mapped back on failure */
546 5957 frx_opts lo = *o;
547 5957 ▶ if (o->max_bytes && len > o->max_bytes) {
Branch: 2 branches
BranchTrueFalseTotal
if (o->max_bytes && len > o->max_bytes) { 4 5953 5957
if (o->max_bytes && len > o->max_bytes) { 1 3 4
548 1 frx_err_set(err, FRX_E_TOO_LARGE, o->max_bytes, "input exceeds max_bytes");
549 1 frx_parse_free(&p);
550 1 frx_doc_free(d);
551 11 return NULL;
552 }
553 5956 lo.max_bytes = 0;
554 5956 ok = frx_enc_detect(&enc, (const unsigned char *)bytes, len, oe.encoding, err)
555 5956 ▶ && frx_enc_run(&enc, (const unsigned char *)bytes, len, err);
Branch: 2 branches
BranchTrueFalseTotal
&& frx_enc_run(&enc, (const unsigned char *)bytes, len, err); 5951 5 5956
&& frx_enc_run(&enc, (const unsigned char *)bytes, len, err); 5946 5 5951
556 5956 ▶ if (!ok) { frx_parse_free(&p); frx_doc_free(d); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { frx_parse_free(&p); frx_doc_free(d); return NULL; } 10 5946 5956
557 5946 ▶ lex_in = enc.out ? (const unsigned char *)enc.out : (const unsigned char *)bytes + enc.bom;
Branch: 1 branch
BranchTrueFalseTotal
lex_in = enc.out ? (const unsigned char *)enc.out : (const unsigned char *)bytes + enc.bom; 545 5401 5946
558 5946 ▶ lex_len = enc.out ? enc.out_len : len - (size_t)enc.bom;
Branch: 1 branch
BranchTrueFalseTotal
lex_len = enc.out ? enc.out_len : len - (size_t)enc.bom; 545 5401 5946
559 5946 frx_lex_setup(&p.lex, &lo, p.profile, &d->arena, enc.kind, enc.bom != 0, enc.override);
560 } else {
561 22242 frx_lex_setup(&p.lex, o, p.profile, &d->arena, FRX_ENC_UTF8, 0, 0);
562 22242 lex_in = (const unsigned char *)bytes;
563 22242 lex_len = len;
564 }
565 28188 p.lex.in = lex_in;
566 28188 p.lex.len = lex_len;
567 28188 p.lex.doc_len = lex_len;
568 28188 p.lex.eof = 1;
569 28188 frx_parse_wire_lex(&p, &oe);
570  
571 28188 ok = frx_lex_prolog(&p.lex);
572 28188 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 27800 388 28188
573 27800 p.lex.prolog_done = 1;
574 27800 ▶ if (p.lex.version.len) d->version = p.lex.version; /* as declared; "1.0" otherwise */
Branch: 1 branch
BranchTrueFalseTotal
if (p.lex.version.len) d->version = p.lex.version; /* as declared; "1.0" otherwise */ 10628 17172 27800
575 27800 d->standalone = p.lex.standalone;
576 }
577 6441414 ▶ while (ok) {
Branch: 1 branch
BranchTrueFalseTotal
while (ok) { 6423587 17827 6441414
578 6423587 step = frx_parse_step(&p);
579 6423587 ▶ if (step == FRX_STEP_OK) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_OK) continue; 6395787 27800 6423587
580 27800 ▶ if (step == FRX_STEP_EOF) { ok = frx_parse_finish(&p, len); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_EOF) { ok = frx_parse_finish(&p, len); break; } 10361 17439 27800
581 17439 ok = 0; /* FAIL; MORE cannot happen with eof set */
582 }
583 28188 ▶ if (ok && p.lex.failed) { p.err = p.lex.err; p.failed = 1; ok = 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (ok && p.lex.failed) { p.err = p.lex.err; p.failed = 1; ok = 0; } 7261 20927 28188
if (ok && p.lex.failed) { p.err = p.lex.err; p.failed = 1; ok = 0; } 0 7261 7261
584  
585 /* The references WFC: Entity Declared exempted, into the document's
586 * arena so the validate pass can report each as VC: Entity Declared.
587 * The lexer's array dies with the lexer. */
588 28188 ▶ if (ok && p.lex.n_undecl) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok && p.lex.n_undecl) { 7261 20927 28188
if (ok && p.lex.n_undecl) { 18 7243 7261
589 18 size_t *u = (size_t *)frx_arena_alloc(&d->arena,
590 18 (size_t)p.lex.n_undecl * sizeof *u);
591 18 ▶ if (!u) { ok = frx_parse_nomem(&p, 0); }
Branch: 1 branch
BranchTrueFalseTotal
if (!u) { ok = frx_parse_nomem(&p, 0); } 0 18 18
592 else {
593 18 memcpy(u, p.lex.undecl, (size_t)p.lex.n_undecl * sizeof *u);
594 18 d->undeclared_refs = u;
595 18 d->n_undeclared_refs = p.lex.n_undecl;
596 }
597 }
598  
599 /* the validity constraints, over the finished tree: `validate => 1`
600 * fails the parse at the first one, `collect` hangs them all off the
601 * document and returns it */
602 28188 ▶ if (ok && oe.validate && !frx_validate_doc(d, oe.validate == 2, &p.err)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ok && oe.validate && !frx_validate_doc(d, oe.validate == 2, &p.err)) { 7261 20927 28188
if (ok && oe.validate && !frx_validate_doc(d, oe.validate == 2, &p.err)) { 1386 5875 7261
if (ok && oe.validate && !frx_validate_doc(d, oe.validate == 2, &p.err)) { 71 1315 1386
603 71 p.failed = 1;
604 71 ok = 0;
605 }
606  
607 /* XInclude, after the tree is whole and valid: every xi:include
608 * replaced by what it names, through the same resolver */
609 28188 ▶ if (ok && oe.xinclude && p.profile == FRX_PROFILE_FULL
Branch: 3 branches
BranchTrueFalseTotal
if (ok && oe.xinclude && p.profile == FRX_PROFILE_FULL 7190 20998 28188
if (ok && oe.xinclude && p.profile == FRX_PROFILE_FULL 179 7011 7190
if (ok && oe.xinclude && p.profile == FRX_PROFILE_FULL 179 0 179
610 179 ▶ && !frx_xinclude_doc(d, &oe, &p.err)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_xinclude_doc(d, &oe, &p.err)) { 25 154 179
611 25 p.failed = 1;
612 25 ok = 0;
613 }
614  
615 28188 frx_parse_free(&p);
616 28188 frx_tree_sink_free(&ts);
617  
618 28188 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 21023 7165 28188
619 21023 ▶ *err = p.failed ? p.err : p.lex.err;
Branch: 1 branch
BranchTrueFalseTotal
*err = p.failed ? p.err : p.lex.err; 20635 388 21023
620 21023 ▶ if (err->code == FRX_OK) frx_err_set(err, FRX_E_SYNTAX, len, "parse failed");
Branch: 1 branch
BranchTrueFalseTotal
if (err->code == FRX_OK) frx_err_set(err, FRX_E_SYNTAX, len, "parse failed"); 0 21023 21023
621 21023 ▶ if (p.profile == FRX_PROFILE_FULL && (enc.out || enc.bom)) {
Branch: 3 branches
BranchTrueFalseTotal
if (p.profile == FRX_PROFILE_FULL && (enc.out || enc.bom)) { 2332 18691 21023
if (p.profile == FRX_PROFILE_FULL && (enc.out || enc.bom)) { 2285 47 2332
if (p.profile == FRX_PROFILE_FULL && (enc.out || enc.bom)) { 3 2282 2285
622 /* the offset is in the transcoded bytes; say it in the caller's */
623 50 err->offset = frx_enc_input_offset(&enc, (const unsigned char *)bytes, len, err->offset);
624 50 ▶ if (enc.out) err->enc = frx_enc_name(enc.kind);
Branch: 1 branch
BranchTrueFalseTotal
if (enc.out) err->enc = frx_enc_name(enc.kind); 47 3 50
625 }
626 21023 frx_enc_free(&enc);
627 21023 frx_doc_free(d);
628 21023 return NULL;
629 }
630 7165 frx_enc_free(&enc); /* the tree holds copies of every string */
631 7165 return d;
632 }
633  
634 /* the strict parse: what the v1 ABI's parse entry means, forever */
635 static frx_doc *
636 82 frx_parse_doc(const char *bytes, size_t len, const frx_opts *o, frx_err *err)
637 {
638 frx_opts_ex oe;
639 82 memset(&oe, 0, sizeof oe);
640 82 oe.size = sizeof oe;
641 82 ▶ if (o) oe.base = *o;
Branch: 1 branch
BranchTrueFalseTotal
if (o) oe.base = *o; 75 7 82
642 82 oe.profile = FRX_PROFILE_STRICT;
643 82 return frx_parse_doc_ex(bytes, len, &oe, err);
644 }
645  
646 /* ---- the walkers ------------------------------------------------------ */
647  
648 /* does the element match (ns, local)? ns NULL: any; "": none */
649 static int
650 1402653 frx_node_matches(const frx_node *n, const char *ns, const char *local)
651 {
652 1402653 ▶ if (n->kind != FRX_ELEMENT) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n->kind != FRX_ELEMENT) return 0; 600921 801732 1402653
653 801732 ▶ if (!frx_str_eq(&n->local, local, strlen(local))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_str_eq(&n->local, local, strlen(local))) return 0; 700476 101256 801732
654 101256 ▶ if (ns && !frx_str_eq(&n->ns, ns, strlen(ns))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (ns && !frx_str_eq(&n->ns, ns, strlen(ns))) return 0; 19 101237 101256
if (ns && !frx_str_eq(&n->ns, ns, strlen(ns))) return 0; 9 10 19
655 101247 return 1;
656 }
657  
658 /* direct children matching, from after (NULL to start) */
659 static const frx_node *
660 40 frx_find(const frx_node *n, const char *ns, const char *local, const frx_node *after)
661 {
662 40 ▶ const frx_node *c = after ? after->next : n->first_child;
Branch: 1 branch
BranchTrueFalseTotal
const frx_node *c = after ? after->next : n->first_child; 17 23 40
663 200108 ▶ for (; c; c = c->next)
Branch: 1 branch
BranchTrueFalseTotal
for (; c; c = c->next) 200089 19 200108
664 200089 ▶ if (frx_node_matches(c, ns, local)) return c;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_node_matches(c, ns, local)) return c; 21 200068 200089
665 19 return NULL;
666 }
667  
668 /* the node after n in document order, staying inside root; NULL at the end */
669 static const frx_node *
670 1329403 frx_walk_next(const frx_node *n, const frx_node *root)
671 {
672 1329403 ▶ if (n->first_child) return n->first_child;
Branch: 1 branch
BranchTrueFalseTotal
if (n->first_child) return n->first_child; 611280 718123 1329403
673 1330623 ▶ while (n != root && !n->next) n = n->parent;
Branch: 2 branches
BranchTrueFalseTotal
while (n != root && !n->next) n = n->parent; 1328304 2319 1330623
while (n != root && !n->next) n = n->parent; 612500 715804 1328304
674 718123 ▶ return n == root ? NULL : n->next;
Branch: 1 branch
BranchTrueFalseTotal
return n == root ? NULL : n->next; 715804 2319 718123
675 }
676  
677 /* every descendant of n in document order, to cb; 0 from cb stops */
678 static int
679 123 frx_descendants(const frx_node *n, int (*cb)(const frx_node *, void *), void *ud)
680 {
681 123 const frx_node *c = n->first_child;
682 599 ▶ while (c) {
Branch: 1 branch
BranchTrueFalseTotal
while (c) { 476 123 599
683 476 ▶ if (!cb(c, ud)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!cb(c, ud)) return 0; 0 476 476
684 476 c = frx_walk_next(c, n);
685 }
686 123 return 1;
687 }
688  
689 static int
690 476 frx_text_cb(const frx_node *n, void *ud)
691 {
692 476 ▶ if (n->kind == FRX_TEXT) frx_buf_append_n((frx_buf *)ud, n->value.p, n->value.len);
Branch: 1 branch
BranchTrueFalseTotal
if (n->kind == FRX_TEXT) frx_buf_append_n((frx_buf *)ud, n->value.p, n->value.len); 140 336 476
693 476 return 1;
694 }
695  
696 /* text: element or document = descendant text in order; text = itself;
697 * comment = body; PI = data */
698 static void
699 164 frx_text(const frx_node *n, frx_buf *out)
700 {
701 164 ▶ switch (n->kind) {
Branch: 1 branch
BranchTrueFalseTotal
switch (n->kind) { 41 123 164
702 41 case FRX_TEXT:
703 case FRX_COMMENT:
704 case FRX_PI:
705 41 frx_buf_append_n(out, n->value.p, n->value.len);
706 41 break;
707 123 default:
708 123 frx_descendants(n, frx_text_cb, out);
709 123 break;
710 }
711 164 }
712  
713 /* the attribute value for (ns, local), or NULL; ns NULL: any, "": none */
714 static const frx_str *
715 760849 frx_attr_value(const frx_node *n, const char *ns, const char *local)
716 {
717 int i;
718 760957 ▶ for (i = 0; i < n->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->n_attrs; i++) { 760911 46 760957
719 760911 const frx_attr *a = &n->attrs[i];
720 760911 ▶ if (!frx_str_eq(&a->local, local, strlen(local))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_str_eq(&a->local, local, strlen(local))) continue; 101 760810 760911
721 760810 ▶ if (ns && !frx_str_eq(&a->ns, ns, strlen(ns))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (ns && !frx_str_eq(&a->ns, ns, strlen(ns))) continue; 51 760759 760810
if (ns && !frx_str_eq(&a->ns, ns, strlen(ns))) continue; 7 44 51
722 760803 return &a->value;
723 }
724 46 return NULL;
725 }
726  
727 /* exactly one element, or NULL */
728 static const frx_node *
729 63 frx_by_id(const frx_doc *d, const char *attr, const char *value, size_t vlen)
730 {
731 frx_idx key;
732 const frx_idx *hit;
733 63 ▶ if (!d->n_ids) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!d->n_ids) return NULL; 8 55 63
734 55 key.attr.p = attr;
735 55 key.attr.len = strlen(attr);
736 55 key.value.p = value;
737 55 key.value.len = vlen;
738 55 key.node = NULL;
739 55 key.offset = 0;
740 /* keys are unique after finish, so the key order finds the one */
741 55 hit = (const frx_idx *)bsearch(&key, d->ids, (size_t)d->n_ids, sizeof *d->ids, frx_idx_key_cmp);
742 55 ▶ return hit ? hit->node : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return hit ? hit->node : NULL; 42 13 55
743 }
744  
745 #endif /* FRX_PARSE_H */
« include/frx/frx_ns.h ↑ Summary include/frx/frx_tree.h »