include/frx/frx_ns.h

filter: Statementstmt 100% 57 / 57 Branchbran 88.5% 62 / 70 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.7% 119 / 127 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/node.xs ↑ Summary include/frx/frx_parse.h »
1 #ifndef FRX_NS_H
2 #define FRX_NS_H
3  
4 /* Namespaces 1.0: the scope chain, lookup, interning, and the rules.
5 *
6 * A scope chain rather than per-element copies: an element carrying
7 * declarations creates a scope record pointing at the enclosing one, and a
8 * lookup walks the chain. Lookups are O(depth), memory is one record per
9 * declaring element, and pointers stay stable. Canonicalisation
10 * collects the in-scope set by walking the chain with a per-prefix seen
11 * filter.
12 *
13 * Namespace URIs are interned per document, so each is held once and two
14 * elements in one namespace share a pointer; comparisons in find stay
15 * memcmp with lengths so a consumer's own string matches too.
16 *
17 * The rules, each a refusal with FRX_E_NAMESPACE and an offset:
18 *
19 * - The xml prefix is bound implicitly and never stored as a
20 * declaration. An explicit xmlns:xml with the correct URI is accepted
21 * and dropped; with any other URI it is refused. No other prefix may
22 * be bound to the xml namespace name (section 6.2 of the 1.0 third
23 * edition).
24 * - xmlns:xmlns is refused, and so is binding any prefix to the xmlns
25 * namespace name.
26 * - xmlns:p="" - undeclaring a prefix - is refused; that is Namespaces
27 * 1.1. xmlns="" is accepted and undeclares the default.
28 * - A relative namespace URI is refused. Both Canonical XML 1.0 and 1.1
29 * require an implementation to fail on a document containing one;
30 * failing here keeps the ABI's c14n entry infallible, and Namespaces
31 * 1.0 deprecates relative URIs anyway. The user decided this on
32 * 2026-09-06. "Relative" means "has no scheme": RFC 3986's scheme
33 * production is ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) ":".
34 * - An element or attribute prefix with no binding in scope is refused.
35 *
36 * Unprefixed attributes have no namespace, never the default (6.2).
37 *
38 * Needs frx_tree.h. */
39  
40 #define FRX_XML_NS "http://www.w3.org/XML/1998/namespace"
41 #define FRX_XMLNS_NS "http://www.w3.org/2000/xmlns/"
42  
43 /* the URI bound to prefix, or NULL when unbound; the default with no
44 * declaration is "" (no namespace) */
45 static const frx_str *
46 4198348 frx_ns_lookup(const frx_scope *s, const char *prefix, size_t plen)
47 {
48 static const frx_str xml_ns = { FRX_XML_NS, sizeof FRX_XML_NS - 1 };
49 int i;
50 4198348 ▶ if (plen == 3 && memcmp(prefix, "xml", 3) == 0) return &xml_ns;
Branch: 2 branches
BranchTrueFalseTotal
if (plen == 3 && memcmp(prefix, "xml", 3) == 0) return &xml_ns; 7589 4190759 4198348
if (plen == 3 && memcmp(prefix, "xml", 3) == 0) return &xml_ns; 5147 2442 7589
51 4200998 ▶ for (; s; s = s->up) {
Branch: 1 branch
BranchTrueFalseTotal
for (; s; s = s->up) { 3209800 991198 4200998
52 4106708 ▶ for (i = 0; i < s->n_decls; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n_decls; i++) 4098911 7797 4106708
53 4098911 ▶ if (frx_str_eq(&s->decls[i].prefix, prefix, plen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(&s->decls[i].prefix, prefix, plen)) { 3202003 896908 4098911
54 /* Namespaces 1.1 section 5: xmlns:p="" undeclares p. The
55 * chain stops here rather than falling through to an outer
56 * binding - that is the whole point of an undeclaration -
57 * and the prefix reads as unbound, so using it is an
58 * error. For the DEFAULT prefix an empty value is "no
59 * namespace", which is a binding and not an
60 * undeclaration, so plen guards it. */
61 3202003 ▶ if (plen && !s->decls[i].uri.len) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (plen && !s->decls[i].uri.len) return NULL; 907956 2294047 3202003
if (plen && !s->decls[i].uri.len) return NULL; 1 907955 907956
62 3202002 return &s->decls[i].uri;
63 }
64 }
65 991198 ▶ return plen == 0 ? &FRX_EMPTY_STR : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return plen == 0 ? &FRX_EMPTY_STR : NULL; 990864 334 991198
66 }
67  
68 /* RFC 3986 scheme: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) ":" */
69 static int
70 54271 frx_ns_is_absolute(const char *p, size_t n)
71 {
72 size_t i;
73 54271 ▶ if (!n) return 1; /* the empty URI is xmlns="" */
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return 1; /* the empty URI is xmlns="" */ 3186 51085 54271
74 51085 ▶ if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) return 0; 51083 2 51085
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) return 0; 51083 0 51083
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) return 0; 51083 2 51085
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) return 0; 0 51083 51083
75 178210 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 178198 12 178210
76 178198 char c = p[i];
77 178198 ▶ if (c == ':') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c == ':') return 1; 51049 127149 178198
78 127172 ▶ if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 5 branches
BranchTrueFalseTotal
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 127121 28 127149
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 127121 0 127121
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 127121 28 127149
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 127121 127121
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 22 1 23
79 28 ▶ || (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.'))
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 3 25 28
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 0 3 3
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 24 1 25
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 23 1 24
80 22 return 0;
81 }
82 12 return 0;
83 }
84  
85 /* the interning table lives in the parser; the strings live in the arena */
86 typedef struct frx_intern {
87 frx_str *uris;
88 int n;
89 int cap;
90 } frx_intern;
91  
92 static void
93 58168 frx_intern_init(frx_intern *t)
94 {
95 58168 t->uris = NULL;
96 58168 t->n = t->cap = 0;
97 58168 }
98  
99 static void
100 29084 frx_intern_free(frx_intern *t)
101 {
102 29084 free(t->uris);
103 29084 frx_intern_init(t);
104 29084 }
105  
106 /* the interned copy of (p, n); p NULL on allocation failure */
107 static frx_str
108 54060 frx_intern_uri(frx_intern *t, frx_arena *a, const char *p, size_t n)
109 {
110 int i;
111 frx_str s;
112 54060 ▶ if (!n) return FRX_EMPTY_STR;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return FRX_EMPTY_STR; 3186 50874 54060
113 126780 ▶ for (i = 0; i < t->n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n; i++) 81508 45272 126780
114 81508 ▶ if (frx_str_eq(&t->uris[i], p, n)) return t->uris[i];
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(&t->uris[i], p, n)) return t->uris[i]; 5602 75906 81508
115 45272 s = frx_arena_strndup(a, p, n);
116 45272 ▶ if (!s.p) return s;
Branch: 1 branch
BranchTrueFalseTotal
if (!s.p) return s; 0 45272 45272
117 45272 ▶ if (t->n == t->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->n == t->cap) { 12585 32687 45272
118 12585 ▶ int ncap = t->cap ? t->cap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = t->cap ? t->cap * 2 : 16; 0 12585 12585
119 12585 frx_str *nu = (frx_str *)realloc(t->uris, (size_t)ncap * sizeof *nu);
120 12585 ▶ if (!nu) { s.p = NULL; s.len = 0; return s; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nu) { s.p = NULL; s.len = 0; return s; } 0 12585 12585
121 12585 t->uris = nu;
122 12585 t->cap = ncap;
123 }
124 45272 t->uris[t->n++] = s;
125 45272 return s;
126 }
127  
128 /* split a qname at its colon; the lexer guaranteed at most one, with a
129 * part on each side */
130 static void
131 10383457 frx_ns_split(const frx_str *qname, frx_str *prefix, frx_str *local)
132 {
133 10383457 const char *colon = (const char *)memchr(qname->p, ':', qname->len);
134 10383457 ▶ if (!colon) {
Branch: 1 branch
BranchTrueFalseTotal
if (!colon) { 8569940 1813517 10383457
135 8569940 *prefix = FRX_EMPTY_STR;
136 8569940 *local = *qname;
137 } else {
138 1813517 prefix->p = qname->p;
139 1813517 prefix->len = (size_t)(colon - qname->p);
140 1813517 local->p = colon + 1;
141 1813517 local->len = qname->len - prefix->len - 1;
142 }
143 10383457 }
144  
145 /* Is this attribute a namespace declaration, and for which prefix?
146 * Returns 1 with *prefix set (empty for xmlns=""), 0 otherwise. */
147 static int
148 6184602 frx_ns_is_decl(const frx_str *qname, frx_str *prefix)
149 {
150 6184602 ▶ if (frx_str_eq(qname, "xmlns", 5)) { *prefix = FRX_EMPTY_STR; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(qname, "xmlns", 5)) { *prefix = FRX_EMPTY_STR; return 1; } 23926 6160676 6184602
151 6160676 ▶ if (qname->len > 6 && memcmp(qname->p, "xmlns:", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (qname->len > 6 && memcmp(qname->p, "xmlns:", 6) == 0) { 550032 5610644 6160676
if (qname->len > 6 && memcmp(qname->p, "xmlns:", 6) == 0) { 179524 370508 550032
152 179524 prefix->p = qname->p + 6;
153 179524 prefix->len = qname->len - 6;
154 179524 return 1;
155 }
156 5981152 return 0;
157 }
158  
159 #endif /* FRX_NS_H */
« xs/node.xs ↑ Summary include/frx/frx_parse.h »