| 1 |
|
| #ifndef FRX_XINCLUDE_H |
| 2 |
|
| #define FRX_XINCLUDE_H |
| 3 |
|
| |
| 4 |
|
| /* XInclude 1.0, under `xinclude => 1` and the full profile: the pass |
| 5 |
|
| * that replaces every xi:include element with what it names. |
| 6 |
|
| * |
| 7 |
|
| * A pass over the tree after the parse, like validation, and the first |
| 8 |
|
| * consumer of the tree's import and the second of the caller's resolver. |
| 9 |
|
| * Every byte still arrives through the caller's resolver, called with |
| 10 |
|
| * kind xinclude, so a document cannot reach the filesystem or the |
| 11 |
|
| * network by naming an href any more than by naming an external entity. |
| 12 |
|
| * |
| 13 |
|
| * What is supported: href, absent for a same-document include; parse, |
| 14 |
|
| * "xml" (the default) or "text"; encoding, for text; xpointer, limited |
| 15 |
|
| * to the element() scheme and the bare shorthand name, both of which |
| 16 |
|
| * resolve through the ID index and a child sequence walk (decision 4: |
| 17 |
|
| * full XPointer needs XPath over the included document, which frx_xpath.h |
| 18 |
|
| * provides, and is one call away when a consumer asks); and xi:fallback. |
| 19 |
|
| * accept and accept-language are read and ignored: they are for a |
| 20 |
|
| * transport this dist does not have. |
| 21 |
|
| * |
| 22 |
|
| * The errors XInclude 4.1 separates are separated here. A resource error |
| 23 |
|
| * (the resolver refuses, or the fetched bytes do not parse) takes the |
| 24 |
|
| * xi:fallback children in place of the include, or is fatal when there |
| 25 |
|
| * is no fallback. An inclusion loop and a chain deeper than |
| 26 |
|
| * max_xinclude_depth (default 8) are fatal whatever fallback says: a |
| 27 |
|
| * document that includes itself has no fallback answer. |
| 28 |
|
| * |
| 29 |
|
| * Nothing recurses (rule 2). The pass is a work list of (include |
| 30 |
|
| * element, chain, depth): processing one imports its content, then scans |
| 31 |
|
| * exactly what was imported for further xi:include elements and pushes |
| 32 |
|
| * them with the chain extended by the URI just fetched. The chain is a |
| 33 |
|
| * linked list in the arena, one node per fetched URI, so two branches |
| 34 |
|
| * share their common ancestry and a loop is a walk up it. |
| 35 |
|
| * |
| 36 |
|
| * The included subtree is imported into the including document, so the |
| 37 |
|
| * result is one arena and one document, and section 4.7.5's xml:base |
| 38 |
|
| * fixup puts the resolved URI on the imported root when it differs from |
| 39 |
|
| * the base the include element stood at. That attribute is part of the |
| 40 |
|
| * data model and appears in the canonical form; the POD says so. |
| 41 |
|
| * |
| 42 |
|
| * Needs frx_tree.h, frx_parse.h, frx_edit.h, frx_xmlattr.h, frx_enc.h. */ |
| 43 |
|
| |
| 44 |
|
| #define FRX_XI_NS "http://www.w3.org/2001/XInclude" |
| 45 |
|
| #define FRX_DEFAULT_MAX_XINCLUDE_DEPTH 8 |
| 46 |
|
| |
| 47 |
|
| /* one fetched URI on the inclusion chain, and what it was included from */ |
| 48 |
|
| typedef struct frx_xi_chain { |
| 49 |
|
| frx_str uri; |
| 50 |
|
| const struct frx_xi_chain *up; |
| 51 |
|
| } frx_xi_chain; |
| 52 |
|
| |
| 53 |
|
| typedef struct frx_xi_work { |
| 54 |
|
| frx_node *inc; /* the xi:include element */ |
| 55 |
|
| const frx_xi_chain *chain; |
| 56 |
|
| int depth; |
| 57 |
|
| } frx_xi_work; |
| 58 |
|
| |
| 59 |
|
| typedef struct frx_xi { |
| 60 |
|
| frx_doc *doc; |
| 61 |
|
| const frx_opts_ex *oe; |
| 62 |
|
| frx_err *err; |
| 63 |
|
| frx_xi_work *work; |
| 64 |
|
| int n_work, cap_work; |
| 65 |
|
| int fetches; |
| 66 |
|
| int max_depth; |
| 67 |
|
| } frx_xi; |
| 68 |
|
| |
| 69 |
|
| /* the refusal as an expression, for `return FRX_XI_FAIL(...)`, and as a |
| 70 |
|
| * statement, for the functions that answer with a NULL node: a discarded |
| 71 |
|
| * comma value is an error under the MinGW proof's -Werror */ |
| 72 |
|
| #define FRX_XI_FAIL(x, off, what) (frx_err_set((x)->err, FRX_E_SYNTAX, (off), (what)), 0) |
| 73 |
|
| #define FRX_XI_SET(x, off, what) frx_err_set((x)->err, FRX_E_SYNTAX, (off), (what)) |
| 74 |
|
| |
| 75 |
|
| static int |
| 76 |
1021 |
| frx_xi_is(const frx_node *n, const char *local) |
| 77 |
|
| { |
| 78 |
1021 |
| return n->kind == FRX_ELEMENT |
| 79 |
533 |
▶ | && frx_str_eq(&n->ns, FRX_XI_NS, sizeof FRX_XI_NS - 1) |
Branch: 1 branch
| Branch | True | False | Total |
| && frx_str_eq(&n->ns, FRX_XI_NS, sizeof FRX_XI_NS - 1) |
178 |
355 |
533 |
|
| 80 |
1554 |
▶ | && frx_str_eq(&n->local, local, strlen(local)); |
Branch: 2 branches
| Branch | True | False | Total |
| && frx_str_eq(&n->local, local, strlen(local)); |
533 |
488 |
1021 |
| && frx_str_eq(&n->local, local, strlen(local)); |
171 |
7 |
178 |
|
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| /* an attribute with no namespace, which is what XInclude's are */ |
| 84 |
|
| static const frx_str * |
| 85 |
672 |
| frx_xi_attr(const frx_node *n, const char *local) |
| 86 |
|
| { |
| 87 |
|
| int i; |
| 88 |
1198 |
▶ | for (i = 0; i < n->n_attrs; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n->n_attrs; i++) |
700 |
498 |
1198 |
|
| 89 |
700 |
▶ | if (!n->attrs[i].ns.len && frx_str_eq(&n->attrs[i].local, local, strlen(local))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!n->attrs[i].ns.len && frx_str_eq(&n->attrs[i].local, local, strlen(local))) |
700 |
0 |
700 |
| if (!n->attrs[i].ns.len && frx_str_eq(&n->attrs[i].local, local, strlen(local))) |
174 |
526 |
700 |
|
| 90 |
174 |
| return &n->attrs[i].value; |
| 91 |
498 |
| return NULL; |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| static int |
| 95 |
168 |
| frx_xi_push(frx_xi *x, frx_node *inc, const frx_xi_chain *chain, int depth) |
| 96 |
|
| { |
| 97 |
168 |
▶ | if (x->n_work == x->cap_work) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x->n_work == x->cap_work) { |
137 |
31 |
168 |
|
| 98 |
137 |
▶ | int ncap = x->cap_work ? x->cap_work * 2 : 16; |
Branch: 1 branch
| Branch | True | False | Total |
| int ncap = x->cap_work ? x->cap_work * 2 : 16; |
0 |
137 |
137 |
|
| 99 |
137 |
| frx_xi_work *nw = (frx_xi_work *)realloc(x->work, (size_t)ncap * sizeof *nw); |
| 100 |
137 |
▶ | if (!nw) return FRX_XI_FAIL(x, inc->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nw) return FRX_XI_FAIL(x, inc->offset, "out of memory"); |
0 |
137 |
137 |
|
| 101 |
137 |
| x->work = nw; x->cap_work = ncap; |
| 102 |
|
| } |
| 103 |
168 |
| x->work[x->n_work].inc = inc; |
| 104 |
168 |
| x->work[x->n_work].chain = chain; |
| 105 |
168 |
| x->work[x->n_work].depth = depth; |
| 106 |
168 |
| x->n_work++; |
| 107 |
168 |
| return 1; |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
|
| /* every xi:include in the subtree at root, outermost first; one inside |
| 111 |
|
| * another include's fallback is left alone, because fallback content is |
| 112 |
|
| * only processed when it is used */ |
| 113 |
|
| static int |
| 114 |
317 |
| frx_xi_scan(frx_xi *x, frx_node *root, const frx_xi_chain *chain, int depth) |
| 115 |
|
| { |
| 116 |
317 |
| frx_node *n = root; |
| 117 |
1278 |
▶ | while (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (n) { |
1018 |
260 |
1278 |
|
| 118 |
1018 |
▶ | if (frx_xi_is(n, "include")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xi_is(n, "include")) { |
168 |
850 |
1018 |
|
| 119 |
168 |
▶ | if (!frx_xi_push(x, n, chain, depth)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xi_push(x, n, chain, depth)) return 0; |
0 |
168 |
168 |
|
| 120 |
|
| /* its children are fallback: not scanned */ |
| 121 |
361 |
▶ | while (n != root && !n->next) n = n->parent; |
Branch: 2 branches
| Branch | True | False | Total |
| while (n != root && !n->next) n = n->parent; |
304 |
57 |
361 |
| while (n != root && !n->next) n = n->parent; |
193 |
111 |
304 |
|
| 122 |
168 |
▶ | if (n == root) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == root) return 1; |
57 |
111 |
168 |
|
| 123 |
111 |
| n = n->next; |
| 124 |
111 |
| continue; |
| 125 |
|
| } |
| 126 |
850 |
| n = (frx_node *)frx_walk_next(n, root); |
| 127 |
|
| } |
| 128 |
260 |
| return 1; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| /* the element() scheme and the bare name, XPointer Framework section 3 |
| 132 |
|
| * and the element() scheme's own: "name", "name/1/2" or "/1/2" */ |
| 133 |
|
| static frx_node * |
| 134 |
8 |
| frx_xi_xpointer(frx_xi *x, frx_doc *sub, const frx_str *ptr, size_t off) |
| 135 |
|
| { |
| 136 |
8 |
| const char *p = ptr->p; |
| 137 |
8 |
| size_t n = ptr->len, i = 0; |
| 138 |
|
| frx_node *cur; |
| 139 |
|
| |
| 140 |
8 |
▶ | if (n > 8 && memcmp(p, "element(", 8) == 0 && p[n - 1] == ')') { p += 8; n -= 9; i = 0; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (n > 8 && memcmp(p, "element(", 8) == 0 && p[n - 1] == ')') { p += 8; n -= 9; i = 0; } |
5 |
3 |
8 |
| if (n > 8 && memcmp(p, "element(", 8) == 0 && p[n - 1] == ')') { p += 8; n -= 9; i = 0; } |
4 |
1 |
5 |
| if (n > 8 && memcmp(p, "element(", 8) == 0 && p[n - 1] == ')') { p += 8; n -= 9; i = 0; } |
4 |
0 |
4 |
|
| 141 |
4 |
▶ | else if (memchr(p, '(', n)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (memchr(p, '(', n)) { |
1 |
3 |
4 |
|
| 142 |
1 |
| FRX_XI_SET(x, off, "only the element() scheme and a bare name are supported in an xpointer"); |
| 143 |
1 |
| return NULL; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
7 |
▶ | if (n && p[0] != '/') { /* a name, then a child sequence */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (n && p[0] != '/') { /* a name, then a child sequence */ |
7 |
0 |
7 |
| if (n && p[0] != '/') { /* a name, then a child sequence */ |
5 |
2 |
7 |
|
| 147 |
5 |
| size_t k = 0; |
| 148 |
|
| const frx_node *hit; |
| 149 |
21 |
▶ | while (k < n && p[k] != '/') k++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (k < n && p[k] != '/') k++; |
17 |
4 |
21 |
| while (k < n && p[k] != '/') k++; |
16 |
1 |
17 |
|
| 150 |
5 |
| hit = frx_by_id(sub, "id", p, k); |
| 151 |
5 |
▶ | if (!hit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hit) { |
0 |
5 |
5 |
|
| 152 |
|
| /* the ID index of the included document names it by local |
| 153 |
|
| * name, so an id_attrs name and xml:id both answer here */ |
| 154 |
|
| int j; |
| 155 |
0 |
▶ | for (j = 0; j < sub->n_ids; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < sub->n_ids; j++) |
0 |
0 |
0 |
|
| 156 |
0 |
▶ | if (frx_str_eq(&sub->ids[j].value, p, k)) { hit = sub->ids[j].node; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq(&sub->ids[j].value, p, k)) { hit = sub->ids[j].node; break; } |
0 |
0 |
0 |
|
| 157 |
|
| } |
| 158 |
5 |
▶ | if (!hit) { FRX_XI_SET(x, off, "the xpointer names no element in the included document"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hit) { FRX_XI_SET(x, off, "the xpointer names no element in the included document"); return NULL; } |
0 |
5 |
5 |
|
| 159 |
5 |
| cur = (frx_node *)hit; |
| 160 |
5 |
| i = k; |
| 161 |
|
| } else { |
| 162 |
2 |
| cur = sub->root; |
| 163 |
2 |
▶ | if (n >= 2) { /* /1 is the root itself */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (n >= 2) { /* /1 is the root itself */ |
2 |
0 |
2 |
|
| 164 |
2 |
| size_t k = 1, v = 0; |
| 165 |
4 |
▶ | while (k < n && p[k] >= '0' && p[k] <= '9') { v = v * 10 + (size_t)(p[k] - '0'); k++; } |
Branch: 3 branches
| Branch | True | False | Total |
| while (k < n && p[k] >= '0' && p[k] <= '9') { v = v * 10 + (size_t)(p[k] - '0'); k++; } |
4 |
0 |
4 |
| while (k < n && p[k] >= '0' && p[k] <= '9') { v = v * 10 + (size_t)(p[k] - '0'); k++; } |
2 |
2 |
4 |
| while (k < n && p[k] >= '0' && p[k] <= '9') { v = v * 10 + (size_t)(p[k] - '0'); k++; } |
2 |
0 |
2 |
|
| 166 |
2 |
▶ | if (v != 1) { FRX_XI_SET(x, off, "an element() child sequence starts at the document element, /1"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (v != 1) { FRX_XI_SET(x, off, "an element() child sequence starts at the document element, /1"); return NULL; } |
0 |
2 |
2 |
|
| 167 |
2 |
| i = k; |
| 168 |
|
| } else { |
| 169 |
0 |
| i = n; |
| 170 |
|
| } |
| 171 |
|
| } |
| 172 |
|
| |
| 173 |
11 |
▶ | while (i < n) { /* /k steps down the children */ |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < n) { /* /k steps down the children */ |
5 |
6 |
11 |
|
| 174 |
5 |
| size_t v = 0, seen = 0; |
| 175 |
|
| frx_node *c; |
| 176 |
5 |
▶ | if (p[i] != '/') { FRX_XI_SET(x, off, "a malformed element() child sequence"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (p[i] != '/') { FRX_XI_SET(x, off, "a malformed element() child sequence"); return NULL; } |
0 |
5 |
5 |
|
| 177 |
5 |
| i++; |
| 178 |
10 |
▶ | while (i < n && p[i] >= '0' && p[i] <= '9') { v = v * 10 + (size_t)(p[i] - '0'); i++; } |
Branch: 3 branches
| Branch | True | False | Total |
| while (i < n && p[i] >= '0' && p[i] <= '9') { v = v * 10 + (size_t)(p[i] - '0'); i++; } |
7 |
3 |
10 |
| while (i < n && p[i] >= '0' && p[i] <= '9') { v = v * 10 + (size_t)(p[i] - '0'); i++; } |
5 |
2 |
7 |
| while (i < n && p[i] >= '0' && p[i] <= '9') { v = v * 10 + (size_t)(p[i] - '0'); i++; } |
5 |
0 |
5 |
|
| 179 |
5 |
▶ | if (!v) { FRX_XI_SET(x, off, "a malformed element() child sequence"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!v) { FRX_XI_SET(x, off, "a malformed element() child sequence"); return NULL; } |
0 |
5 |
5 |
|
| 180 |
8 |
▶ | for (c = cur->first_child; c; c = c->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = cur->first_child; c; c = c->next) { |
7 |
1 |
8 |
|
| 181 |
7 |
▶ | if (c->kind != FRX_ELEMENT) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->kind != FRX_ELEMENT) continue; |
0 |
7 |
7 |
|
| 182 |
7 |
▶ | if (++seen == v) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (++seen == v) break; |
4 |
3 |
7 |
|
| 183 |
|
| } |
| 184 |
5 |
▶ | if (!c) { FRX_XI_SET(x, off, "the xpointer names no element in the included document"); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) { FRX_XI_SET(x, off, "the xpointer names no element in the included document"); return NULL; } |
1 |
4 |
5 |
|
| 185 |
4 |
| cur = c; |
| 186 |
|
| } |
| 187 |
6 |
| return cur; |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| /* the fallback children in place of the include; the include is removed. |
| 191 |
|
| * 0 when there is no fallback, which makes the resource error fatal. */ |
| 192 |
|
| static int |
| 193 |
15 |
| frx_xi_fallback(frx_xi *x, frx_node *inc) |
| 194 |
|
| { |
| 195 |
15 |
| frx_node *fb = NULL, *c; |
| 196 |
15 |
▶ | for (c = inc->first_child; c; c = c->next) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = inc->first_child; c; c = c->next) |
3 |
12 |
15 |
|
| 197 |
3 |
▶ | if (frx_xi_is(c, "fallback")) { fb = c; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_xi_is(c, "fallback")) { fb = c; break; } |
3 |
0 |
3 |
|
| 198 |
15 |
▶ | if (!fb) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fb) return 0; |
12 |
3 |
15 |
|
| 199 |
6 |
▶ | while (fb->first_child) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (fb->first_child) { |
3 |
3 |
6 |
|
| 200 |
3 |
| frx_node *kid = fb->first_child; |
| 201 |
|
| frx_err e; |
| 202 |
3 |
▶ | if (!frx_remove(x->doc, kid, &e)) { *x->err = e; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_remove(x->doc, kid, &e)) { *x->err = e; return -1; } |
0 |
3 |
3 |
|
| 203 |
3 |
▶ | if (!frx_insert_before(x->doc, inc, kid, &e)) { *x->err = e; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_insert_before(x->doc, inc, kid, &e)) { *x->err = e; return -1; } |
0 |
3 |
3 |
|
| 204 |
|
| } |
| 205 |
|
| { |
| 206 |
|
| frx_err e; |
| 207 |
3 |
▶ | if (!frx_remove(x->doc, inc, &e)) { *x->err = e; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_remove(x->doc, inc, &e)) { *x->err = e; return -1; } |
0 |
3 |
3 |
|
| 208 |
|
| } |
| 209 |
3 |
| return 1; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| /* the resolver, for one href already resolved to an absolute URI */ |
| 213 |
|
| static int |
| 214 |
156 |
| frx_xi_fetch(frx_xi *x, const frx_str *uri, frx_fetched *out, size_t off) |
| 215 |
|
| { |
| 216 |
|
| const char *msg; |
| 217 |
156 |
▶ | if (!x->oe->resolve) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x->oe->resolve) |
1 |
155 |
156 |
|
| 218 |
1 |
| return FRX_XI_FAIL(x, off, "an xi:include cannot be read without a resolver; pass resolve"); |
| 219 |
155 |
▶ | if (x->fetches >= (x->oe->max_fetches ? x->oe->max_fetches : FRX_DEFAULT_MAX_FETCHES)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (x->fetches >= (x->oe->max_fetches ? x->oe->max_fetches : FRX_DEFAULT_MAX_FETCHES)) |
5 |
150 |
155 |
| if (x->fetches >= (x->oe->max_fetches ? x->oe->max_fetches : FRX_DEFAULT_MAX_FETCHES)) |
1 |
154 |
155 |
|
| 220 |
1 |
| return FRX_XI_FAIL(x, off, "more external fetches than max_fetches"); |
| 221 |
154 |
| x->fetches++; |
| 222 |
154 |
| memset(out, 0, sizeof *out); |
| 223 |
154 |
| msg = x->oe->resolve(x->oe->resolve_ud, NULL, uri->p, NULL, FRX_REF_XINCLUDE, out); |
| 224 |
154 |
▶ | if (msg) return FRX_XI_FAIL(x, off, msg); |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg) return FRX_XI_FAIL(x, off, msg); |
10 |
144 |
154 |
|
| 225 |
144 |
| return 1; |
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
|
| /* parse="text": the bytes as one text node under the named encoding */ |
| 229 |
|
| static frx_node * |
| 230 |
3 |
| frx_xi_text(frx_xi *x, const frx_fetched *f, const frx_str *enc_name, size_t off) |
| 231 |
|
| { |
| 232 |
|
| frx_enc enc; |
| 233 |
|
| frx_err e; |
| 234 |
|
| frx_node *t; |
| 235 |
|
| const char *utf8; |
| 236 |
|
| size_t ulen; |
| 237 |
|
| char name[64]; |
| 238 |
|
| |
| 239 |
3 |
| memset(&enc, 0, sizeof enc); |
| 240 |
3 |
▶ | if (enc_name && enc_name->len && enc_name->len < sizeof name) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (enc_name && enc_name->len && enc_name->len < sizeof name) { |
1 |
2 |
3 |
| if (enc_name && enc_name->len && enc_name->len < sizeof name) { |
1 |
0 |
1 |
| if (enc_name && enc_name->len && enc_name->len < sizeof name) { |
1 |
0 |
1 |
|
| 241 |
1 |
| memcpy(name, enc_name->p, enc_name->len); |
| 242 |
1 |
| name[enc_name->len] = '\0'; |
| 243 |
|
| } else { |
| 244 |
2 |
| name[0] = '\0'; |
| 245 |
|
| } |
| 246 |
3 |
▶ | if (!frx_enc_detect(&enc, (const unsigned char *)f->bytes, f->len, name[0] ? name : NULL, &e) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!frx_enc_detect(&enc, (const unsigned char *)f->bytes, f->len, name[0] ? name : NULL, &e) |
1 |
2 |
3 |
| if (!frx_enc_detect(&enc, (const unsigned char *)f->bytes, f->len, name[0] ? name : NULL, &e) |
3 |
0 |
3 |
|
| 247 |
3 |
▶ | || !frx_enc_run(&enc, (const unsigned char *)f->bytes, f->len, &e)) { |
Branch: 1 branch
| Branch | True | False | Total |
| || !frx_enc_run(&enc, (const unsigned char *)f->bytes, f->len, &e)) { |
0 |
3 |
3 |
|
| 248 |
0 |
| frx_enc_free(&enc); |
| 249 |
0 |
| *x->err = e; |
| 250 |
0 |
| x->err->offset = off; |
| 251 |
0 |
| return NULL; |
| 252 |
|
| } |
| 253 |
3 |
▶ | utf8 = enc.out ? enc.out : f->bytes + enc.bom; |
Branch: 1 branch
| Branch | True | False | Total |
| utf8 = enc.out ? enc.out : f->bytes + enc.bom; |
1 |
2 |
3 |
|
| 254 |
3 |
▶ | ulen = enc.out ? enc.out_len : f->len - (size_t)enc.bom; |
Branch: 1 branch
| Branch | True | False | Total |
| ulen = enc.out ? enc.out_len : f->len - (size_t)enc.bom; |
1 |
2 |
3 |
|
| 255 |
3 |
| t = frx_new_text(x->doc, utf8, ulen, &e); |
| 256 |
3 |
| frx_enc_free(&enc); |
| 257 |
3 |
▶ | if (!t) { *x->err = e; x->err->offset = off; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) { *x->err = e; x->err->offset = off; } |
1 |
2 |
3 |
|
| 258 |
3 |
| return t; |
| 259 |
|
| } |
| 260 |
|
| |
| 261 |
|
| /* one xi:include: fetch, build, replace. 1 done, 0 refused. */ |
| 262 |
|
| static int |
| 263 |
168 |
| frx_xi_one(frx_xi *x, const frx_xi_work *w) |
| 264 |
|
| { |
| 265 |
168 |
| frx_node *inc = w->inc; |
| 266 |
168 |
| const frx_str *href = frx_xi_attr(inc, "href"); |
| 267 |
168 |
| const frx_str *mode = frx_xi_attr(inc, "parse"); |
| 268 |
168 |
| const frx_str *ptr = frx_xi_attr(inc, "xpointer"); |
| 269 |
168 |
| const frx_str *encn = frx_xi_attr(inc, "encoding"); |
| 270 |
168 |
▶ | int text = mode && frx_str_eq(mode, "text", 4); |
Branch: 2 branches
| Branch | True | False | Total |
| int text = mode && frx_str_eq(mode, "text", 4); |
4 |
164 |
168 |
| int text = mode && frx_str_eq(mode, "text", 4); |
3 |
1 |
4 |
|
| 271 |
|
| frx_buf base, uri; |
| 272 |
|
| frx_str uri_s; |
| 273 |
|
| frx_fetched f; |
| 274 |
168 |
| frx_node *content = NULL; |
| 275 |
168 |
| frx_doc *sub = NULL; |
| 276 |
|
| const frx_xi_chain *c; |
| 277 |
|
| frx_err e; |
| 278 |
168 |
| int ok = 1, fetched = 0, rc; |
| 279 |
|
| |
| 280 |
168 |
▶ | if (mode && !text && !frx_str_eq(mode, "xml", 3)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (mode && !text && !frx_str_eq(mode, "xml", 3)) |
4 |
164 |
168 |
| if (mode && !text && !frx_str_eq(mode, "xml", 3)) |
1 |
3 |
4 |
| if (mode && !text && !frx_str_eq(mode, "xml", 3)) |
1 |
0 |
1 |
|
| 281 |
1 |
| return FRX_XI_FAIL(x, inc->offset, "parse must be xml or text"); |
| 282 |
167 |
▶ | if (w->depth + 1 > x->max_depth) |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->depth + 1 > x->max_depth) |
2 |
165 |
167 |
|
| 283 |
2 |
| return FRX_XI_FAIL(x, inc->offset, "xi:include nested deeper than max_xinclude_depth"); |
| 284 |
165 |
▶ | if (!href && !ptr) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!href && !ptr) |
7 |
158 |
165 |
| if (!href && !ptr) |
5 |
2 |
7 |
|
| 285 |
5 |
| return FRX_XI_FAIL(x, inc->offset, "an xi:include needs an href or an xpointer"); |
| 286 |
|
| |
| 287 |
|
| /* the href against the base in force at the include element */ |
| 288 |
160 |
| frx_buf_init(&base); |
| 289 |
160 |
| frx_buf_init(&uri); |
| 290 |
160 |
▶ | if (!frx_node_base_uri(inc, x->doc, &base)) { frx_buf_free(&base); frx_buf_free(&uri); return FRX_XI_FAIL(x, inc->offset, "out of memory"); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_node_base_uri(inc, x->doc, &base)) { frx_buf_free(&base); frx_buf_free(&uri); return FRX_XI_FAIL(x, inc->offset, "out of memory"); } |
0 |
160 |
160 |
|
| 291 |
160 |
▶ | if (href) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (href) { |
158 |
2 |
160 |
|
| 292 |
158 |
▶ | if (!frx_uri_join(&uri, base.p ? base.p : "", base.len, href->p, href->len, 0)) ok = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!frx_uri_join(&uri, base.p ? base.p : "", base.len, href->p, href->len, 0)) ok = 0; |
158 |
0 |
158 |
| if (!frx_uri_join(&uri, base.p ? base.p : "", base.len, href->p, href->len, 0)) ok = 0; |
0 |
158 |
158 |
|
| 293 |
|
| } else { |
| 294 |
2 |
▶ | frx_buf_append_n(&uri, base.p ? base.p : "", base.len); |
Branch: 1 branch
| Branch | True | False | Total |
| frx_buf_append_n(&uri, base.p ? base.p : "", base.len); |
2 |
0 |
2 |
|
| 295 |
|
| } |
| 296 |
160 |
▶ | if (ok) ok = !uri.failed && frx_edit_dup(x->doc, uri.p ? uri.p : "", uri.len, &uri_s); |
Branch: 4 branches
| Branch | True | False | Total |
| if (ok) ok = !uri.failed && frx_edit_dup(x->doc, uri.p ? uri.p : "", uri.len, &uri_s); |
160 |
0 |
160 |
| if (ok) ok = !uri.failed && frx_edit_dup(x->doc, uri.p ? uri.p : "", uri.len, &uri_s); |
160 |
0 |
160 |
| if (ok) ok = !uri.failed && frx_edit_dup(x->doc, uri.p ? uri.p : "", uri.len, &uri_s); |
160 |
0 |
160 |
| if (ok) ok = !uri.failed && frx_edit_dup(x->doc, uri.p ? uri.p : "", uri.len, &uri_s); |
160 |
0 |
160 |
|
| 297 |
160 |
| frx_buf_free(&base); |
| 298 |
160 |
| frx_buf_free(&uri); |
| 299 |
160 |
▶ | if (!ok) return FRX_XI_FAIL(x, inc->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) return FRX_XI_FAIL(x, inc->offset, "out of memory"); |
0 |
160 |
160 |
|
| 300 |
|
| |
| 301 |
|
| /* a loop is fatal, fallback or not */ |
| 302 |
160 |
▶ | if (href) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (href) { |
158 |
2 |
160 |
|
| 303 |
424 |
▶ | for (c = w->chain; c; c = c->up) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = w->chain; c; c = c->up) |
268 |
156 |
424 |
|
| 304 |
268 |
▶ | if (frx_str_eq2(&c->uri, &uri_s)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_str_eq2(&c->uri, &uri_s)) |
2 |
266 |
268 |
|
| 305 |
2 |
| return FRX_XI_FAIL(x, inc->offset, "an xi:include names a document already being included: a loop"); |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
158 |
▶ | if (href) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (href) { |
156 |
2 |
158 |
|
| 309 |
156 |
▶ | if (!frx_xi_fetch(x, &uri_s, &f, inc->offset)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xi_fetch(x, &uri_s, &f, inc->offset)) { |
12 |
144 |
156 |
|
| 310 |
|
| /* a resource error: the fallback, or fatal */ |
| 311 |
12 |
| rc = frx_xi_fallback(x, inc); |
| 312 |
12 |
▶ | if (rc < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) return 0; |
0 |
12 |
12 |
|
| 313 |
12 |
▶ | if (rc == 0) return 0; /* the fetch's own message stands */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) return 0; /* the fetch's own message stands */ |
10 |
2 |
12 |
|
| 314 |
2 |
| frx_err_set(x->err, FRX_OK, 0, NULL); |
| 315 |
2 |
| return 1; |
| 316 |
|
| } |
| 317 |
144 |
| fetched = 1; |
| 318 |
|
| } |
| 319 |
|
| |
| 320 |
146 |
▶ | if (text) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (text) { |
3 |
143 |
146 |
|
| 321 |
3 |
▶ | if (!href) { if (fetched && f.release) f.release(f.ud, &f); return FRX_XI_FAIL(x, inc->offset, "parse=\"text\" needs an href"); } |
Branch: 3 branches
| Branch | True | False | Total |
| if (!href) { if (fetched && f.release) f.release(f.ud, &f); return FRX_XI_FAIL(x, inc->offset, "parse=\"text\" needs an href"); } |
0 |
3 |
3 |
| if (!href) { if (fetched && f.release) f.release(f.ud, &f); return FRX_XI_FAIL(x, inc->offset, "parse=\"text\" needs an href"); } |
0 |
0 |
0 |
| if (!href) { if (fetched && f.release) f.release(f.ud, &f); return FRX_XI_FAIL(x, inc->offset, "parse=\"text\" needs an href"); } |
0 |
0 |
0 |
|
| 322 |
3 |
| content = frx_xi_text(x, &f, encn, inc->offset); |
| 323 |
3 |
▶ | if (f.release) f.release(f.ud, &f); |
Branch: 1 branch
| Branch | True | False | Total |
| if (f.release) f.release(f.ud, &f); |
3 |
0 |
3 |
|
| 324 |
3 |
▶ | if (!content) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!content) { |
1 |
2 |
3 |
|
| 325 |
1 |
| rc = frx_xi_fallback(x, inc); |
| 326 |
1 |
▶ | if (rc <= 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc <= 0) return 0; |
1 |
0 |
1 |
|
| 327 |
0 |
| frx_err_set(x->err, FRX_OK, 0, NULL); |
| 328 |
0 |
| return 1; |
| 329 |
|
| } |
| 330 |
|
| } else { |
| 331 |
|
| frx_node *pick; |
| 332 |
143 |
▶ | if (href) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (href) { |
141 |
2 |
143 |
|
| 333 |
141 |
| frx_opts_ex so = *x->oe; |
| 334 |
141 |
| so.xinclude = 0; /* this pass owns the nesting */ |
| 335 |
141 |
| so.base_uri = uri_s.p; |
| 336 |
141 |
| sub = frx_parse_doc_ex(f.bytes, f.len, &so, &e); |
| 337 |
141 |
▶ | if (f.release) f.release(f.ud, &f); |
Branch: 1 branch
| Branch | True | False | Total |
| if (f.release) f.release(f.ud, &f); |
141 |
0 |
141 |
|
| 338 |
141 |
▶ | if (!sub) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sub) { |
2 |
139 |
141 |
|
| 339 |
2 |
| rc = frx_xi_fallback(x, inc); |
| 340 |
6 |
▶ | if (rc < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) return 0; |
0 |
2 |
2 |
|
| 341 |
2 |
▶ | if (rc == 0) { *x->err = e; x->err->offset = inc->offset; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) { *x->err = e; x->err->offset = inc->offset; return 0; } |
1 |
1 |
2 |
|
| 342 |
1 |
| frx_err_set(x->err, FRX_OK, 0, NULL); |
| 343 |
1 |
| return 1; |
| 344 |
|
| } |
| 345 |
139 |
▶ | pick = ptr ? frx_xi_xpointer(x, sub, ptr, inc->offset) : sub->root; |
Branch: 1 branch
| Branch | True | False | Total |
| pick = ptr ? frx_xi_xpointer(x, sub, ptr, inc->offset) : sub->root; |
6 |
133 |
139 |
|
| 346 |
139 |
▶ | if (!pick) { frx_doc_free(sub); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pick) { frx_doc_free(sub); return 0; } |
2 |
137 |
139 |
|
| 347 |
137 |
| content = frx_import(x->doc, pick, &e); |
| 348 |
137 |
| frx_doc_free(sub); |
| 349 |
137 |
▶ | if (!content) { *x->err = e; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!content) { *x->err = e; return 0; } |
0 |
137 |
137 |
|
| 350 |
|
| } else { |
| 351 |
|
| /* a same-document include: the pointer over this document */ |
| 352 |
2 |
▶ | pick = ptr ? frx_xi_xpointer(x, x->doc, ptr, inc->offset) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| pick = ptr ? frx_xi_xpointer(x, x->doc, ptr, inc->offset) : NULL; |
2 |
0 |
2 |
|
| 353 |
2 |
▶ | if (!pick) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pick) return 0; |
0 |
2 |
2 |
|
| 354 |
2 |
▶ | if (frx_edit_is_ancestor(pick, inc)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_edit_is_ancestor(pick, inc)) |
1 |
1 |
2 |
|
| 355 |
1 |
| return FRX_XI_FAIL(x, inc->offset, "a same-document xi:include names an ancestor of itself: a loop"); |
| 356 |
1 |
| content = frx_import(x->doc, pick, &e); |
| 357 |
1 |
▶ | if (!content) { *x->err = e; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!content) { *x->err = e; return 0; } |
0 |
1 |
1 |
|
| 358 |
|
| } |
| 359 |
|
| /* section 4.7.5: the included root carries where it came from, |
| 360 |
|
| * when that differs from the base the include element stood at */ |
| 361 |
138 |
▶ | if (href && content->kind == FRX_ELEMENT) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (href && content->kind == FRX_ELEMENT) { |
137 |
1 |
138 |
| if (href && content->kind == FRX_ELEMENT) { |
137 |
0 |
137 |
|
| 362 |
|
| frx_buf inc_base; |
| 363 |
137 |
| frx_buf_init(&inc_base); |
| 364 |
137 |
▶ | if (frx_node_base_uri(inc, x->doc, &inc_base) |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_node_base_uri(inc, x->doc, &inc_base) |
137 |
0 |
137 |
|
| 365 |
137 |
▶ | && !(inc_base.len == uri_s.len && (!uri_s.len || memcmp(inc_base.p, uri_s.p, uri_s.len) == 0))) { |
Branch: 3 branches
| Branch | True | False | Total |
| && !(inc_base.len == uri_s.len && (!uri_s.len || memcmp(inc_base.p, uri_s.p, uri_s.len) == 0))) { |
125 |
12 |
137 |
| && !(inc_base.len == uri_s.len && (!uri_s.len || memcmp(inc_base.p, uri_s.p, uri_s.len) == 0))) { |
125 |
0 |
125 |
| && !(inc_base.len == uri_s.len && (!uri_s.len || memcmp(inc_base.p, uri_s.p, uri_s.len) == 0))) { |
125 |
0 |
125 |
|
| 366 |
137 |
▶ | if (!frx_set_attr(x->doc, content, FRX_XML_NS, sizeof FRX_XML_NS - 1, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_set_attr(x->doc, content, FRX_XML_NS, sizeof FRX_XML_NS - 1, |
0 |
137 |
137 |
|
| 367 |
|
| "xml", 3, "base", 4, uri_s.p, uri_s.len, &e)) { |
| 368 |
0 |
| frx_buf_free(&inc_base); |
| 369 |
0 |
| *x->err = e; |
| 370 |
0 |
| return 0; |
| 371 |
|
| } |
| 372 |
|
| } |
| 373 |
137 |
| frx_buf_free(&inc_base); |
| 374 |
|
| } |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
140 |
▶ | if (!frx_insert_before(x->doc, inc, content, &e)) { *x->err = e; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_insert_before(x->doc, inc, content, &e)) { *x->err = e; return 0; } |
0 |
140 |
140 |
|
| 378 |
140 |
▶ | if (!frx_remove(x->doc, inc, &e)) { *x->err = e; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_remove(x->doc, inc, &e)) { *x->err = e; return 0; } |
0 |
140 |
140 |
|
| 379 |
|
| |
| 380 |
|
| /* what was just imported may include further documents */ |
| 381 |
140 |
▶ | if (!text) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!text) { |
138 |
2 |
140 |
|
| 382 |
138 |
| frx_xi_chain *link = (frx_xi_chain *)frx_arena_alloc(&x->doc->arena, sizeof *link); |
| 383 |
138 |
▶ | if (!link) return FRX_XI_FAIL(x, inc->offset, "out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!link) return FRX_XI_FAIL(x, inc->offset, "out of memory"); |
0 |
138 |
138 |
|
| 384 |
138 |
| link->uri = uri_s; |
| 385 |
138 |
| link->up = w->chain; |
| 386 |
138 |
▶ | if (!frx_xi_scan(x, content, link, w->depth + 1)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xi_scan(x, content, link, w->depth + 1)) return 0; |
0 |
138 |
138 |
|
| 387 |
|
| } |
| 388 |
140 |
| return 1; |
| 389 |
|
| } |
| 390 |
|
| |
| 391 |
|
| /* the pass over the document; 0 with *err on refusal */ |
| 392 |
|
| static int |
| 393 |
179 |
| frx_xinclude_doc(frx_doc *d, const frx_opts_ex *oe, frx_err *err) |
| 394 |
|
| { |
| 395 |
|
| frx_xi x; |
| 396 |
|
| frx_xi_chain root_chain; |
| 397 |
179 |
| int i, ok = 1; |
| 398 |
|
| |
| 399 |
179 |
| memset(&x, 0, sizeof x); |
| 400 |
179 |
| x.doc = d; |
| 401 |
179 |
| x.oe = oe; |
| 402 |
179 |
| x.err = err; |
| 403 |
179 |
▶ | x.max_depth = oe->max_xinclude_depth ? oe->max_xinclude_depth : FRX_DEFAULT_MAX_XINCLUDE_DEPTH; |
Branch: 1 branch
| Branch | True | False | Total |
| x.max_depth = oe->max_xinclude_depth ? oe->max_xinclude_depth : FRX_DEFAULT_MAX_XINCLUDE_DEPTH; |
2 |
177 |
179 |
|
| 404 |
179 |
| frx_err_set(err, FRX_OK, 0, NULL); |
| 405 |
|
| |
| 406 |
179 |
| root_chain.uri = d->base_uri; |
| 407 |
179 |
| root_chain.up = NULL; |
| 408 |
179 |
▶ | if (!d->document || !frx_xi_scan(&x, d->document, &root_chain, 0)) { free(x.work); return 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!d->document || !frx_xi_scan(&x, d->document, &root_chain, 0)) { free(x.work); return 0; } |
179 |
0 |
179 |
| if (!d->document || !frx_xi_scan(&x, d->document, &root_chain, 0)) { free(x.work); return 0; } |
0 |
179 |
179 |
|
| 409 |
|
| |
| 410 |
|
| /* the list grows as includes bring in more; each is processed once */ |
| 411 |
347 |
▶ | for (i = 0; ok && i < x.n_work; i++) ok = frx_xi_one(&x, &x.work[i]); |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; ok && i < x.n_work; i++) ok = frx_xi_one(&x, &x.work[i]); |
322 |
25 |
347 |
| for (i = 0; ok && i < x.n_work; i++) ok = frx_xi_one(&x, &x.work[i]); |
168 |
154 |
322 |
|
| 412 |
179 |
| free(x.work); |
| 413 |
179 |
▶ | if (ok) d->renumber = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok) d->renumber = 1; |
154 |
25 |
179 |
|
| 414 |
179 |
| return ok; |
| 415 |
|
| } |
| 416 |
|
| |
| 417 |
|
| #endif /* FRX_XINCLUDE_H */ |