include/frx/frx_c14n.h

filter: Statementstmt 98.7% 308 / 312 Branchbran 86.5% 244 / 282 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.9% 552 / 594 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_xmlattr.h ↑ Summary xs/node.xs »
1 #ifndef FRX_C14N_H
2 #define FRX_C14N_H
3  
4 /* Canonical XML: the serialiser, the node set, and the three algorithms.
5 *
6 * THE NODE SET is the apex's subtree minus every `without` subtree. A node
7 * in `without` excludes itself, its attributes, its declarations and
8 * everything below it; one outside the apex's subtree excludes nothing;
9 * one equal to the apex renders nothing. Exclusion never orphans a node -
10 * a node's ancestors are in the set whenever the node is - so exclusion
11 * never triggers the ancestor-inheritance rules, and that is why the
12 * enveloped-signature transform can be an exclusion during serialisation
13 * rather than an edit of the tree. The tree is never mutated.
14 *
15 * The only element whose ancestors are outside the set is therefore the
16 * apex, and the ancestor rules of each algorithm apply to it alone.
17 *
18 * RENDERING, shared by every mode (Canonical XML 1.0 section 2.3):
19 * - names as written; declarations before attributes; declarations
20 * sorted by prefix with the default first; attributes sorted by
21 * namespace URI then local name, the empty URI least;
22 * - text escapes & < > and CR; attribute values escape & < " TAB CR LF
23 * (> is not escaped in a value, " is not escaped in text);
24 * - comment and PI bodies unescaped; <?target data?> or <?target?>;
25 * - <a></a>, never <a/>; comments only when asked; PIs always; the
26 * declaration and any DOCTYPE never;
27 * - a document apex renders top-level comments and PIs with a newline
28 * after each one before the root and before each one after it.
29 *
30 * NAMESPACES, inclusive (Canonical XML 1.0 and 1.1): an element renders an
31 * in-scope binding unless its parent in the output has the same binding
32 * in scope; xmlns="" is rendered only when a non-empty default was in
33 * scope above; the xml prefix is never rendered. For the apex, whose
34 * parent is outside the set, every in-scope binding is rendered.
35 * Exclusive renders only what is visibly utilised.
36 *
37 * THE APEX AND xml: ATTRIBUTES. Canonical XML 1.0 section 2.4: the nearest
38 * occurrence of every attribute in the xml namespace on the omitted
39 * ancestors is copied onto the apex unless the apex has one of that name.
40 * Canonical XML 1.1 section 2.4: only xml:lang and xml:space are simple
41 * inheritable and are copied that way; xml:id is never copied; xml:base
42 * is joined - the values on the omitted ancestors and the apex, outermost
43 * first, reduced from the innermost pair outward through the spec's
44 * "join-URI-References", which is RFC 3986 sections 5.2.1, 5.2.2 and
45 * 5.2.4 with these changes: the base needs no scheme; a trailing ".." on
46 * the base becomes "../"; remove-dot-segments keeps leading "../"
47 * segments, collapses runs of "/", and appends "/" to a trailing "..";
48 * the reference's fragment is dropped. The join is performed only when an
49 * omitted ancestor had an xml:base, and an empty result is not rendered.
50 * Every other xml: attribute is ordinary. Exclusive inherits nothing.
51 *
52 * Output is UTF-8 bytes into an frx_buf; the SV layer wraps them once
53 * with no character flag, because a signature is over bytes.
54 *
55 * THE DOCUMENT'S VERSION IS NOT CONSULTED. Canonical XML and Exclusive
56 * XML Canonicalization are defined over the data model - nodes, names,
57 * values - and say nothing about the XML version that produced it. An
58 * XML 1.1 document (full profile) reaches here as the same kind of tree,
59 * its NEL and LS line ends already LF and its referenced restricted
60 * characters already characters, and canonicalises under all three
61 * algorithms exactly as a 1.0 document does; no declaration is ever
62 * rendered, so no version is either.
63 *
64 * Nothing recurses (rule 2): the walk is iterative, the end tags emitted
65 * on the way back up. malloc for the scratch arrays, freed before return.
66 *
67 * Needs frx_abi.h, frx_buf.h, frx_uri.h, frx_tree.h, frx_ns.h, frx_parse.h. */
68  
69 /* ---- escaping --------------------------------------------------------- */
70  
71 static void
72 206029 frx_c14n_esc_text(frx_buf *o, const char *p, size_t n)
73 {
74 206029 size_t i, run = 0;
75 5071232 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4865203 206029 5071232
76 4865203 const char *rep = NULL;
77 4865203 switch (p[i]) {
78 152 case '&': rep = "&amp;"; break;
79 407 case '<': rep = "&lt;"; break;
80 187 case '>': rep = "&gt;"; break;
81 65 case '\r': rep = "&#xD;"; break;
82 4864392 default: break;
83 }
84 4865203 ▶ if (rep) {
Branch: 1 branch
BranchTrueFalseTotal
if (rep) { 811 4864392 4865203
85 811 ▶ if (i > run) frx_buf_append_n(o, p + run, i - run);
Branch: 1 branch
BranchTrueFalseTotal
if (i > run) frx_buf_append_n(o, p + run, i - run); 640 171 811
86 811 frx_buf_append(o, rep);
87 811 run = i + 1;
88 }
89 }
90 206029 ▶ if (n > run) frx_buf_append_n(o, p + run, n - run);
Branch: 1 branch
BranchTrueFalseTotal
if (n > run) frx_buf_append_n(o, p + run, n - run); 205947 82 206029
91 206029 }
92  
93 static void
94 205117 frx_c14n_esc_attr(frx_buf *o, const char *p, size_t n)
95 {
96 205117 size_t i, run = 0;
97 3540111 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3334994 205117 3540111
98 3334994 const char *rep = NULL;
99 3334994 switch (p[i]) {
100 268 case '&': rep = "&amp;"; break;
101 41 case '<': rep = "&lt;"; break;
102 172 case '"': rep = "&quot;"; break;
103 35 case '\t': rep = "&#x9;"; break;
104 36 case '\n': rep = "&#xA;"; break;
105 38 case '\r': rep = "&#xD;"; break;
106 3334404 default: break;
107 }
108 3334994 ▶ if (rep) {
Branch: 1 branch
BranchTrueFalseTotal
if (rep) { 590 3334404 3334994
109 590 ▶ if (i > run) frx_buf_append_n(o, p + run, i - run);
Branch: 1 branch
BranchTrueFalseTotal
if (i > run) frx_buf_append_n(o, p + run, i - run); 200 390 590
110 590 frx_buf_append(o, rep);
111 590 run = i + 1;
112 }
113 }
114 205117 ▶ if (n > run) frx_buf_append_n(o, p + run, n - run);
Branch: 1 branch
BranchTrueFalseTotal
if (n > run) frx_buf_append_n(o, p + run, n - run); 204752 365 205117
115 205117 }
116  
117 /* ---- the xml:base join: frx_uri.h in its Canonical XML 1.1 form ------- */
118  
119 static int
120 19 frx_join_base(frx_buf *out, const char *base, size_t blen, const char *ref, size_t rlen)
121 {
122 19 return frx_uri_join(out, base, blen, ref, rlen, 1);
123 }
124  
125 /* ---- the renderer ------------------------------------------------------ */
126  
127 typedef struct frx_c14n_ns {
128 frx_str prefix;
129 frx_str uri;
130 } frx_c14n_ns;
131  
132 /* an attribute to render: the element's own, or one inherited onto the apex */
133 typedef struct frx_c14n_attr {
134 frx_str ns;
135 frx_str local;
136 frx_str qname;
137 frx_str value;
138 } frx_c14n_attr;
139  
140 typedef struct frx_c14n_ctx {
141 const frx_c14n *c;
142 frx_buf *out;
143 const frx_node *apex;
144 int failed;
145 frx_c14n_ns *ns; int n_ns, cap_ns; /* this element's rendered declarations */
146 frx_c14n_attr *at; int n_at, cap_at; /* this element's rendered attributes */
147 frx_buf base; /* the joined xml:base, when any */
148 /* ns_rendered of the Recommendation's section 3.1: every declaration an
149 * output ancestor rendered, nearest last, with a mark per open element
150 * so the end tag restores the ancestor's view */
151 frx_c14n_ns *rendered; int n_rendered, cap_rendered;
152 int *marks; int n_marks, cap_marks;
153 } frx_c14n_ctx;
154  
155 static int
156 419051 frx_c14n_omitted(const frx_c14n *c, const frx_node *n)
157 {
158 int i;
159 420398 ▶ for (i = 0; i < c->n_without; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_without; i++) 1361 419037 420398
160 1361 ▶ if (c->without[i] == n) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c->without[i] == n) return 1; 14 1347 1361
161 419037 return 0;
162 }
163  
164 static int
165 94 frx_c14n_is_xml_ns(const frx_str *ns)
166 {
167 94 return frx_str_eq(ns, FRX_XML_NS, sizeof FRX_XML_NS - 1);
168 }
169  
170 static int
171 209453 frx_c14n_push_ns(frx_c14n_ctx *x, const frx_str *prefix, const frx_str *uri)
172 {
173 209453 ▶ if (x->n_ns == x->cap_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->n_ns == x->cap_ns) { 2707 206746 209453
174 2707 ▶ int ncap = x->cap_ns ? x->cap_ns * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = x->cap_ns ? x->cap_ns * 2 : 16; 0 2707 2707
175 2707 frx_c14n_ns *nn = (frx_c14n_ns *)realloc(x->ns, (size_t)ncap * sizeof *nn);
176 2707 ▶ if (!nn) { x->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nn) { x->failed = 1; return 0; } 0 2707 2707
177 2707 x->ns = nn; x->cap_ns = ncap;
178 }
179 209453 x->ns[x->n_ns].prefix = *prefix;
180 209453 x->ns[x->n_ns].uri = *uri;
181 209453 x->n_ns++;
182 209453 return 1;
183 }
184  
185 static int
186 203069 frx_c14n_push_attr(frx_c14n_ctx *x, const frx_str *ns, const frx_str *local,
187 const frx_str *qname, const frx_str *value)
188 {
189 203069 ▶ if (x->n_at == x->cap_at) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->n_at == x->cap_at) { 1000 202069 203069
190 1000 ▶ int ncap = x->cap_at ? x->cap_at * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = x->cap_at ? x->cap_at * 2 : 16; 0 1000 1000
191 1000 frx_c14n_attr *na = (frx_c14n_attr *)realloc(x->at, (size_t)ncap * sizeof *na);
192 1000 ▶ if (!na) { x->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!na) { x->failed = 1; return 0; } 0 1000 1000
193 1000 x->at = na; x->cap_at = ncap;
194 }
195 203069 x->at[x->n_at].ns = *ns;
196 203069 x->at[x->n_at].local = *local;
197 203069 x->at[x->n_at].qname = *qname;
198 203069 x->at[x->n_at].value = *value;
199 203069 x->n_at++;
200 203069 return 1;
201 }
202  
203 /* is prefix already in x->ns? */
204 static int
205 2835 frx_c14n_ns_seen(const frx_c14n_ctx *x, const frx_str *prefix)
206 {
207 int i;
208 4664 ▶ for (i = 0; i < x->n_ns; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->n_ns; i++) 2223 2441 4664
209 2223 ▶ if (frx_str_eq2(&x->ns[i].prefix, prefix)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&x->ns[i].prefix, prefix)) return 1; 394 1829 2223
210 2441 return 0;
211 }
212  
213 /* is prefix in the PrefixList ("#default" names the default)? */
214 static int
215 108971 frx_c14n_in_prefix_list(const frx_c14n *c, const frx_str *prefix)
216 {
217 int i;
218 108977 ▶ for (i = 0; i < c->n_prefix; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_prefix; i++) { 8 108969 108977
219 8 const char *p = c->prefix_list[i];
220 8 ▶ if (prefix->len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (prefix->len == 0) { 3 5 8
221 3 ▶ if (strcmp(p, "#default") == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(p, "#default") == 0) return 1; 1 2 3
222 5 ▶ } else if (frx_str_eq(prefix, p, strlen(p))) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (frx_str_eq(prefix, p, strlen(p))) { 1 4 5
223 1 return 1;
224 }
225 }
226 108969 return 0;
227 }
228  
229 static int
230 56 frx_c14n_cmp_ns(const void *pa, const void *pb)
231 {
232 56 const frx_c14n_ns *a = (const frx_c14n_ns *)pa, *b = (const frx_c14n_ns *)pb;
233 56 return frx_str_cmp(&a->prefix, &b->prefix);
234 }
235  
236 static int
237 1758 frx_c14n_cmp_attr(const void *pa, const void *pb)
238 {
239 1758 const frx_c14n_attr *a = (const frx_c14n_attr *)pa, *b = (const frx_c14n_attr *)pb;
240 1758 int c = frx_str_cmp(&a->ns, &b->ns);
241 1758 ▶ return c ? c : frx_str_cmp(&a->local, &b->local);
Branch: 1 branch
BranchTrueFalseTotal
return c ? c : frx_str_cmp(&a->local, &b->local); 1481 277 1758
242 }
243  
244 /* what the output ancestors rendered for prefix, nearest wins; NULL when
245 * none did */
246 static const frx_str *
247 207816 frx_c14n_rendered_value(const frx_c14n_ctx *x, const frx_str *prefix)
248 {
249 int i;
250 208482 ▶ for (i = x->n_rendered - 1; i >= 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = x->n_rendered - 1; i >= 0; i--) 1301 207181 208482
251 1301 ▶ if (frx_str_eq2(&x->rendered[i].prefix, prefix)) return &x->rendered[i].uri;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&x->rendered[i].prefix, prefix)) return &x->rendered[i].uri; 635 666 1301
252 207181 return NULL;
253 }
254  
255 /* remember what this element rendered, behind a mark its end tag pops */
256 static int
257 207686 frx_c14n_push_frame(frx_c14n_ctx *x)
258 {
259 int i;
260 207686 ▶ if (x->n_marks == x->cap_marks) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->n_marks == x->cap_marks) { 2720 204966 207686
261 2720 ▶ int ncap = x->cap_marks ? x->cap_marks * 2 : 32;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = x->cap_marks ? x->cap_marks * 2 : 32; 13 2707 2720
262 2720 int *nm = (int *)realloc(x->marks, (size_t)ncap * sizeof *nm);
263 2720 ▶ if (!nm) { x->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nm) { x->failed = 1; return 0; } 0 2720 2720
264 2720 x->marks = nm; x->cap_marks = ncap;
265 }
266 207686 x->marks[x->n_marks++] = x->n_rendered;
267 208453 ▶ for (i = 0; i < x->n_ns; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->n_ns; i++) { 767 207686 208453
268 767 ▶ if (x->n_rendered == x->cap_rendered) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->n_rendered == x->cap_rendered) { 140 627 767
269 140 ▶ int ncap = x->cap_rendered ? x->cap_rendered * 2 : 32;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = x->cap_rendered ? x->cap_rendered * 2 : 32; 0 140 140
270 140 frx_c14n_ns *nr = (frx_c14n_ns *)realloc(x->rendered, (size_t)ncap * sizeof *nr);
271 140 ▶ if (!nr) { x->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nr) { x->failed = 1; return 0; } 0 140 140
272 140 x->rendered = nr; x->cap_rendered = ncap;
273 }
274 767 x->rendered[x->n_rendered++] = x->ns[i];
275 }
276 207686 return 1;
277 }
278  
279 static void
280 207686 frx_c14n_pop_frame(frx_c14n_ctx *x)
281 {
282 207686 ▶ if (x->n_marks > 0) x->n_rendered = x->marks[--x->n_marks];
Branch: 1 branch
BranchTrueFalseTotal
if (x->n_marks > 0) x->n_rendered = x->marks[--x->n_marks]; 207686 0 207686
283 207686 }
284  
285 /* Does element e visibly utilise prefix? Its own, or one of its
286 * attributes' (an unprefixed attribute utilises nothing). */
287 static int
288 108969 frx_c14n_utilised(const frx_node *e, const frx_str *prefix)
289 {
290 int i;
291 108969 ▶ if (frx_str_eq2(&e->prefix, prefix)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&e->prefix, prefix)) return 1; 107290 1679 108969
292 1679 ▶ if (prefix->len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (prefix->len == 0) return 0; 630 1049 1679
293 1581 ▶ for (i = 0; i < e->n_attrs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < e->n_attrs; i++) 574 1007 1581
294 574 ▶ if (frx_str_eq2(&e->attrs[i].prefix, prefix)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&e->attrs[i].prefix, prefix)) return 1; 42 532 574
295 1007 return 0;
296 }
297  
298 /* Collect the declarations e renders, into x->ns, sorted.
299 *
300 * Candidates are every in-scope binding, nearest per prefix, with the
301 * default counted as "" when nothing declared it. Inclusive keeps every
302 * candidate; exclusive keeps those e visibly utilises or the PrefixList
303 * names. A candidate is then rendered unless an output ancestor rendered
304 * the same prefix with the same value (ns_rendered, section 3.1 of the
305 * exclusive Recommendation, which for inclusive is the same test as
306 * section 2.3 of Canonical XML). xmlns="" is the one candidate rendered
307 * only when an ancestor rendered a non-empty default: there is nothing to
308 * undo otherwise. */
309 static int
310 207686 frx_c14n_collect_ns(frx_c14n_ctx *x, const frx_node *e)
311 {
312 const frx_scope *s;
313 207686 int exclusive = x->c->mode == FRX_C14N_EXC;
314 207686 int i, w, saw_default = 0;
315  
316 207686 x->n_ns = 0;
317 209229 ▶ for (s = e->scope; s; s = s->up) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = e->scope; s; s = s->up) { 1543 207686 209229
318 4378 ▶ for (i = 0; i < s->n_decls; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n_decls; i++) { 2835 1543 4378
319 2835 const frx_nsdecl *d = &s->decls[i];
320 2835 ▶ if (frx_c14n_ns_seen(x, &d->prefix)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_c14n_ns_seen(x, &d->prefix)) continue; 394 2441 2835
321 2441 ▶ if (!frx_c14n_push_ns(x, &d->prefix, &d->uri)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_c14n_push_ns(x, &d->prefix, &d->uri)) return 0; 0 2441 2441
322 2441 ▶ if (d->prefix.len == 0) saw_default = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (d->prefix.len == 0) saw_default = 1; 674 1767 2441
323 }
324 }
325 207686 ▶ if (!saw_default && !frx_c14n_push_ns(x, &FRX_EMPTY_STR, &FRX_EMPTY_STR)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!saw_default && !frx_c14n_push_ns(x, &FRX_EMPTY_STR, &FRX_EMPTY_STR)) return 0; 207012 674 207686
if (!saw_default && !frx_c14n_push_ns(x, &FRX_EMPTY_STR, &FRX_EMPTY_STR)) return 0; 0 207012 207012
326  
327 207686 w = 0;
328 417139 ▶ for (i = 0; i < x->n_ns; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->n_ns; i++) { 209453 207686 417139
329 209453 const frx_c14n_ns *c = &x->ns[i];
330 const frx_str *have;
331 int keep;
332 209453 ▶ if (exclusive && !frx_c14n_in_prefix_list(x->c, &c->prefix)
Branch: 2 branches
BranchTrueFalseTotal
if (exclusive && !frx_c14n_in_prefix_list(x->c, &c->prefix) 108971 100482 209453
if (exclusive && !frx_c14n_in_prefix_list(x->c, &c->prefix) 108969 2 108971
333 108969 ▶ && !frx_c14n_utilised(e, &c->prefix)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_c14n_utilised(e, &c->prefix)) { 1637 107332 108969
334 1637 keep = 0;
335 } else {
336 207816 have = frx_c14n_rendered_value(x, &c->prefix);
337 207816 ▶ if (c->prefix.len == 0 && c->uri.len == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (c->prefix.len == 0 && c->uri.len == 0) 207056 760 207816
if (c->prefix.len == 0 && c->uri.len == 0) 206916 140 207056
338 206916 ▶ keep = have != NULL && have->len > 0;
Branch: 2 branches
BranchTrueFalseTotal
keep = have != NULL && have->len > 0; 255 206661 206916
keep = have != NULL && have->len > 0; 236 19 255
339 else
340 900 ▶ keep = have == NULL || !frx_str_eq2(have, &c->uri);
Branch: 2 branches
BranchTrueFalseTotal
keep = have == NULL || !frx_str_eq2(have, &c->uri); 380 520 900
keep = have == NULL || !frx_str_eq2(have, &c->uri); 11 369 380
341 }
342 209453 ▶ if (keep) x->ns[w++] = *c;
Branch: 1 branch
BranchTrueFalseTotal
if (keep) x->ns[w++] = *c; 767 208686 209453
343 }
344 207686 x->n_ns = w;
345 207686 ▶ if (x->n_ns > 1) qsort(x->ns, (size_t)x->n_ns, sizeof *x->ns, frx_c14n_cmp_ns);
Branch: 1 branch
BranchTrueFalseTotal
if (x->n_ns > 1) qsort(x->ns, (size_t)x->n_ns, sizeof *x->ns, frx_c14n_cmp_ns); 32 207654 207686
346 207686 return 1;
347 }
348  
349 /* the nearest ancestor attribute in the xml namespace with this local
350 * name, above e (e itself excluded); NULL when none */
351 static const frx_attr *
352 15 frx_c14n_nearest_xml(const frx_node *e, const char *local)
353 {
354 const frx_node *a;
355 31 ▶ for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) {
Branch: 2 branches
BranchTrueFalseTotal
for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) { 31 0 31
for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) { 19 12 31
356 int i;
357 36 ▶ for (i = 0; i < a->n_attrs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->n_attrs; i++) 20 16 36
358 20 ▶ if (frx_c14n_is_xml_ns(&a->attrs[i].ns) && frx_str_eq(&a->attrs[i].local, local, strlen(local)))
Branch: 2 branches
BranchTrueFalseTotal
if (frx_c14n_is_xml_ns(&a->attrs[i].ns) && frx_str_eq(&a->attrs[i].local, local, strlen(local))) 20 0 20
if (frx_c14n_is_xml_ns(&a->attrs[i].ns) && frx_str_eq(&a->attrs[i].local, local, strlen(local))) 3 17 20
359 3 return &a->attrs[i];
360 }
361 12 return NULL;
362 }
363  
364 static const frx_attr *
365 36 frx_c14n_own_xml(const frx_node *e, const char *local)
366 {
367 int i;
368 54 ▶ for (i = 0; i < e->n_attrs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < e->n_attrs; i++) 27 27 54
369 27 ▶ if (frx_c14n_is_xml_ns(&e->attrs[i].ns) && frx_str_eq(&e->attrs[i].local, local, strlen(local)))
Branch: 2 branches
BranchTrueFalseTotal
if (frx_c14n_is_xml_ns(&e->attrs[i].ns) && frx_str_eq(&e->attrs[i].local, local, strlen(local))) 24 3 27
if (frx_c14n_is_xml_ns(&e->attrs[i].ns) && frx_str_eq(&e->attrs[i].local, local, strlen(local))) 9 15 24
370 9 return &e->attrs[i];
371 27 return NULL;
372 }
373  
374 /* Collect the attributes e renders, into x->at, sorted: its own, plus
375 * what the algorithm inherits onto the apex. */
376 static int
377 207686 frx_c14n_collect_attrs(frx_c14n_ctx *x, const frx_node *e)
378 {
379 int i;
380 207686 ▶ int is_apex = e == x->apex && e->parent && e->parent->kind == FRX_ELEMENT;
Branch: 3 branches
BranchTrueFalseTotal
int is_apex = e == x->apex && e->parent && e->parent->kind == FRX_ELEMENT; 2707 204979 207686
int is_apex = e == x->apex && e->parent && e->parent->kind == FRX_ELEMENT; 2706 1 2707
int is_apex = e == x->apex && e->parent && e->parent->kind == FRX_ELEMENT; 55 2651 2706
381  
382 207686 x->n_at = 0;
383 410739 ▶ for (i = 0; i < e->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < e->n_attrs; i++) { 203053 207686 410739
384 203053 const frx_attr *a = &e->attrs[i];
385 203053 ▶ if (!frx_c14n_push_attr(x, &a->ns, &a->local, &a->qname, &a->value)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_c14n_push_attr(x, &a->ns, &a->local, &a->qname, &a->value)) return 0; 0 203053 203053
386 }
387 207686 ▶ if (!is_apex || x->c->mode == FRX_C14N_EXC) goto sort;
Branch: 2 branches
BranchTrueFalseTotal
if (!is_apex || x->c->mode == FRX_C14N_EXC) goto sort; 55 207631 207686
if (!is_apex || x->c->mode == FRX_C14N_EXC) goto sort; 28 27 55
388  
389 27 ▶ if (x->c->mode == FRX_C14N_INC10) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->c->mode == FRX_C14N_INC10) { 19 8 27
390 /* every xml: attribute, nearest occurrence, unless e has it */
391 const frx_node *a;
392 44 ▶ for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) {
Branch: 2 branches
BranchTrueFalseTotal
for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) { 44 0 44
for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) { 25 19 44
393 48 ▶ for (i = 0; i < a->n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->n_attrs; i++) { 23 25 48
394 23 const frx_attr *at = &a->attrs[i];
395 23 int j, have = 0;
396 23 ▶ if (!frx_c14n_is_xml_ns(&at->ns)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_c14n_is_xml_ns(&at->ns)) continue; 6 17 23
397 32 ▶ for (j = 0; j < x->n_at; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < x->n_at; j++) 21 11 32
398 21 ▶ if (frx_c14n_is_xml_ns(&x->at[j].ns) && frx_str_eq2(&x->at[j].local, &at->local)) { have = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (frx_c14n_is_xml_ns(&x->at[j].ns) && frx_str_eq2(&x->at[j].local, &at->local)) { have = 1; break; } 16 5 21
if (frx_c14n_is_xml_ns(&x->at[j].ns) && frx_str_eq2(&x->at[j].local, &at->local)) { have = 1; break; } 6 10 16
399 17 ▶ if (have) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (have) continue; 6 11 17
400 11 ▶ if (!frx_c14n_push_attr(x, &at->ns, &at->local, &at->qname, &at->value)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_c14n_push_attr(x, &at->ns, &at->local, &at->qname, &at->value)) return 0; 0 11 11
401 }
402 }
403 } else {
404 /* 1.1: xml:lang and xml:space simply; xml:base joined; xml:id never */
405 static const char *const simple[] = { "lang", "space" };
406 size_t k;
407 24 ▶ for (k = 0; k < sizeof simple / sizeof simple[0]; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < sizeof simple / sizeof simple[0]; k++) { 16 8 24
408 /* not `near`: windows.h defines it, and the whole file stops
409 * compiling there (tools/mingw/win_macros.h holds the set) */
410 const frx_attr *inherited;
411 16 ▶ if (frx_c14n_own_xml(e, simple[k])) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_c14n_own_xml(e, simple[k])) continue; 1 15 16
412 15 inherited = frx_c14n_nearest_xml(e, simple[k]);
413 15 ▶ if (inherited && !frx_c14n_push_attr(x, &inherited->ns, &inherited->local,
Branch: 2 branches
BranchTrueFalseTotal
if (inherited && !frx_c14n_push_attr(x, &inherited->ns, &inherited->local, 3 12 15
if (inherited && !frx_c14n_push_attr(x, &inherited->ns, &inherited->local, 0 3 3
414 0 &inherited->qname, &inherited->value)) return 0;
415 }
416 {
417 /* the ancestors' xml:base values, outermost first, then e's own */
418 const frx_node *a;
419 8 const frx_attr **vals = NULL;
420 8 int nv = 0, cap = 0, any_ancestor = 0, depth = 0;
421 8 const frx_attr *own = frx_c14n_own_xml(e, "base");
422 20 ▶ for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) depth++;
Branch: 2 branches
BranchTrueFalseTotal
for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) depth++; 20 0 20
for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) depth++; 12 8 20
423 8 ▶ if (depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth) { 8 0 8
424 8 vals = (const frx_attr **)malloc((size_t)(depth + 1) * sizeof *vals);
425 8 ▶ if (!vals) { x->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!vals) { x->failed = 1; return 0; } 0 8 8
426 8 cap = depth + 1;
427 /* fill from the innermost ancestor backwards into the array's tail */
428 8 nv = 0;
429 20 ▶ for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) {
Branch: 2 branches
BranchTrueFalseTotal
for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) { 20 0 20
for (a = e->parent; a && a->kind == FRX_ELEMENT; a = a->parent) { 12 8 20
430 12 const frx_attr *b = frx_c14n_own_xml(a, "base");
431 12 ▶ if (b) { vals[nv++] = b; any_ancestor = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (b) { vals[nv++] = b; any_ancestor = 1; } 5 7 12
432 }
433 /* vals is innermost-first; the reduction wants innermost-first
434 * combining, so this order is the one to consume */
435 }
436 8 ▶ if (any_ancestor) {
Branch: 1 branch
BranchTrueFalseTotal
if (any_ancestor) { 4 4 8
437 /* acc = own (or the innermost ancestor value); then for each
438 * further outer value: acc = join(outer, acc) */
439 frx_buf acc;
440 4 int start = 0;
441 4 frx_buf_init(&acc);
442 4 ▶ if (own) {
Branch: 1 branch
BranchTrueFalseTotal
if (own) { 2 2 4
443 2 frx_buf_append_n(&acc, own->value.p, own->value.len);
444 } else {
445 2 frx_buf_append_n(&acc, vals[0]->value.p, vals[0]->value.len);
446 2 start = 1;
447 }
448 7 ▶ for (i = start; i < nv && !x->failed; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = start; i < nv && !x->failed; i++) { 3 4 7
for (i = start; i < nv && !x->failed; i++) { 3 0 3
449 frx_buf tmp;
450 3 frx_buf_init(&tmp);
451 3 ▶ if (!frx_join_base(&tmp, vals[i]->value.p, vals[i]->value.len,
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_join_base(&tmp, vals[i]->value.p, vals[i]->value.len, 0 3 3
452 3 ▶ acc.p ? acc.p : "", acc.len)) { x->failed = 1; }
Branch: 1 branch
BranchTrueFalseTotal
acc.p ? acc.p : "", acc.len)) { x->failed = 1; } 3 0 3
453 3 frx_buf_free(&acc);
454 3 acc = tmp;
455 }
456 4 x->base.len = 0;
457 4 ▶ frx_buf_append_n(&x->base, acc.p ? acc.p : "", acc.len);
Branch: 1 branch
BranchTrueFalseTotal
frx_buf_append_n(&x->base, acc.p ? acc.p : "", acc.len); 4 0 4
458 4 frx_buf_free(&acc);
459 /* replace e's own xml:base with the joined value, or add it */
460 7 ▶ if (x->base.len && !x->failed) {
Branch: 2 branches
BranchTrueFalseTotal
if (x->base.len && !x->failed) { 3 1 4
if (x->base.len && !x->failed) { 3 0 3
461 static const frx_str q = { "xml:base", 8 };
462 static const frx_str l = { "base", 4 };
463 static const frx_str nsx = { FRX_XML_NS, sizeof FRX_XML_NS - 1 };
464 frx_str v;
465 3 int j, replaced = 0;
466 3 v.p = x->base.p; v.len = x->base.len;
467 4 ▶ for (j = 0; j < x->n_at; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < x->n_at; j++) { 2 2 4
468 2 ▶ if (frx_c14n_is_xml_ns(&x->at[j].ns) && frx_str_eq(&x->at[j].local, "base", 4)) {
Branch: 2 branches
BranchTrueFalseTotal
if (frx_c14n_is_xml_ns(&x->at[j].ns) && frx_str_eq(&x->at[j].local, "base", 4)) { 1 1 2
if (frx_c14n_is_xml_ns(&x->at[j].ns) && frx_str_eq(&x->at[j].local, "base", 4)) { 1 0 1
469 1 x->at[j].value = v; replaced = 1; break;
470 }
471 }
472 3 ▶ if (!replaced && !frx_c14n_push_attr(x, &nsx, &l, &q, &v)) { free(vals); return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (!replaced && !frx_c14n_push_attr(x, &nsx, &l, &q, &v)) { free(vals); return 0; } 2 1 3
if (!replaced && !frx_c14n_push_attr(x, &nsx, &l, &q, &v)) { free(vals); return 0; } 0 2 2
473 1 ▶ } else if (!x->failed) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!x->failed) { 1 0 1
474 /* an empty result: xml:base MUST NOT be rendered */
475 1 int j, w = 0;
476 2 ▶ for (j = 0; j < x->n_at; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < x->n_at; j++) { 1 1 2
477 1 ▶ if (frx_c14n_is_xml_ns(&x->at[j].ns) && frx_str_eq(&x->at[j].local, "base", 4)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (frx_c14n_is_xml_ns(&x->at[j].ns) && frx_str_eq(&x->at[j].local, "base", 4)) continue; 1 0 1
if (frx_c14n_is_xml_ns(&x->at[j].ns) && frx_str_eq(&x->at[j].local, "base", 4)) continue; 1 0 1
478 0 x->at[w++] = x->at[j];
479 }
480 1 x->n_at = w;
481 }
482 }
483 8 free(vals);
484 (void)cap;
485 8 ▶ if (x->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (x->failed) return 0; 0 8 8
486 }
487 }
488 8 sort:
489 207686 ▶ if (x->n_at > 1) qsort(x->at, (size_t)x->n_at, sizeof *x->at, frx_c14n_cmp_attr);
Branch: 1 branch
BranchTrueFalseTotal
if (x->n_at > 1) qsort(x->at, (size_t)x->n_at, sizeof *x->at, frx_c14n_cmp_attr); 547 207139 207686
490 207686 return 1;
491 }
492  
493 static void
494 207686 frx_c14n_start_tag(frx_c14n_ctx *x, const frx_node *e)
495 {
496 207686 frx_buf *o = x->out;
497 int i;
498 207686 ▶ if (!frx_c14n_collect_ns(x, e) || !frx_c14n_collect_attrs(x, e)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_c14n_collect_ns(x, e) || !frx_c14n_collect_attrs(x, e)) return; 207686 0 207686
if (!frx_c14n_collect_ns(x, e) || !frx_c14n_collect_attrs(x, e)) return; 0 207686 207686
499 207686 frx_buf_append_ch(o, '<');
500 207686 frx_buf_append_n(o, e->qname.p, e->qname.len);
501 208453 ▶ for (i = 0; i < x->n_ns; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->n_ns; i++) { 767 207686 208453
502 767 frx_buf_append(o, " xmlns");
503 767 ▶ if (x->ns[i].prefix.len) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->ns[i].prefix.len) { 442 325 767
504 442 frx_buf_append_ch(o, ':');
505 442 frx_buf_append_n(o, x->ns[i].prefix.p, x->ns[i].prefix.len);
506 }
507 767 frx_buf_append(o, "=\"");
508 767 frx_c14n_esc_attr(o, x->ns[i].uri.p, x->ns[i].uri.len);
509 767 frx_buf_append_ch(o, '"');
510 }
511 410754 ▶ for (i = 0; i < x->n_at; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->n_at; i++) { 203068 207686 410754
512 203068 frx_buf_append_ch(o, ' ');
513 203068 frx_buf_append_n(o, x->at[i].qname.p, x->at[i].qname.len);
514 203068 frx_buf_append(o, "=\"");
515 203068 frx_c14n_esc_attr(o, x->at[i].value.p, x->at[i].value.len);
516 203068 frx_buf_append_ch(o, '"');
517 }
518 207686 frx_buf_append_ch(o, '>');
519 207686 frx_c14n_push_frame(x);
520 }
521  
522 static void
523 207686 frx_c14n_end_tag(frx_c14n_ctx *x, const frx_node *e)
524 {
525 207686 frx_buf_append(x->out, "</");
526 207686 frx_buf_append_n(x->out, e->qname.p, e->qname.len);
527 207686 frx_buf_append_ch(x->out, '>');
528 207686 frx_c14n_pop_frame(x);
529 207686 }
530  
531 static void
532 205089 frx_c14n_misc(frx_c14n_ctx *x, const frx_node *n)
533 {
534 205089 frx_buf *o = x->out;
535 205089 switch (n->kind) {
536 204510 case FRX_TEXT:
537 204510 frx_c14n_esc_text(o, n->value.p, n->value.len);
538 204510 break;
539 337 case FRX_COMMENT:
540 337 ▶ if (x->c->comments) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->c->comments) { 36 301 337
541 36 frx_buf_append(o, "<!--");
542 36 frx_buf_append_n(o, n->value.p, n->value.len);
543 36 frx_buf_append(o, "-->");
544 }
545 337 break;
546 242 case FRX_PI:
547 242 frx_buf_append(o, "<?");
548 242 frx_buf_append_n(o, n->local.p, n->local.len);
549 242 ▶ if (n->value.len) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->value.len) { 166 76 242
550 166 frx_buf_append_ch(o, ' ');
551 166 frx_buf_append_n(o, n->value.p, n->value.len);
552 }
553 242 frx_buf_append(o, "?>");
554 242 break;
555 0 default:
556 0 break;
557 }
558 205089 }
559  
560 /* the subtree rooted at the element `top`, iteratively */
561 static void
562 2708 frx_c14n_subtree(frx_c14n_ctx *x, const frx_node *top)
563 {
564 2708 const frx_node *n = top;
565 2708 ▶ if (frx_c14n_omitted(x->c, top)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_c14n_omitted(x->c, top)) return; 2707 1 2708
566 409905 for (;;) {
567 412612 int descend = 0;
568 412612 ▶ if (x->failed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (x->failed) return; 0 412612 412612
569 412612 ▶ if (!frx_c14n_omitted(x->c, n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_c14n_omitted(x->c, n)) { 412600 12 412612
570 412600 ▶ if (n->kind == FRX_ELEMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->kind == FRX_ELEMENT) { 207686 204914 412600
571 207686 frx_c14n_start_tag(x, n);
572 207686 ▶ if (n->first_child) descend = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n->first_child) descend = 1; 204087 3599 207686
573 3599 else frx_c14n_end_tag(x, n);
574 } else {
575 204914 frx_c14n_misc(x, n);
576 }
577 }
578 412612 ▶ if (descend) { n = n->first_child; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (descend) { n = n->first_child; continue; } 204087 208525 412612
579 /* ascend, closing on the way, until a sibling or the top */
580 412612 ▶ while (n != top && !n->next) {
Branch: 2 branches
BranchTrueFalseTotal
while (n != top && !n->next) { 409905 2707 412612
while (n != top && !n->next) { 204087 205818 409905
581 204087 n = n->parent;
582 204087 frx_c14n_end_tag(x, n);
583 }
584 208525 ▶ if (n == top) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n == top) return; 2707 205818 208525
585 205818 n = n->next;
586 }
587 }
588  
589 /* the whole thing: 1 and bytes in out, or 0 on allocation failure */
590 static int
591 2711 frx_c14n_render(const frx_node *apex, const frx_c14n *c, frx_buf *out)
592 {
593 frx_c14n_ctx x;
594 2711 memset(&x, 0, sizeof x);
595 2711 x.c = c;
596 2711 x.out = out;
597 2711 x.apex = apex;
598 2711 frx_buf_init(&x.base);
599  
600 2711 ▶ if (apex->kind == FRX_DOCUMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (apex->kind == FRX_DOCUMENT) { 2605 106 2711
601 /* misc before the root: each followed by "\n"; after: each preceded */
602 const frx_node *n;
603 2605 int after_root = 0;
604 6335 ▶ for (n = apex->first_child; n && !x.failed; n = n->next) {
Branch: 2 branches
BranchTrueFalseTotal
for (n = apex->first_child; n && !x.failed; n = n->next) { 3730 2605 6335
for (n = apex->first_child; n && !x.failed; n = n->next) { 3730 0 3730
605 3730 ▶ if (frx_c14n_omitted(c, n)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (frx_c14n_omitted(c, n)) continue; 1 3729 3730
606 3729 ▶ if (n->kind == FRX_ELEMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->kind == FRX_ELEMENT) { 2603 1126 3729
607 2603 x.apex = n; /* the root has no omitted ancestors */
608 2603 frx_c14n_subtree(&x, n);
609 2603 x.apex = apex;
610 2603 after_root = 1;
611 1126 ▶ } else if (n->kind == FRX_COMMENT && !c->comments) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (n->kind == FRX_COMMENT && !c->comments) { 967 159 1126
} else if (n->kind == FRX_COMMENT && !c->comments) { 952 15 967
612 952 continue;
613 } else {
614 174 ▶ if (after_root) frx_buf_append_ch(out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (after_root) frx_buf_append_ch(out, '\n'); 60 114 174
615 174 frx_c14n_misc(&x, n);
616 174 ▶ if (!after_root) frx_buf_append_ch(out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (!after_root) frx_buf_append_ch(out, '\n'); 114 60 174
617 }
618 }
619 106 ▶ } else if (apex->kind == FRX_ELEMENT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (apex->kind == FRX_ELEMENT) { 105 1 106
620 105 frx_c14n_subtree(&x, apex);
621 } else {
622 1 ▶ if (!frx_c14n_omitted(c, apex)) frx_c14n_misc(&x, apex);
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_c14n_omitted(c, apex)) frx_c14n_misc(&x, apex); 1 0 1
623 }
624  
625 2711 free(x.ns);
626 2711 free(x.at);
627 2711 free(x.rendered);
628 2711 free(x.marks);
629 2711 frx_buf_free(&x.base);
630 2711 ▶ return !x.failed && !out->failed;
Branch: 2 branches
BranchTrueFalseTotal
return !x.failed && !out->failed; 2711 0 2711
return !x.failed && !out->failed; 2711 0 2711
631 }
632  
633 #endif /* FRX_C14N_H */
« include/frx/frx_xmlattr.h ↑ Summary xs/node.xs »