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 29063 frx_opts_ex_norm(const frx_opts_ex *in, frx_opts_ex *out)
126 {
127 29063 memset(out, 0, sizeof *out);
128 29063 out->size = sizeof *out;
129 29063 out->profile = FRX_PROFILE_STRICT;
130 29063 ▶ if (in) {
Branch: 1 branch
BranchTrueFalseTotal
if (in) { 29063 0 29063
131 29063 size_t n = in->size < sizeof *out ? in->size : sizeof *out;
132 29063 ▶ 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), 29063 0 29063
133 (const char *)in + sizeof(size_t),
134 n - sizeof(size_t));
135 29063 out->size = sizeof *out;
136 }
137 29063 }
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 29063 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 29063 memset(p, 0, sizeof *p);
164 29063 p->profile = oe->profile == FRX_PROFILE_FULL ? FRX_PROFILE_FULL : FRX_PROFILE_STRICT;
165 29063 p->validate = oe->validate != 0;
166 29063 ▶ 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 29055 29063
167 29063 p->sink = sink;
168 29063 p->ud = ud;
169 29063 p->arena = arena;
170 29063 p->tok_arena = tok_arena;
171 29063 p->text_entity = FRX_EMPTY_STR;
172 29063 frx_intern_init(&p->intern);
173 29063 frx_buf_init(&p->text);
174 29063 }
175  
176 /* what the extended options say to the lexer, once it exists */
177 static void
178 29048 frx_parse_wire_lex(frx_parse *p, const frx_opts_ex *oe)
179 {
180 29048 frx_lex *l = &p->lex;
181 29048 ▶ 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 29044 29048
182 29048 ▶ 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 29044 29048
183 29048 ▶ 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 29044 29048
184 29048 ▶ 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 29041 29048
185 29048 ▶ 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 29046 29048
186 29048 l->resolve = oe->resolve;
187 29048 l->resolve_ud = oe->resolve_ud;
188 29048 l->max_bytes = oe->base.max_bytes;
189 29048 l->validate = p->validate;
190 29048 ▶ if (oe->base_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (oe->base_uri) { 5580 23468 29048
191 5580 l->cur_base.p = oe->base_uri;
192 5580 l->cur_base.len = strlen(oe->base_uri);
193 }
194 29048 }
195  
196 static void
197 29063 frx_parse_free(frx_parse *p)
198 {
199 29063 frx_lex_free(&p->lex);
200 29063 frx_intern_free(&p->intern);
201 29063 frx_buf_free(&p->text);
202 29063 free(p->spans);
203 29063 free(p->stack);
204 29063 p->spans = NULL;
205 29063 p->stack = NULL;
206 29063 }
207  
208 static const frx_scope *
209 3464218 frx_parse_scope(const frx_parse *p)
210 {
211 3464218 ▶ return p->depth ? p->stack[p->depth - 1].scope : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return p->depth ? p->stack[p->depth - 1].scope : NULL; 3439665 24553 3464218
212 }
213  
214 /* the pending text goes to the sink as one event */
215 static int
216 6447624 frx_parse_flush_text(frx_parse *p)
217 {
218 int ok;
219 6447624 ▶ if (!p->have_text) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!p->have_text) return 1; 3617889 2829735 6447624
220 2829735 p->have_text = 0;
221 2829735 ▶ 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 2829735 2829735
222 2829735 ok = p->sink->text(p, p->ud, p->text.p ? p->text.p : "", p->text.len,
223 2829735 ▶ 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, 2829735 0 2829735
224 p->text_flags);
225 2829735 p->n_spans = 0;
226 2829735 p->text_flags = 0;
227 2829735 return ok;
228 }
229  
230 static int
231 7052895 frx_parse_text(frx_parse *p, const frx_tok *t)
232 {
233 7052895 ▶ if (!p->depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->depth) { 27930 7024965 7052895
234 27930 ▶ if (t->cdata) /* not Misc, whatever it holds */
Branch: 1 branch
BranchTrueFalseTotal
if (t->cdata) /* not Misc, whatever it holds */ 4 27926 27930
235 4 return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, t->offset, "a CDATA section is not allowed outside the root element");
236 27926 ▶ 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 */ 27894 32 27926
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 7024965 ▶ 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 2838806 7024965
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 7024965 ▶ if (!p->have_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->have_text) { 2838826 4186139 7024965
245 2838826 p->have_text = 1;
246 2838826 p->text_offset = t->offset;
247 2838826 p->text_entity = t->entity;
248 2838826 p->text.len = 0;
249 2838826 p->n_spans = 0;
250 2838826 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 7024965 p->text_flags |= t->text_flags;
260 7024965 ▶ if (t->cdata && p->profile == FRX_PROFILE_FULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (t->cdata && p->profile == FRX_PROFILE_FULL) { 52036 6972929 7024965
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 7024965 frx_buf_append_n(&p->text, t->value.p, t->value.len);
272 7024965 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 3464218 frx_parse_element(frx_parse *p, const frx_tok *t)
280 {
281 frx_ev_start ev;
282 3464218 frx_nsdecl *decls = NULL;
283 3464218 frx_attr *attrs = NULL;
284 3464218 frx_scope *scope = NULL;
285 3464218 const frx_scope *parent_scope = frx_parse_scope(p);
286 3464218 int n_decls = 0, n_attrs = 0, i, j;
287  
288 3464218 ▶ if (!frx_parse_flush_text(p)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_parse_flush_text(p)) return 0; 0 3464218 3464218
289  
290 3464218 ▶ if (!p->depth && p->seen_root)
Branch: 2 branches
BranchTrueFalseTotal
if (!p->depth && p->seen_root) 24553 3439665 3464218
if (!p->depth && p->seen_root) 10 24543 24553
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 3464208 ▶ if (p->depth + 1 > p->max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (p->depth + 1 > p->max_depth) 5 3464203 3464208
295 5 return FRX_PARSE_FAIL(p, FRX_E_TOO_DEEP, t->offset, "nesting deeper than max_depth");
296  
297 3464203 memset(&ev, 0, sizeof ev);
298 3464203 ev.qname = t->name;
299 3464203 ev.offset = t->offset;
300 3464203 ev.empty = t->kind == FRX_TOK_EMPTY;
301  
302 /* declarations first: they bind the element's own prefix */
303 5492344 ▶ for (i = 0; i < t->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n_attrs; i++) { 2028141 3464203 5492344
304 frx_str prefix;
305 2028141 ▶ 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++; 54241 1973900 2028141
306 }
307 3464203 ▶ if (n_decls) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_decls) { 35428 3428775 3464203
308 35428 decls = (frx_nsdecl *)frx_arena_alloc(p->tok_arena, (size_t)n_decls * sizeof *decls);
309 35428 scope = (frx_scope *)frx_arena_alloc(p->tok_arena, sizeof *scope);
310 35428 ▶ if (!decls || !scope) return frx_parse_nomem(p, t->offset);
Branch: 2 branches
BranchTrueFalseTotal
if (!decls || !scope) return frx_parse_nomem(p, t->offset); 35428 0 35428
if (!decls || !scope) return frx_parse_nomem(p, t->offset); 0 35428 35428
311 35428 n_decls = 0;
312 149643 ▶ for (i = 0; i < t->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n_attrs; i++) { 114303 35340 149643
313 114303 const frx_lex_attr *a = &t->attrs[i];
314 frx_str prefix;
315 114303 const frx_str *v = &a->value;
316 114426 ▶ if (!frx_ns_is_decl(&a->name, &prefix)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_ns_is_decl(&a->name, &prefix)) continue; 60072 54231 114303
317 54231 ▶ if (frx_str_eq(&prefix, "xmlns", 5))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(&prefix, "xmlns", 5)) 4 54227 54231
318 88 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset, "the xmlns prefix cannot be declared");
319 54227 ▶ 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 54224 54227
320 3 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset, "the xmlns namespace name cannot be bound");
321 54224 ▶ if (frx_str_eq(&prefix, "xml", 3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(&prefix, "xml", 3)) { 162 54062 54224
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 54062 ▶ 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 54056 54062
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 54056 ▶ if (prefix.len && !v->len && !p->lex.version11)
Branch: 3 branches
BranchTrueFalseTotal
if (prefix.len && !v->len && !p->lex.version11) 47036 7020 54056
if (prefix.len && !v->len && !p->lex.version11) 5 47031 47036
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 54054 ▶ if (!frx_ns_is_absolute(v->p, v->len))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_ns_is_absolute(v->p, v->len)) 34 54020 54054
339 34 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset, "a relative namespace URI is refused; canonicalisation is undefined over it");
340 79280 ▶ for (j = 0; j < n_decls; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n_decls; j++) 25260 54020 79280
341 25260 ▶ if (frx_str_eq2(&decls[j].prefix, &prefix))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&decls[j].prefix, &prefix)) 0 25260 25260
342 0 return FRX_PARSE_FAIL(p, FRX_E_DUP_ATTR, a->offset, "prefix declared twice on one element");
343 54020 decls[n_decls].prefix = prefix;
344 54020 decls[n_decls].uri = frx_intern_uri(&p->intern, p->arena, v->p, v->len);
345 54020 ▶ 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); 50834 3186 54020
if (v->len && !decls[n_decls].uri.p) return frx_parse_nomem(p, a->offset); 0 50834 50834
346 54020 n_decls++;
347 }
348 }
349 3464115 ▶ if (n_decls) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_decls) { 35217 3428898 3464115
350 35217 scope->up = parent_scope;
351 35217 scope->decls = decls;
352 35217 scope->n_decls = n_decls;
353 35217 ev.decls = decls;
354 35217 ev.n_decls = n_decls;
355 35217 ev.scope = scope;
356 } else {
357 3428898 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 3464115 frx_ns_split(&ev.qname, &ev.prefix, &ev.local);
364 3464115 uri = frx_ns_lookup(ev.scope, ev.prefix.p, ev.prefix.len);
365 3464115 ▶ if (!uri)
Branch: 1 branch
BranchTrueFalseTotal
if (!uri) 184 3463931 3464115
366 184 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, t->offset, "element prefix is not bound to a namespace");
367 3463931 ev.ns = *uri;
368 }
369  
370 /* the attributes: everything that was not a declaration */
371 5491593 ▶ for (i = 0; i < t->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n_attrs; i++) { 2027662 3463931 5491593
372 frx_str prefix;
373 2027662 ▶ 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++; 1973609 54053 2027662
374 }
375 3463931 ▶ if (n_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_attrs) { 1173589 2290342 3463931
376 1173589 attrs = (frx_attr *)frx_arena_alloc(p->tok_arena, (size_t)n_attrs * sizeof *attrs);
377 1173589 ▶ if (!attrs) return frx_parse_nomem(p, t->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!attrs) return frx_parse_nomem(p, t->offset); 0 1173589 1173589
378 1173589 n_attrs = 0;
379 3187692 ▶ for (i = 0; i < t->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n_attrs; i++) { 2014253 1173439 3187692
380 2014253 const frx_lex_attr *la = &t->attrs[i];
381 2014253 frx_attr *a = &attrs[n_attrs];
382 frx_str prefix;
383 2014253 ▶ if (frx_ns_is_decl(&la->name, &prefix)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_ns_is_decl(&la->name, &prefix)) continue; 40805 1973448 2014253
384 1973448 a->qname = la->name;
385 1973448 a->value = la->value;
386 1973448 a->offset = la->offset;
387 1973448 a->defaulted = la->defaulted;
388 1973448 frx_ns_split(&a->qname, &a->prefix, &a->local);
389 1973448 ▶ if (a->prefix.len) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->prefix.len) { 733919 1239529 1973448
390 733919 const frx_str *uri = frx_ns_lookup(ev.scope, a->prefix.p, a->prefix.len);
391 733919 ▶ if (!uri)
Branch: 1 branch
BranchTrueFalseTotal
if (!uri) 144 733775 733919
392 150 return FRX_PARSE_FAIL(p, FRX_E_NAMESPACE, a->offset, "attribute prefix is not bound to a namespace");
393 733775 a->ns = *uri;
394 } else {
395 1239529 a->ns = FRX_EMPTY_STR; /* never the default */
396 }
397 52804557 ▶ for (j = 0; j < n_attrs; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n_attrs; j++) 50831259 1973298 52804557
398 50831259 ▶ 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 50828683 50831259
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 1973298 n_attrs++;
402 }
403 1173439 ev.attrs = attrs;
404 1173439 ev.n_attrs = n_attrs;
405 }
406  
407 3463781 ▶ if (!p->depth) p->seen_root = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!p->depth) p->seen_root = 1; 24417 3439364 3463781
408 3463781 ▶ 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 3463778 3463781
409  
410 3463778 ▶ if (!ev.empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ev.empty) { 3048129 415649 3463778
411 3048129 ▶ if (p->depth == p->cap_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->depth == p->cap_stack) { 24398 3023731 3048129
412 24398 ▶ 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 22938 24398
413 24398 frx_open *ns = (frx_open *)realloc(p->stack, (size_t)ncap * sizeof *ns);
414 24398 ▶ if (!ns) return frx_parse_nomem(p, t->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!ns) return frx_parse_nomem(p, t->offset); 0 24398 24398
415 24398 p->stack = ns;
416 24398 p->cap_stack = ncap;
417 }
418 3048129 p->stack[p->depth].qname = ev.qname;
419 3048129 p->stack[p->depth].scope = ev.scope;
420 3048129 p->stack[p->depth].offset = ev.offset;
421 3048129 p->depth++;
422 }
423 3463778 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 2974359 frx_parse_end(frx_parse *p, const frx_tok *t)
439 {
440 frx_str qname;
441 2974359 ▶ if (!p->depth)
Branch: 1 branch
BranchTrueFalseTotal
if (!p->depth) 4 2974355 2974359
442 4 return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, t->offset, "end tag with no open element");
443 2974355 ▶ 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 2972700 2974355
444 1655 return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, t->offset, "end tag does not match the open element");
445 2972700 ▶ if (!frx_parse_flush_text(p)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_parse_flush_text(p)) return 0; 0 2972700 2972700
446 2972700 qname = p->stack[p->depth - 1].qname;
447 2972700 p->depth--;
448 2972700 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 13535819 frx_parse_step(frx_parse *p)
456 {
457 frx_tok t;
458 int ok;
459 13535819 ▶ if (p->failed) return FRX_STEP_FAIL;
Branch: 1 branch
BranchTrueFalseTotal
if (p->failed) return FRX_STEP_FAIL; 0 13535819 13535819
460 13535819 ▶ if (!frx_lex_next(&p->lex, &t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_lex_next(&p->lex, &t)) { 29060 13506759 13535819
461 29060 ▶ 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 13749 29060
462 13749 ▶ if (t.kind == FRX_TOK_MORE) return FRX_STEP_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (t.kind == FRX_TOK_MORE) return FRX_STEP_MORE; 3349 10400 13749
463 10400 return FRX_STEP_EOF;
464 }
465 13506759 switch (t.kind) {
466 3464218 case FRX_TOK_START:
467 3464218 case FRX_TOK_EMPTY: ok = frx_parse_element(p, &t); break;
468 2974359 case FRX_TOK_END: ok = frx_parse_end(p, &t); break;
469 7052895 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 13506759 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 10400 frx_parse_finish(frx_parse *p, size_t end_offset)
481 {
482 10400 ▶ if (p->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p->failed) return 0; 0 10400 10400
483 10400 ▶ if (p->depth)
Branch: 1 branch
BranchTrueFalseTotal
if (p->depth) 3032 7368 10400
484 3032 return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, p->stack[p->depth - 1].offset, "element is never closed");
485 7368 ▶ if (!p->seen_root)
Branch: 1 branch
BranchTrueFalseTotal
if (!p->seen_root) 64 7304 7368
486 64 return FRX_PARSE_FAIL(p, FRX_E_ROOT, end_offset, "no root element");
487 7304 ▶ 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; 7264 40 7304
if (p->sink->finish && !p->sink->finish(p, p->ud)) return 0; 4 7260 7264
488 7300 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 28198 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 28198 const unsigned char *lex_in = NULL;
503 28198 size_t lex_len = 0;
504 int ok, step;
505 int i;
506  
507 28198 frx_opts_ex_norm(oe_in, &oe);
508 28198 o = &oe.base;
509 28198 memset(&enc, 0, sizeof enc);
510  
511 28198 frx_err_set(err, FRX_OK, 0, NULL);
512 28198 d = frx_doc_new();
513 28198 ▶ 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 28198 28198
514  
515 28198 frx_tree_sink_init(&ts, d, 0);
516 28198 frx_parse_init(&p, &oe, &d->arena, &d->arena, &FRX_TREE_SINK, &ts);
517 28198 d->max_depth = p.max_depth;
518 28198 d->profile = p.profile;
519  
520 /* the id attribute names, copied so the caller's strings need not live */
521 28198 ▶ if (o->n_id_attrs > 0 && o->id_attrs) {
Branch: 2 branches
BranchTrueFalseTotal
if (o->n_id_attrs > 0 && o->id_attrs) { 23171 5027 28198
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 28198 ▶ if (oe.base_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (oe.base_uri) { 5581 22617 28198
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 28198 d->document = frx_node_new(d, FRX_DOCUMENT, 0);
541 28198 ▶ 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 28198 28198
542  
543 28198 ▶ if (p.profile == FRX_PROFILE_FULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (p.profile == FRX_PROFILE_FULL) { 5957 22241 28198
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 22241 frx_lex_setup(&p.lex, o, p.profile, &d->arena, FRX_ENC_UTF8, 0, 0);
562 22241 lex_in = (const unsigned char *)bytes;
563 22241 lex_len = len;
564 }
565 28187 p.lex.in = lex_in;
566 28187 p.lex.len = lex_len;
567 28187 p.lex.doc_len = lex_len;
568 28187 p.lex.eof = 1;
569 28187 frx_parse_wire_lex(&p, &oe);
570  
571 28187 ok = frx_lex_prolog(&p.lex);
572 28187 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 27799 388 28187
573 27799 p.lex.prolog_done = 1;
574 27799 ▶ 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 17171 27799
575 27799 d->standalone = p.lex.standalone;
576 }
577 6441410 ▶ while (ok) {
Branch: 1 branch
BranchTrueFalseTotal
while (ok) { 6423583 17827 6441410
578 6423583 step = frx_parse_step(&p);
579 6423583 ▶ if (step == FRX_STEP_OK) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (step == FRX_STEP_OK) continue; 6395784 27799 6423583
580 27799 ▶ 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; } 10360 17439 27799
581 17439 ok = 0; /* FAIL; MORE cannot happen with eof set */
582 }
583 28187 ▶ 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; } 7260 20927 28187
if (ok && p.lex.failed) { p.err = p.lex.err; p.failed = 1; ok = 0; } 0 7260 7260
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 28187 ▶ if (ok && p.lex.n_undecl) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok && p.lex.n_undecl) { 7260 20927 28187
if (ok && p.lex.n_undecl) { 18 7242 7260
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 28187 ▶ 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)) { 7260 20927 28187
if (ok && oe.validate && !frx_validate_doc(d, oe.validate == 2, &p.err)) { 1386 5874 7260
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 28187 ▶ if (ok && oe.xinclude && p.profile == FRX_PROFILE_FULL
Branch: 3 branches
BranchTrueFalseTotal
if (ok && oe.xinclude && p.profile == FRX_PROFILE_FULL 7189 20998 28187
if (ok && oe.xinclude && p.profile == FRX_PROFILE_FULL 179 7010 7189
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 28187 frx_parse_free(&p);
616 28187 frx_tree_sink_free(&ts);
617  
618 28187 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 21023 7164 28187
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 7164 frx_enc_free(&enc); /* the tree holds copies of every string */
631 7164 return d;
632 }
633  
634 /* the strict parse: what the v1 ABI's parse entry means, forever */
635 static frx_doc *
636 81 frx_parse_doc(const char *bytes, size_t len, const frx_opts *o, frx_err *err)
637 {
638 frx_opts_ex oe;
639 81 memset(&oe, 0, sizeof oe);
640 81 oe.size = sizeof oe;
641 81 ▶ if (o) oe.base = *o;
Branch: 1 branch
BranchTrueFalseTotal
if (o) oe.base = *o; 75 6 81
642 81 oe.profile = FRX_PROFILE_STRICT;
643 81 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 »