| 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 |
4198246 |
| 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 |
4198246 |
▶ | if (plen == 3 && memcmp(prefix, "xml", 3) == 0) return &xml_ns; |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen == 3 && memcmp(prefix, "xml", 3) == 0) return &xml_ns; |
7589 |
4190657 |
4198246 |
| if (plen == 3 && memcmp(prefix, "xml", 3) == 0) return &xml_ns; |
5147 |
2442 |
7589 |
|
| 51 |
4200896 |
▶ | for (; s; s = s->up) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; s; s = s->up) { |
3209700 |
991196 |
4200896 |
|
| 52 |
4106588 |
▶ | for (i = 0; i < s->n_decls; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < s->n_decls; i++) |
4098791 |
7797 |
4106588 |
|
| 53 |
4098791 |
▶ | if (frx_str_eq(&s->decls[i].prefix, prefix, plen)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(&s->decls[i].prefix, prefix, plen)) { |
3201903 |
896888 |
4098791 |
|
| 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 |
3201903 |
▶ | if (plen && !s->decls[i].uri.len) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen && !s->decls[i].uri.len) return NULL; |
907936 |
2293967 |
3201903 |
| if (plen && !s->decls[i].uri.len) return NULL; |
1 |
907935 |
907936 |
|
| 62 |
3201902 |
| return &s->decls[i].uri; |
| 63 |
|
| } |
| 64 |
|
| } |
| 65 |
991196 |
▶ | return plen == 0 ? &FRX_EMPTY_STR : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return plen == 0 ? &FRX_EMPTY_STR : NULL; |
990862 |
334 |
991196 |
|
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
|
| /* RFC 3986 scheme: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) ":" */ |
| 69 |
|
| static int |
| 70 |
54231 |
| frx_ns_is_absolute(const char *p, size_t n) |
| 71 |
|
| { |
| 72 |
|
| size_t i; |
| 73 |
54231 |
▶ | if (!n) return 1; /* the empty URI is xmlns="" */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return 1; /* the empty URI is xmlns="" */ |
3186 |
51045 |
54231 |
|
| 74 |
51045 |
▶ | if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) return 0; |
51043 |
2 |
51045 |
| if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) return 0; |
51043 |
0 |
51043 |
| if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) return 0; |
51043 |
2 |
51045 |
| if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) return 0; |
0 |
51043 |
51043 |
|
| 75 |
178090 |
▶ | for (i = 1; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < n; i++) { |
178078 |
12 |
178090 |
|
| 76 |
178078 |
| char c = p[i]; |
| 77 |
178078 |
▶ | if (c == ':') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == ':') return 1; |
51009 |
127069 |
178078 |
|
| 78 |
127092 |
▶ | if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
Branch: 5 branches
| Branch | True | False | Total |
| if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
127041 |
28 |
127069 |
| if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
127041 |
0 |
127041 |
| if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
127041 |
28 |
127069 |
| if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
0 |
127041 |
127041 |
| if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
22 |
1 |
23 |
|
| 79 |
28 |
▶ | || (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) |
Branch: 4 branches
| Branch | True | False | Total |
| || (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 |
58126 |
| frx_intern_init(frx_intern *t) |
| 94 |
|
| { |
| 95 |
58126 |
| t->uris = NULL; |
| 96 |
58126 |
| t->n = t->cap = 0; |
| 97 |
58126 |
| } |
| 98 |
|
| |
| 99 |
|
| static void |
| 100 |
29063 |
| frx_intern_free(frx_intern *t) |
| 101 |
|
| { |
| 102 |
29063 |
| free(t->uris); |
| 103 |
29063 |
| frx_intern_init(t); |
| 104 |
29063 |
| } |
| 105 |
|
| |
| 106 |
|
| /* the interned copy of (p, n); p NULL on allocation failure */ |
| 107 |
|
| static frx_str |
| 108 |
54020 |
| frx_intern_uri(frx_intern *t, frx_arena *a, const char *p, size_t n) |
| 109 |
|
| { |
| 110 |
|
| int i; |
| 111 |
|
| frx_str s; |
| 112 |
54020 |
▶ | if (!n) return FRX_EMPTY_STR; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return FRX_EMPTY_STR; |
3186 |
50834 |
54020 |
|
| 113 |
126720 |
▶ | for (i = 0; i < t->n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->n; i++) |
81488 |
45232 |
126720 |
|
| 114 |
81488 |
▶ | if (frx_str_eq(&t->uris[i], p, n)) return t->uris[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(&t->uris[i], p, n)) return t->uris[i]; |
5602 |
75886 |
81488 |
|
| 115 |
45232 |
| s = frx_arena_strndup(a, p, n); |
| 116 |
45232 |
▶ | if (!s.p) return s; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s.p) return s; |
0 |
45232 |
45232 |
|
| 117 |
45232 |
▶ | if (t->n == t->cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->n == t->cap) { |
12565 |
32667 |
45232 |
|
| 118 |
12565 |
▶ | int ncap = t->cap ? t->cap * 2 : 16; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = t->cap ? t->cap * 2 : 16; |
0 |
12565 |
12565 |
|
| 119 |
12565 |
| frx_str *nu = (frx_str *)realloc(t->uris, (size_t)ncap * sizeof *nu); |
| 120 |
12565 |
▶ | if (!nu) { s.p = NULL; s.len = 0; return s; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nu) { s.p = NULL; s.len = 0; return s; } |
0 |
12565 |
12565 |
|
| 121 |
12565 |
| t->uris = nu; |
| 122 |
12565 |
| t->cap = ncap; |
| 123 |
|
| } |
| 124 |
45232 |
| t->uris[t->n++] = s; |
| 125 |
45232 |
| 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 |
10383232 |
| frx_ns_split(const frx_str *qname, frx_str *prefix, frx_str *local) |
| 132 |
|
| { |
| 133 |
10383232 |
| const char *colon = (const char *)memchr(qname->p, ':', qname->len); |
| 134 |
10383232 |
▶ | if (!colon) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!colon) { |
8569755 |
1813477 |
10383232 |
|
| 135 |
8569755 |
| *prefix = FRX_EMPTY_STR; |
| 136 |
8569755 |
| *local = *qname; |
| 137 |
|
| } else { |
| 138 |
1813477 |
| prefix->p = qname->p; |
| 139 |
1813477 |
| prefix->len = (size_t)(colon - qname->p); |
| 140 |
1813477 |
| local->p = colon + 1; |
| 141 |
1813477 |
| local->len = qname->len - prefix->len - 1; |
| 142 |
|
| } |
| 143 |
10383232 |
| } |
| 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 |
6184359 |
| frx_ns_is_decl(const frx_str *qname, frx_str *prefix) |
| 149 |
|
| { |
| 150 |
6184359 |
▶ | if (frx_str_eq(qname, "xmlns", 5)) { *prefix = FRX_EMPTY_STR; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(qname, "xmlns", 5)) { *prefix = FRX_EMPTY_STR; return 1; } |
23866 |
6160493 |
6184359 |
|
| 151 |
6160493 |
▶ | if (qname->len > 6 && memcmp(qname->p, "xmlns:", 6) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (qname->len > 6 && memcmp(qname->p, "xmlns:", 6) == 0) { |
549972 |
5610521 |
6160493 |
| if (qname->len > 6 && memcmp(qname->p, "xmlns:", 6) == 0) { |
179464 |
370508 |
549972 |
|
| 152 |
179464 |
| prefix->p = qname->p + 6; |
| 153 |
179464 |
| prefix->len = qname->len - 6; |
| 154 |
179464 |
| return 1; |
| 155 |
|
| } |
| 156 |
5981029 |
| return 0; |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
|
| #endif /* FRX_NS_H */ |