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