include/frx/frx_sink.h

filter: Statementstmt 92.3% 170 / 184 Branchbran 73.3% 113 / 154 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.7% 283 / 338 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_xinclude.h ↑ Summary include/frx/frx_plugin.h »
1 #ifndef FRX_SINK_H
2 #define FRX_SINK_H
3  
4 /* What the parse driver produces, and the tree builder as one consumer
5 * of it.
6 *
7 * frx_parse.h resolves names and namespaces, enforces every
8 * well-formedness constraint, merges text, and then calls a sink: a
9 * vtable of start, end, text, comment, pi, doctype and finish. Every sink
10 * sees resolved names and only well-formed sequences, because the checks
11 * live in the driver and not in any sink. The tree sink here builds the
12 * document the codec returns; frx_reader.h's sink queues events for a
13 * pull reader; the reader's subtree capture is this tree sink again,
14 * copying, over one element of a stream.
15 *
16 * Ownership: an event's strings and arrays live in the driver's token
17 * arena. In tree mode that arena is the document's own, so the tree sink
18 * keeps the pointers and copies nothing, exactly as 0.01's parser did. In
19 * copy mode (a subtree cut out of a stream) every string is duplicated
20 * into the document's arena and the namespace chain is rebuilt from the
21 * event's scope, because the reader releases its token arena at the end
22 * of each record.
23 *
24 * Needs frx_tree.h, frx_ns.h; frx_parse.h declares frx_parse and the
25 * event structs and includes this file after them. */
26  
27 typedef struct frx_tree_sink {
28 frx_doc *doc;
29 frx_frame *top; /* the open element's frame; NULL at the top level */
30 frx_idx *ids; /* growing, malloc'd; copied into the arena at finish */
31 int n_ids;
32 int cap_ids;
33 int copy; /* duplicate every string into doc's arena */
34 } frx_tree_sink;
35  
36 static void
37 788934 frx_tree_sink_init(frx_tree_sink *ts, frx_doc *doc, int copy)
38 {
39 788934 memset(ts, 0, sizeof *ts);
40 788934 ts->doc = doc;
41 788934 ts->copy = copy;
42 788934 }
43  
44 static void
45 789788 frx_tree_sink_free(frx_tree_sink *ts)
46 {
47 789788 free(ts->ids);
48 789788 ts->ids = NULL;
49 789788 ts->n_ids = ts->cap_ids = 0;
50 789788 }
51  
52 static frx_node *
53 5383076 frx_tree_sink_parent(frx_tree_sink *ts)
54 {
55 5383076 ▶ return ts->top ? ts->top->node : ts->doc->document;
Branch: 1 branch
BranchTrueFalseTotal
return ts->top ? ts->top->node : ts->doc->document; 4591113 791963 5383076
56 }
57  
58 /* s into the document's arena when copying; s itself otherwise */
59 static int
60 11187066 frx_tree_sink_str(frx_tree_sink *ts, const frx_str *s, frx_str *out)
61 {
62 11187066 ▶ if (!ts->copy) { *out = *s; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ts->copy) { *out = *s; return 1; } 2178303 9008763 11187066
63 9008763 ▶ *out = frx_arena_strndup(&ts->doc->arena, s->p ? s->p : "", s->len);
Branch: 1 branch
BranchTrueFalseTotal
*out = frx_arena_strndup(&ts->doc->arena, s->p ? s->p : "", s->len); 9008763 0 9008763
64 9008763 return out->p != NULL;
65 }
66  
67 static int
68 18254 frx_tree_sink_push_id(frx_parse *p, frx_tree_sink *ts, const frx_attr *a, const frx_node *n)
69 {
70 18254 ▶ if (ts->n_ids == ts->cap_ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (ts->n_ids == ts->cap_ids) { 9416 8838 18254
71 9416 ▶ int ncap = ts->cap_ids ? ts->cap_ids * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = ts->cap_ids ? ts->cap_ids * 2 : 16; 4 9412 9416
72 9416 frx_idx *ni = (frx_idx *)realloc(ts->ids, (size_t)ncap * sizeof *ni);
73 9416 ▶ if (!ni) return frx_parse_nomem(p, a->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!ni) return frx_parse_nomem(p, a->offset); 0 9416 9416
74 9416 ts->ids = ni;
75 9416 ts->cap_ids = ncap;
76 }
77 18254 ts->ids[ts->n_ids].attr = a->local;
78 18254 ts->ids[ts->n_ids].value = a->value;
79 18254 ts->ids[ts->n_ids].node = n;
80 18254 ts->ids[ts->n_ids].offset = a->offset;
81 18254 ts->n_ids++;
82 18254 return 1;
83 }
84  
85 /* An ID attribute: one named in id_attrs by local name in any namespace,
86 * or, under the full profile, xml:id, which the xml:id Recommendation
87 * makes an ID in every document with no declaration. Under
88 * strict xml:id is an ordinary attribute, as in 0.01, so that profile's
89 * index cannot grow an entry. */
90 static int
91 1913153 frx_tree_sink_is_id_attr(const frx_tree_sink *ts, const frx_attr *a)
92 {
93 int i;
94 2209656 ▶ for (i = 0; i < ts->doc->n_id_attrs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ts->doc->n_id_attrs; i++) 314732 1894924 2209656
95 314732 ▶ if (frx_str_eq2(&ts->doc->id_attrs[i], &a->local)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&ts->doc->id_attrs[i], &a->local)) return 1; 18229 296503 314732
96 1894924 ▶ if (ts->doc->profile == FRX_PROFILE_FULL
Branch: 1 branch
BranchTrueFalseTotal
if (ts->doc->profile == FRX_PROFILE_FULL 7943 1886981 1894924
97 7943 ▶ && frx_str_eq(&a->ns, FRX_XML_NS, sizeof FRX_XML_NS - 1)
Branch: 1 branch
BranchTrueFalseTotal
&& frx_str_eq(&a->ns, FRX_XML_NS, sizeof FRX_XML_NS - 1) 285 7658 7943
98 285 ▶ && frx_str_eq(&a->local, "id", 2)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
&& frx_str_eq(&a->local, "id", 2)) return 1; 28 257 285
99 1894896 return 0;
100 }
101  
102 /* the element's scope when copying: the subtree's root flattens the
103 * whole chain it inherits into one record (nearest binding first, which
104 * is the order lookup walks), a descendant with declarations gets a
105 * record over its parent's, one without shares its parent's */
106 static const frx_scope *
107 2282193 frx_tree_sink_scope(frx_tree_sink *ts, const frx_ev_start *ev, const frx_nsdecl *own, int n_own)
108 {
109 2282193 frx_arena *a = &ts->doc->arena;
110 frx_scope *s;
111 2282193 ▶ if (ts->top) {
Branch: 1 branch
BranchTrueFalseTotal
if (ts->top) { 1521457 760736 2282193
112 1521457 ▶ if (!n_own) return ts->top->node->scope;
Branch: 1 branch
BranchTrueFalseTotal
if (!n_own) return ts->top->node->scope; 1521457 0 1521457
113 0 s = (frx_scope *)frx_arena_alloc(a, sizeof *s);
114 0 ▶ if (!s) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return NULL; 0 0 0
115 0 s->up = ts->top->node->scope;
116 0 s->decls = own;
117 0 s->n_decls = n_own;
118 0 return s;
119 }
120 {
121 const frx_scope *c;
122 760736 int total = 0, k = 0;
123 frx_nsdecl *all;
124 1521469 ▶ for (c = ev->scope; c; c = c->up) total += c->n_decls;
Branch: 1 branch
BranchTrueFalseTotal
for (c = ev->scope; c; c = c->up) total += c->n_decls; 760733 760736 1521469
125 760736 ▶ if (!total) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!total) return NULL; 3 760733 760736
126 760733 all = (frx_nsdecl *)frx_arena_alloc(a, (size_t)total * sizeof *all);
127 760733 s = (frx_scope *)frx_arena_alloc(a, sizeof *s);
128 760733 ▶ if (!all || !s) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!all || !s) return NULL; 760733 0 760733
if (!all || !s) return NULL; 0 760733 760733
129 1521466 ▶ for (c = ev->scope; c; c = c->up) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = ev->scope; c; c = c->up) { 760733 760733 1521466
130 int i;
131 2242197 ▶ for (i = 0; i < c->n_decls; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_decls; i++) { 1481464 760733 2242197
132 1481464 all[k].prefix = frx_arena_strndup(a, c->decls[i].prefix.p, c->decls[i].prefix.len);
133 1481464 all[k].uri = frx_arena_strndup(a, c->decls[i].uri.p, c->decls[i].uri.len);
134 1481464 ▶ if (!all[k].prefix.p || !all[k].uri.p) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!all[k].prefix.p || !all[k].uri.p) return NULL; 1481464 0 1481464
if (!all[k].prefix.p || !all[k].uri.p) return NULL; 0 1481464 1481464
135 1481464 k++;
136 }
137 }
138 760733 s->up = NULL;
139 760733 s->decls = all;
140 760733 s->n_decls = total;
141 760733 return s;
142 }
143 }
144  
145 static int
146 3363633 frx_tree_sink_start(frx_parse *p, void *ud, const frx_ev_start *ev)
147 {
148 3363633 frx_tree_sink *ts = (frx_tree_sink *)ud;
149 3363633 frx_doc *d = ts->doc;
150 3363633 frx_node *n = frx_node_new(d, FRX_ELEMENT, ev->offset);
151 int i;
152 3363633 ▶ if (!n) return frx_parse_nomem(p, ev->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return frx_parse_nomem(p, ev->offset); 0 3363633 3363633
153  
154 3363633 ▶ if (!frx_tree_sink_str(ts, &ev->qname, &n->qname)) return frx_parse_nomem(p, ev->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_tree_sink_str(ts, &ev->qname, &n->qname)) return frx_parse_nomem(p, ev->offset); 0 3363633 3363633
155 3363633 frx_ns_split(&n->qname, &n->prefix, &n->local);
156 3363633 ▶ if (!frx_tree_sink_str(ts, &ev->ns, &n->ns)) return frx_parse_nomem(p, ev->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_tree_sink_str(ts, &ev->ns, &n->ns)) return frx_parse_nomem(p, ev->offset); 0 3363633 3363633
157 3363633 ▶ if (ev->xmlns_xml) n->from_ref |= FRX_NODE_XMLNS_XML;
Branch: 1 branch
BranchTrueFalseTotal
if (ev->xmlns_xml) n->from_ref |= FRX_NODE_XMLNS_XML; 120 3363513 3363633
158  
159 3363633 ▶ if (ev->n_decls) {
Branch: 1 branch
BranchTrueFalseTotal
if (ev->n_decls) { 35041 3328592 3363633
160 35041 ▶ if (ts->copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (ts->copy) { 0 35041 35041
161 0 frx_nsdecl *decls = (frx_nsdecl *)frx_arena_alloc(&d->arena, (size_t)ev->n_decls * sizeof *decls);
162 0 ▶ if (!decls) return frx_parse_nomem(p, ev->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!decls) return frx_parse_nomem(p, ev->offset); 0 0 0
163 0 ▶ for (i = 0; i < ev->n_decls; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ev->n_decls; i++) { 0 0 0
164 0 ▶ if (!frx_tree_sink_str(ts, &ev->decls[i].prefix, &decls[i].prefix)
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_tree_sink_str(ts, &ev->decls[i].prefix, &decls[i].prefix) 0 0 0
165 0 ▶ || !frx_tree_sink_str(ts, &ev->decls[i].uri, &decls[i].uri))
Branch: 1 branch
BranchTrueFalseTotal
|| !frx_tree_sink_str(ts, &ev->decls[i].uri, &decls[i].uri)) 0 0 0
166 0 return frx_parse_nomem(p, ev->offset);
167 }
168 0 n->decls = decls;
169 } else {
170 35041 n->decls = ev->decls;
171 }
172 35041 n->n_decls = ev->n_decls;
173 }
174 3363633 ▶ if (ts->copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (ts->copy) { 2282193 1081440 3363633
175 2282193 n->scope = frx_tree_sink_scope(ts, ev, n->decls, n->n_decls);
176 2282193 ▶ if (!n->scope && (ev->scope || n->n_decls)) return frx_parse_nomem(p, ev->offset);
Branch: 3 branches
BranchTrueFalseTotal
if (!n->scope && (ev->scope || n->n_decls)) return frx_parse_nomem(p, ev->offset); 7 2282186 2282193
if (!n->scope && (ev->scope || n->n_decls)) return frx_parse_nomem(p, ev->offset); 7 0 7
if (!n->scope && (ev->scope || n->n_decls)) return frx_parse_nomem(p, ev->offset); 0 7 7
177 } else {
178 1081440 n->scope = ev->scope;
179 }
180  
181 3363633 ▶ if (ev->n_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ev->n_attrs) { 1113384 2250249 3363633
182 1113384 ▶ if (ts->copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (ts->copy) { 760732 352652 1113384
183 760732 frx_attr *attrs = (frx_attr *)frx_arena_alloc(&d->arena, (size_t)ev->n_attrs * sizeof *attrs);
184 760732 ▶ if (!attrs) return frx_parse_nomem(p, ev->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!attrs) return frx_parse_nomem(p, ev->offset); 0 760732 760732
185 2242189 ▶ for (i = 0; i < ev->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ev->n_attrs; i++) { 1481457 760732 2242189
186 1481457 attrs[i] = ev->attrs[i];
187 1481457 ▶ if (!frx_tree_sink_str(ts, &ev->attrs[i].qname, &attrs[i].qname)
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_tree_sink_str(ts, &ev->attrs[i].qname, &attrs[i].qname) 1481457 0 1481457
188 1481457 ▶ || !frx_tree_sink_str(ts, &ev->attrs[i].value, &attrs[i].value)
Branch: 1 branch
BranchTrueFalseTotal
|| !frx_tree_sink_str(ts, &ev->attrs[i].value, &attrs[i].value) 1481457 0 1481457
189 1481457 ▶ || !frx_tree_sink_str(ts, &ev->attrs[i].ns, &attrs[i].ns))
Branch: 1 branch
BranchTrueFalseTotal
|| !frx_tree_sink_str(ts, &ev->attrs[i].ns, &attrs[i].ns)) 0 1481457 1481457
190 0 return frx_parse_nomem(p, ev->offset);
191 1481457 frx_ns_split(&attrs[i].qname, &attrs[i].prefix, &attrs[i].local);
192 }
193 760732 n->attrs = attrs;
194 } else {
195 352652 n->attrs = ev->attrs;
196 }
197 1113384 n->n_attrs = ev->n_attrs;
198 3026534 ▶ for (i = 0; i < n->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->n_attrs; i++) { 1913153 1113381 3026534
199 1913153 const frx_attr *a = &n->attrs[i];
200 1913153 ▶ if (!frx_tree_sink_is_id_attr(ts, a)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_tree_sink_is_id_attr(ts, a)) continue; 1894896 18257 1913153
201 18257 ▶ if (frx_str_eq(&a->ns, FRX_XML_NS, sizeof FRX_XML_NS - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(&a->ns, FRX_XML_NS, sizeof FRX_XML_NS - 1)) { 28 18229 18257
202 /* xml:id section 4: the value must be an NCName */
203 28 ▶ if (!frx_is_ncname(a->value.p, a->value.len))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_is_ncname(a->value.p, a->value.len)) 3 25 28
204 3 return FRX_PARSE_FAIL(p, FRX_E_SYNTAX, a->offset,
205 "an xml:id value must be an NCName");
206 25 ▶ if (d->id_source == FRX_ID_NONE) d->id_source = FRX_ID_XMLID;
Branch: 1 branch
BranchTrueFalseTotal
if (d->id_source == FRX_ID_NONE) d->id_source = FRX_ID_XMLID; 16 9 25
207 }
208 18254 ▶ if (!frx_tree_sink_push_id(p, ts, a, n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_tree_sink_push_id(p, ts, a, n)) return 0; 0 18254 18254
209 }
210 }
211  
212 3363630 frx_node_append(frx_tree_sink_parent(ts), n);
213 3363630 ▶ if (!ts->top) d->root = n;
Branch: 1 branch
BranchTrueFalseTotal
if (!ts->top) d->root = n; 785096 2578534 3363630
214 3363630 ▶ if (!ev->empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ev->empty) { 2948019 415611 3363630
215 2948019 frx_frame *f = (frx_frame *)frx_arena_alloc(&d->arena, sizeof *f);
216 2948019 ▶ if (!f) return frx_parse_nomem(p, ev->offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!f) return frx_parse_nomem(p, ev->offset); 0 2948019 2948019
217 2948019 f->node = n;
218 2948019 f->up = ts->top;
219 2948019 ts->top = f;
220 }
221 3363630 return 1;
222 }
223  
224 static int
225 2872612 frx_tree_sink_end(frx_parse *p, void *ud, const frx_str *qname, size_t offset)
226 {
227 2872612 frx_tree_sink *ts = (frx_tree_sink *)ud;
228 (void)p; (void)qname; (void)offset;
229 2872612 ▶ if (ts->top) ts->top = ts->top->up;
Branch: 1 branch
BranchTrueFalseTotal
if (ts->top) ts->top = ts->top->up; 2872612 0 2872612
230 2872612 return 1;
231 }
232  
233 static int
234 2008857 frx_tree_sink_text(frx_parse *p, void *ud, const char *s, size_t n,
235 const frx_span *spans, int n_spans, size_t offset, const frx_str *entity,
236 int text_flags)
237 {
238 2008857 frx_tree_sink *ts = (frx_tree_sink *)ud;
239 frx_node *node;
240 (void)entity;
241 /* A reference whose replacement text is empty produces a run with no
242 * characters. There is no text node for it - the data model has none,
243 * and one would change every walk and every canonical form - but the
244 * reference did stand in the element's content, which is what an
245 * element declared EMPTY may not have (erratum E15, case E15a).
246 * An empty CDATA section is not this: it is still a section, it still
247 * has a span, and it still makes the content character data. */
248 2008857 ▶ if (n == 0 && n_spans == 0 && (text_flags & FRX_TEXT_REF_HERE)) {
Branch: 3 branches
BranchTrueFalseTotal
if (n == 0 && n_spans == 0 && (text_flags & FRX_TEXT_REF_HERE)) { 52 2008805 2008857
if (n == 0 && n_spans == 0 && (text_flags & FRX_TEXT_REF_HERE)) { 51 1 52
if (n == 0 && n_spans == 0 && (text_flags & FRX_TEXT_REF_HERE)) { 51 0 51
249 51 frx_node *parent = frx_tree_sink_parent(ts);
250 51 ▶ if (parent) parent->from_ref |= FRX_NODE_FROM_REF;
Branch: 1 branch
BranchTrueFalseTotal
if (parent) parent->from_ref |= FRX_NODE_FROM_REF; 51 0 51
251 51 return 1;
252 }
253 2008806 node = frx_node_new(ts->doc, FRX_TEXT, offset);
254 2008806 ▶ if (!node) return frx_parse_nomem(p, offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!node) return frx_parse_nomem(p, offset); 0 2008806 2008806
255 2008806 node->from_ref = (text_flags & FRX_TEXT_REF_CHARS) ? FRX_NODE_FROM_REF : 0;
256 2008806 ▶ node->value = frx_arena_strndup(&ts->doc->arena, s ? s : "", n);
Branch: 1 branch
BranchTrueFalseTotal
node->value = frx_arena_strndup(&ts->doc->arena, s ? s : "", n); 2008806 0 2008806
257 2008806 ▶ if (!node->value.p) return frx_parse_nomem(p, offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!node->value.p) return frx_parse_nomem(p, offset); 0 2008806 2008806
258 2008806 ▶ if (n_spans) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_spans) { 175 2008631 2008806
259 175 frx_span *sp = (frx_span *)frx_arena_alloc(&ts->doc->arena, (size_t)n_spans * sizeof *sp);
260 175 ▶ if (!sp) return frx_parse_nomem(p, offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!sp) return frx_parse_nomem(p, offset); 0 175 175
261 175 memcpy(sp, spans, (size_t)n_spans * sizeof *sp);
262 175 node->cdata_spans = sp;
263 175 node->n_cdata_spans = n_spans;
264 }
265 2008806 frx_node_append(frx_tree_sink_parent(ts), node);
266 2008806 return 1;
267 }
268  
269 static int
270 10589 frx_tree_sink_misc(frx_parse *p, frx_tree_sink *ts, int kind, const frx_str *name, const frx_str *value, size_t offset)
271 {
272 10589 frx_node *node = frx_node_new(ts->doc, kind, offset);
273 10589 ▶ if (!node) return frx_parse_nomem(p, offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!node) return frx_parse_nomem(p, offset); 0 10589 10589
274 10589 ▶ if (!frx_tree_sink_str(ts, value, &node->value)) return frx_parse_nomem(p, offset);
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_tree_sink_str(ts, value, &node->value)) return frx_parse_nomem(p, offset); 0 10589 10589
275 10589 ▶ if (name && !frx_tree_sink_str(ts, name, &node->local)) return frx_parse_nomem(p, offset);
Branch: 2 branches
BranchTrueFalseTotal
if (name && !frx_tree_sink_str(ts, name, &node->local)) return frx_parse_nomem(p, offset); 4840 5749 10589
if (name && !frx_tree_sink_str(ts, name, &node->local)) return frx_parse_nomem(p, offset); 0 4840 4840
276 10589 frx_node_append(frx_tree_sink_parent(ts), node);
277 10589 return 1;
278 }
279  
280 static int
281 5749 frx_tree_sink_comment(frx_parse *p, void *ud, const frx_str *value, size_t offset)
282 {
283 5749 return frx_tree_sink_misc(p, (frx_tree_sink *)ud, FRX_COMMENT, NULL, value, offset);
284 }
285  
286 static int
287 4840 frx_tree_sink_pi(frx_parse *p, void *ud, const frx_str *target, const frx_str *data, size_t offset)
288 {
289 4840 return frx_tree_sink_misc(p, (frx_tree_sink *)ud, FRX_PI, target, data, offset);
290 }
291  
292 static int
293 3775 frx_tree_sink_doctype(frx_parse *p, void *ud, const frx_doctype *dt, const frx_dtd *dtd)
294 {
295 3775 frx_tree_sink *ts = (frx_tree_sink *)ud;
296 (void)p;
297 3775 ts->doc->doctype = dt;
298 3775 ts->doc->dtd = dtd;
299 3775 return 1;
300 }
301  
302 static int
303 20529 frx_str_cmp(const frx_str *a, const frx_str *b)
304 {
305 20529 size_t n = a->len < b->len ? a->len : b->len;
306 20529 ▶ int c = n ? memcmp(a->p, b->p, n) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int c = n ? memcmp(a->p, b->p, n) : 0; 18772 1757 20529
307 20529 ▶ if (c) return c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c; 9873 10656 20529
308 10656 ▶ return a->len < b->len ? -1 : a->len > b->len ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return a->len < b->len ? -1 : a->len > b->len ? 1 : 0; 10204 452 10656
309 }
310  
311 /* the search order: (attr, value) */
312 static int
313 8612 frx_idx_key_cmp(const void *pa, const void *pb)
314 {
315 8612 const frx_idx *a = (const frx_idx *)pa, *b = (const frx_idx *)pb;
316 8612 int c = frx_str_cmp(&a->attr, &b->attr);
317 8612 ▶ return c ? c : frx_str_cmp(&a->value, &b->value);
Branch: 1 branch
BranchTrueFalseTotal
return c ? c : frx_str_cmp(&a->value, &b->value); 8593 19 8612
318 }
319  
320 /* the sort order: the key, then the offset, so among equal keys the
321 * later one sorts after and the duplicate check cites it */
322 static int
323 8492 frx_idx_cmp(const void *pa, const void *pb)
324 {
325 8492 const frx_idx *a = (const frx_idx *)pa, *b = (const frx_idx *)pb;
326 8492 int c = frx_idx_key_cmp(pa, pb);
327 8492 ▶ if (c) return c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c; 8488 4 8492
328 4 ▶ return a->offset < b->offset ? -1 : a->offset > b->offset ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return a->offset < b->offset ? -1 : a->offset > b->offset ? 1 : 0; 0 4 4
329 }
330  
331 /* sort, refuse duplicates, move the index into the arena. Two elements
332 * sharing a value are refused rather than resolved to either, because
333 * "which one" is the whole of the signature wrapping attack. */
334 static int
335 7264 frx_tree_sink_finish(frx_parse *p, void *ud)
336 {
337 7264 frx_tree_sink *ts = (frx_tree_sink *)ud;
338 frx_idx *in_arena;
339 int i;
340 7264 ▶ if (!ts->n_ids) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!ts->n_ids) return 1; 5283 1981 7264
341 1981 qsort(ts->ids, (size_t)ts->n_ids, sizeof *ts->ids, frx_idx_cmp);
342 4258 ▶ for (i = 1; i < ts->n_ids; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < ts->n_ids; i++) { 2281 1977 4258
343 2281 ▶ if (frx_str_eq2(&ts->ids[i].attr, &ts->ids[i - 1].attr)
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&ts->ids[i].attr, &ts->ids[i - 1].attr) 2278 3 2281
344 2278 ▶ && frx_str_eq2(&ts->ids[i].value, &ts->ids[i - 1].value))
Branch: 1 branch
BranchTrueFalseTotal
&& frx_str_eq2(&ts->ids[i].value, &ts->ids[i - 1].value)) 4 2274 2278
345 4 return FRX_PARSE_FAIL(p, FRX_E_DUP_ID, ts->ids[i].offset,
346 "two elements carry the same ID value");
347 }
348 1977 in_arena = (frx_idx *)frx_arena_alloc(&ts->doc->arena, (size_t)ts->n_ids * sizeof *in_arena);
349 1977 ▶ if (!in_arena) return frx_parse_nomem(p, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!in_arena) return frx_parse_nomem(p, 0); 0 1977 1977
350 1977 memcpy(in_arena, ts->ids, (size_t)ts->n_ids * sizeof *in_arena);
351 1977 ts->doc->ids = in_arena;
352 1977 ts->doc->n_ids = ts->n_ids;
353 1977 return 1;
354 }
355  
356 static const frx_sink FRX_TREE_SINK = {
357 frx_tree_sink_start, frx_tree_sink_end, frx_tree_sink_text,
358 frx_tree_sink_comment, frx_tree_sink_pi, frx_tree_sink_doctype,
359 frx_tree_sink_finish
360 };
361  
362 #endif /* FRX_SINK_H */
« include/frx/frx_xinclude.h ↑ Summary include/frx/frx_plugin.h »