| 1 |
|
| /* |
| 2 |
|
| * XML.xs - root XS file |
| 3 |
|
| * |
| 4 |
|
| * The perl headers and File::Raw's plugin registry, then the shims the 5.10 |
| 5 |
|
| * floor costs, then the C implementation headers from include/frx/ in |
| 6 |
|
| * dependency order, then the per-package XS fragments from xs/ via |
| 7 |
|
| * INCLUDE: (the Punk-Feed layout). |
| 8 |
|
| * |
| 9 |
|
| * One bundle for every package in the distribution. Each .pm under |
| 10 |
|
| * lib/File/Raw/XML/ loads File::Raw::XML and nothing else, so whichever |
| 11 |
|
| * module is loaded first bootstraps all of them. |
| 12 |
|
| * |
| 13 |
|
| * The rules every header under include/frx/ follows: |
| 14 |
|
| * |
| 15 |
|
| * 1. The core is perl-free. frx_arena, frx_buf, frx_err and everything |
| 16 |
|
| * built on them (utf8, lex, tree, ns, parse, c14n) use the C |
| 17 |
|
| * standard library and size_t and never call a Perl_* API or touch an |
| 18 |
|
| * SV. The SV layer is frx_obj.h, frx_plugin.h and frx_abi_impl.h. A |
| 19 |
|
| * stub perl.h under tools/stub/ compiles the core alone; so does a |
| 20 |
|
| * libFuzzer harness. |
| 21 |
|
| * 2. Nothing recurses. max_depth is the caller's, so no function uses the |
| 22 |
|
| * C stack in proportion to document depth: an explicit open-element |
| 23 |
|
| * stack in the arena, and every walk iterative over first_child, next |
| 24 |
|
| * and parent. |
| 25 |
|
| * 3. Allocation is malloc, realloc and free from <stdlib.h>, with the |
| 26 |
|
| * undefs in frx_compat.h so PERL_IMPLICIT_SYS cannot rewrite them. |
| 27 |
|
| * Never Newx: the stub perl.h has none. |
| 28 |
|
| * 4. Errors are a struct, not a croak. frx_err is filled by the core and |
| 29 |
|
| * frx_err_format renders the one message shape; the Perl boundary |
| 30 |
|
| * croaks with it, the ABI returns NULL and a mortal SV. |
| 31 |
|
| * 5. Every core function is ABI-shaped from day one: it never croaks and |
| 32 |
|
| * takes const frx_opts * / const frx_c14n * from frx_abi.h directly. |
| 33 |
|
| * 6. C89 declarations at block top, no VLAs, no designated initialisers, |
| 34 |
|
| * no %zu. Everything static, one translation unit. |
| 35 |
|
| * 7. Private test accessors are underscore XSUBs in xs/internal.xs. |
| 36 |
|
| */ |
| 37 |
|
| |
| 38 |
|
| #define PERL_NO_GET_CONTEXT |
| 39 |
|
| #include "EXTERN.h" |
| 40 |
|
| #include "perl.h" |
| 41 |
|
| #include "XSUB.h" |
| 42 |
|
| |
| 43 |
|
| #include "file_plugin.h" /* File::Raw's registry, via ExtUtils::Depends */ |
| 44 |
|
| #include "file_chunk.h" /* and its pull reads, for Reader->from_file */ |
| 45 |
|
| |
| 46 |
|
| /* The implementation headers, in dependency order. Each states its contract |
| 47 |
|
| * at the top and names what must precede it. */ |
| 48 |
|
| |
| 49 |
|
| #include "frx/frx_compat.h" /* what the 5.10 floor costs; must be first */ |
| 50 |
|
| |
| 51 |
|
| #include <stdlib.h> |
| 52 |
|
| #include <string.h> |
| 53 |
|
| |
| 54 |
|
| #include "frx_abi.h" /* the public types; the core is written against them */ |
| 55 |
|
| #include "frx/frx_err.h" /* the refusal shape (needs nothing) */ |
| 56 |
|
| #include "frx/frx_arena.h" /* the allocator, frx_str (needs nothing) */ |
| 57 |
|
| #include "frx/frx_buf.h" /* output bytes (needs nothing) */ |
| 58 |
|
| #include "frx/frx_uri.h" /* URI references and the RFC 3986 join (needs buf) */ |
| 59 |
|
| #include "frx/frx_utf8.h" /* scalars, Char, Name (needs nothing) */ |
| 60 |
|
| #include "frx/frx_enc.h" /* the transcoder, full only (needs err, utf8) */ |
| 61 |
|
| #include "frx/frx_tree.h" /* frx_node, frx_doc (needs abi, arena, utf8) */ |
| 62 |
|
| #include "frx/frx_lex.h" /* tokens, the entity frames (needs err, arena, buf, utf8, enc, tree) */ |
| 63 |
|
| #include "frx/frx_dtd.h" /* the DOCTYPE and the internal subset, full only (needs lex) */ |
| 64 |
|
| #include "frx/frx_entity.h" /* entity expansion and its budgets (needs lex, dtd) */ |
| 65 |
|
| #include "frx/frx_ns.h" /* scopes, interning, the rules (needs tree) */ |
| 66 |
|
| #include "frx/frx_parse.h" /* the driver, the index, the walkers (all) */ |
| 67 |
|
| #include "frx/frx_reader.h" /* the pull reader over the driver (needs parse, enc) */ |
| 68 |
|
| #include "frx/frx_model.h" /* content models to an automaton (needs dtd) */ |
| 69 |
|
| #include "frx/frx_validate.h" /* the validity constraints (needs parse, model) */ |
| 70 |
|
| #include "frx/frx_c14n.h" /* the serialiser, the three algorithms */ |
| 71 |
|
| #include "frx/frx_write.h" /* the general serialiser (needs c14n for its escapers) */ |
| 72 |
|
| #include "frx/frx_edit.h" /* the mutable tree (needs tree, ns, parse) */ |
| 73 |
|
| #include "frx/frx_xmlattr.h" /* xml:base, xml:lang, xml:space on a node (needs tree, uri) */ |
| 74 |
|
| #include "frx/frx_xinclude.h" /* XInclude 1.0 (needs parse, edit, xmlattr, enc) */ |
| 75 |
|
| #include "frx/frx_xpath_lex.h" /* XPath 1.0 tokens (needs err, buf, utf8, tree) */ |
| 76 |
|
| #include "frx/frx_xpath_parse.h" /* the grammar to an AST (needs arena, xpath_lex) */ |
| 77 |
|
| #include "frx/frx_xpath_eval.h" /* the four types, the axes, the functions (needs parse) */ |
| 78 |
|
| #include "frx/frx_xpath.h" /* compile once, evaluate many (needs the three above) */ |
| 79 |
|
| #include "frx/frx_abi_impl.h" /* the table behind _abi_ptr (needs all of the core) */ |
| 80 |
|
| #include "frx/frx_obj.h" /* the SV layer: blessing, owner ref, options */ |
| 81 |
|
| #include "frx/frx_plugin.h" /* the File::Raw plugin (needs obj) */ |
| 82 |
|
| |
| 83 |
|
| /* c14n(%opts) from an options HV: mode, comments, prefix_list, without. |
| 84 |
|
| * The arrays live on the savestack for the call. doc_iv identifies the |
| 85 |
|
| * document every `without` node must belong to. */ |
| 86 |
|
| static void |
| 87 |
2713 |
| frx_c14n_opts(pTHX_ HV *hv, SV *doc_iv, frx_c14n *c, const char *who) |
| 88 |
|
| { |
| 89 |
|
| HE *he; |
| 90 |
2713 |
| c->mode = FRX_C14N_EXC; |
| 91 |
2713 |
| c->comments = 0; |
| 92 |
2713 |
| c->prefix_list = NULL; |
| 93 |
2713 |
| c->n_prefix = 0; |
| 94 |
2713 |
| c->without = NULL; |
| 95 |
2713 |
| c->n_without = 0; |
| 96 |
2713 |
▶ | if (!hv) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv) return; |
0 |
2713 |
2713 |
|
| 97 |
2713 |
| hv_iterinit(hv); |
| 98 |
2920 |
▶ | while ((he = hv_iternext(hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(hv))) { |
212 |
2708 |
2920 |
|
| 99 |
|
| I32 klen; |
| 100 |
212 |
| const char *k = hv_iterkey(he, &klen); |
| 101 |
212 |
| SV *v = hv_iterval(hv, he); |
| 102 |
363 |
▶ | if (klen == 4 && memcmp(k, "mode", 4) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (klen == 4 && memcmp(k, "mode", 4) == 0) { |
152 |
60 |
212 |
| if (klen == 4 && memcmp(k, "mode", 4) == 0) { |
152 |
0 |
152 |
|
| 103 |
|
| STRLEN n; |
| 104 |
152 |
| const char *m = SvPV(v, n); |
| 105 |
152 |
▶ | if (n == 9 && memcmp(m, "exclusive", 9) == 0) c->mode = FRX_C14N_EXC; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 9 && memcmp(m, "exclusive", 9) == 0) c->mode = FRX_C14N_EXC; |
136 |
16 |
152 |
| if (n == 9 && memcmp(m, "exclusive", 9) == 0) c->mode = FRX_C14N_EXC; |
56 |
80 |
136 |
|
| 106 |
96 |
▶ | else if (n == 9 && memcmp(m, "inclusive", 9) == 0) c->mode = FRX_C14N_INC10; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (n == 9 && memcmp(m, "inclusive", 9) == 0) c->mode = FRX_C14N_INC10; |
80 |
16 |
96 |
| else if (n == 9 && memcmp(m, "inclusive", 9) == 0) c->mode = FRX_C14N_INC10; |
80 |
0 |
80 |
|
| 107 |
16 |
▶ | else if (n == 13 && memcmp(m, "inclusive-1.1", 13) == 0) c->mode = FRX_C14N_INC11; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (n == 13 && memcmp(m, "inclusive-1.1", 13) == 0) c->mode = FRX_C14N_INC11; |
15 |
1 |
16 |
| else if (n == 13 && memcmp(m, "inclusive-1.1", 13) == 0) c->mode = FRX_C14N_INC11; |
15 |
0 |
15 |
|
| 108 |
1 |
| else croak("File::Raw::XML: %s: mode must be 'exclusive', 'inclusive' or " |
| 109 |
|
| "'inclusive-1.1', not '%.*s'", who, (int)n, m); |
| 110 |
60 |
▶ | } else if (klen == 8 && memcmp(k, "comments", 8) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (klen == 8 && memcmp(k, "comments", 8) == 0) { |
38 |
22 |
60 |
| } else if (klen == 8 && memcmp(k, "comments", 8) == 0) { |
38 |
0 |
38 |
|
| 111 |
38 |
| c->comments = SvTRUE(v) ? 1 : 0; |
| 112 |
26 |
▶ | } else if (klen == 11 && memcmp(k, "prefix_list", 11) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (klen == 11 && memcmp(k, "prefix_list", 11) == 0) { |
5 |
17 |
22 |
| } else if (klen == 11 && memcmp(k, "prefix_list", 11) == 0) { |
5 |
0 |
5 |
|
| 113 |
|
| AV *av; |
| 114 |
|
| I32 n, i; |
| 115 |
|
| const char **names; |
| 116 |
5 |
▶ | if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) |
4 |
1 |
5 |
| if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) |
0 |
4 |
4 |
|
| 117 |
1 |
| croak("File::Raw::XML: %s: prefix_list must be an arrayref of prefixes", who); |
| 118 |
4 |
| av = (AV *)SvRV(v); |
| 119 |
4 |
| n = (I32)(av_len(av) + 1); |
| 120 |
4 |
▶ | if (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) { |
4 |
0 |
4 |
|
| 121 |
4 |
| Newx(names, n, const char *); |
| 122 |
4 |
| SAVEFREEPV(names); |
| 123 |
8 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
4 |
4 |
8 |
|
| 124 |
4 |
| SV **e = av_fetch(av, i, 0); |
| 125 |
4 |
▶ | if (!e || !SvOK(*e)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e || !SvOK(*e)) |
4 |
0 |
4 |
| if (!e || !SvOK(*e)) |
0 |
4 |
4 |
|
| 126 |
0 |
| croak("File::Raw::XML: %s: prefix_list must be an arrayref of prefixes", who); |
| 127 |
8 |
| names[i] = SvPV_nolen(*e); |
| 128 |
|
| } |
| 129 |
4 |
| c->prefix_list = names; |
| 130 |
4 |
| c->n_prefix = (int)n; |
| 131 |
|
| } |
| 132 |
31 |
▶ | } else if (klen == 7 && memcmp(k, "without", 7) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (klen == 7 && memcmp(k, "without", 7) == 0) { |
16 |
1 |
17 |
| } else if (klen == 7 && memcmp(k, "without", 7) == 0) { |
16 |
0 |
16 |
|
| 133 |
|
| AV *av; |
| 134 |
|
| I32 n, i; |
| 135 |
|
| const frx_node **nodes; |
| 136 |
16 |
▶ | if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) |
15 |
1 |
16 |
| if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) |
0 |
15 |
15 |
|
| 137 |
1 |
| croak("File::Raw::XML: %s: without must be an arrayref of nodes", who); |
| 138 |
15 |
| av = (AV *)SvRV(v); |
| 139 |
15 |
| n = (I32)(av_len(av) + 1); |
| 140 |
15 |
▶ | if (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) { |
15 |
0 |
15 |
|
| 141 |
15 |
| Newx(nodes, n, const frx_node *); |
| 142 |
15 |
| SAVEFREEPV(nodes); |
| 143 |
30 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
16 |
14 |
30 |
|
| 144 |
16 |
| SV **e = av_fetch(av, i, 0); |
| 145 |
16 |
| SV *their_doc = NULL; |
| 146 |
16 |
▶ | if (!e) croak("File::Raw::XML: %s: without must be an arrayref of nodes", who); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) croak("File::Raw::XML: %s: without must be an arrayref of nodes", who); |
0 |
16 |
16 |
|
| 147 |
16 |
| nodes[i] = frx_node_from_sv(aTHX_ *e, who, &their_doc); |
| 148 |
16 |
▶ | if (their_doc != doc_iv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (their_doc != doc_iv) |
1 |
15 |
16 |
|
| 149 |
1 |
| croak("File::Raw::XML: %s: a without node belongs to another document", who); |
| 150 |
|
| } |
| 151 |
14 |
| c->without = nodes; |
| 152 |
14 |
| c->n_without = (int)n; |
| 153 |
|
| } |
| 154 |
|
| } else { |
| 155 |
1 |
| croak("File::Raw::XML: %s: unknown option '%.*s'", who, (int)klen, k); |
| 156 |
|
| } |
| 157 |
|
| } |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
|
| /* the canonical bytes of apex under c, as a +1 SV with no character flag */ |
| 161 |
|
| static SV * |
| 162 |
2708 |
| frx_c14n_sv(pTHX_ const frx_node *apex, const frx_c14n *c, const char *who) |
| 163 |
|
| { |
| 164 |
|
| frx_buf b; |
| 165 |
|
| SV *out; |
| 166 |
2708 |
| frx_buf_init(&b); |
| 167 |
2708 |
▶ | if (!frx_c14n_render(apex, c, &b)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_c14n_render(apex, c, &b)) { |
0 |
2708 |
2708 |
|
| 168 |
0 |
| frx_buf_free(&b); |
| 169 |
0 |
| croak("File::Raw::XML: %s: out of memory", who); |
| 170 |
|
| } |
| 171 |
2708 |
▶ | out = newSVpvn(b.p ? b.p : "", b.len); |
Branch: 1 branch
| Branch | True | False | Total |
| out = newSVpvn(b.p ? b.p : "", b.len); |
2705 |
3 |
2708 |
|
| 172 |
2708 |
| frx_buf_free(&b); |
| 173 |
2708 |
| return out; |
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
|
| /* the explicit stack the _dump accessor walks with (rule 2) */ |
| 177 |
|
| typedef struct frx_dump_frame { |
| 178 |
|
| const frx_node *node; |
| 179 |
|
| AV *children; |
| 180 |
|
| } frx_dump_frame; |
| 181 |
|
| |
| 182 |
|
| /* ---- the XPath SV layer ------------------------------------------------ |
| 183 |
|
| * |
| 184 |
|
| * A compiled expression is a blessed IV with ext magic whose free hook |
| 185 |
|
| * calls frx_xpath_free; it holds no document, so it carries no owner |
| 186 |
|
| * reference and is the one object here that does not. |
| 187 |
|
| * |
| 188 |
|
| * An attribute and a namespace node in a result are not frx_nodes, so |
| 189 |
|
| * they cannot be File::Raw::XML::Node. Each is a blessed IV carrying the |
| 190 |
|
| * SAME owner reference a node does - the owning element in mg_ptr, the |
| 191 |
|
| * document's inner IV in mg_obj - with the entry's index as the IV's own |
| 192 |
|
| * value. So an Attr keeps its document alive exactly as a Node does, and |
| 193 |
|
| * a distinct vtable per class is what keeps the three apart at the |
| 194 |
|
| * invocant check. */ |
| 195 |
|
| |
| 196 |
|
| #define FRX_XPATH_CLASS "File::Raw::XML::XPath" |
| 197 |
|
| #define FRX_ATTR_CLASS "File::Raw::XML::Attr" |
| 198 |
|
| #define FRX_NS_CLASS "File::Raw::XML::Namespace" |
| 199 |
|
| |
| 200 |
|
| static int |
| 201 |
7 |
| frx_xpath_mg_free(pTHX_ SV *sv, MAGIC *mg) |
| 202 |
|
| { |
| 203 |
7 |
| frx_xpath *x = (frx_xpath *)mg->mg_ptr; |
| 204 |
|
| PERL_UNUSED_ARG(sv); |
| 205 |
7 |
▶ | if (x) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x) { |
7 |
0 |
7 |
|
| 206 |
7 |
| frx_xpath_free(x); |
| 207 |
7 |
| mg->mg_ptr = NULL; |
| 208 |
|
| } |
| 209 |
7 |
| return 0; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| static MGVTBL frx_xpath_vtbl = { NULL, NULL, NULL, NULL, frx_xpath_mg_free, NULL, NULL, NULL }; |
| 213 |
|
| static MGVTBL frx_attr_vtbl = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; |
| 214 |
|
| static MGVTBL frx_nsnode_vtbl = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; |
| 215 |
|
| |
| 216 |
|
| static SV * |
| 217 |
7 |
| frx_xpath_bless(pTHX_ frx_xpath *x, const char *class_name) |
| 218 |
|
| { |
| 219 |
7 |
| SV *iv = newSViv(0); |
| 220 |
7 |
| sv_magicext(iv, NULL, PERL_MAGIC_ext, &frx_xpath_vtbl, (const char *)x, 0); |
| 221 |
7 |
| return sv_bless(newRV_noinc(iv), gv_stashpv(class_name, GV_ADD)); |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
|
| static frx_xpath * |
| 225 |
11 |
| frx_xpath_from_sv(pTHX_ SV *sv, const char *meth) |
| 226 |
|
| { |
| 227 |
11 |
| MAGIC *mg = frx_obj_magic(aTHX_ sv, &frx_xpath_vtbl); |
| 228 |
11 |
▶ | if (!mg || !mg->mg_ptr) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!mg || !mg->mg_ptr) |
10 |
1 |
11 |
| if (!mg || !mg->mg_ptr) |
0 |
10 |
10 |
|
| 229 |
1 |
| croak("File::Raw::XML: %s: the invocant is not a " FRX_XPATH_CLASS, meth); |
| 230 |
10 |
| return (frx_xpath *)mg->mg_ptr; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
|
| /* an attribute or namespace entry of the element owner, +1, blessed */ |
| 234 |
|
| static SV * |
| 235 |
16 |
| frx_entry_bless(pTHX_ SV *doc_iv, const frx_node *owner, int index, int is_ns) |
| 236 |
|
| { |
| 237 |
16 |
| SV *iv = newSViv(index); |
| 238 |
16 |
▶ | sv_magicext(iv, doc_iv, PERL_MAGIC_ext, |
Branch: 1 branch
| Branch | True | False | Total |
| sv_magicext(iv, doc_iv, PERL_MAGIC_ext, |
6 |
10 |
16 |
|
| 239 |
|
| is_ns ? &frx_nsnode_vtbl : &frx_attr_vtbl, (const char *)owner, 0); |
| 240 |
16 |
▶ | return sv_bless(newRV_noinc(iv), |
Branch: 1 branch
| Branch | True | False | Total |
| return sv_bless(newRV_noinc(iv), |
6 |
10 |
16 |
|
| 241 |
|
| gv_stashpv(is_ns ? FRX_NS_CLASS : FRX_ATTR_CLASS, GV_ADD)); |
| 242 |
|
| } |
| 243 |
|
| |
| 244 |
|
| static const frx_node * |
| 245 |
44 |
| frx_entry_from_sv(pTHX_ SV *sv, const char *meth, int is_ns, int *index, SV **doc_iv) |
| 246 |
|
| { |
| 247 |
44 |
▶ | MAGIC *mg = frx_obj_magic(aTHX_ sv, is_ns ? &frx_nsnode_vtbl : &frx_attr_vtbl); |
Branch: 1 branch
| Branch | True | False | Total |
| MAGIC *mg = frx_obj_magic(aTHX_ sv, is_ns ? &frx_nsnode_vtbl : &frx_attr_vtbl); |
15 |
29 |
44 |
|
| 248 |
44 |
▶ | if (!mg || !mg->mg_ptr || !mg->mg_obj) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!mg || !mg->mg_ptr || !mg->mg_obj) |
43 |
1 |
44 |
| if (!mg || !mg->mg_ptr || !mg->mg_obj) |
43 |
0 |
43 |
| if (!mg || !mg->mg_ptr || !mg->mg_obj) |
0 |
43 |
43 |
|
| 249 |
1 |
▶ | croak("File::Raw::XML: %s: the invocant is not a %s", meth, |
Branch: 1 branch
| Branch | True | False | Total |
| croak("File::Raw::XML: %s: the invocant is not a %s", meth, |
1 |
0 |
1 |
|
| 250 |
|
| is_ns ? FRX_NS_CLASS : FRX_ATTR_CLASS); |
| 251 |
43 |
▶ | if (index) *index = (int)SvIV(SvRV(sv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (index) *index = (int)SvIV(SvRV(sv)); |
40 |
3 |
43 |
|
| 252 |
43 |
▶ | if (doc_iv) *doc_iv = mg->mg_obj; |
Branch: 1 branch
| Branch | True | False | Total |
| if (doc_iv) *doc_iv = mg->mg_obj; |
3 |
40 |
43 |
|
| 253 |
43 |
| return (const frx_node *)mg->mg_ptr; |
| 254 |
|
| } |
| 255 |
|
| |
| 256 |
|
| /* the frx_doc behind a document's inner IV */ |
| 257 |
|
| static frx_doc * |
| 258 |
2 |
| frx_doc_of_iv(pTHX_ SV *doc_iv, const char *meth) |
| 259 |
|
| { |
| 260 |
2 |
▶ | MAGIC *mg = doc_iv ? mg_findext(doc_iv, PERL_MAGIC_ext, &frx_doc_vtbl) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| MAGIC *mg = doc_iv ? mg_findext(doc_iv, PERL_MAGIC_ext, &frx_doc_vtbl) : NULL; |
2 |
0 |
2 |
|
| 261 |
2 |
▶ | if (!mg || !mg->mg_ptr) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!mg || !mg->mg_ptr) |
2 |
0 |
2 |
| if (!mg || !mg->mg_ptr) |
0 |
2 |
2 |
|
| 262 |
0 |
| croak("File::Raw::XML: %s: the node has no document", meth); |
| 263 |
2 |
| return (frx_doc *)mg->mg_ptr; |
| 264 |
|
| } |
| 265 |
|
| |
| 266 |
|
| /* the context of an evaluation: a Document is its document node, a Node |
| 267 |
|
| * is itself, and both name the document the results belong to */ |
| 268 |
|
| static const frx_node * |
| 269 |
237 |
| frx_xpath_context(pTHX_ SV *sv, const char *meth, frx_doc **doc, SV **doc_iv) |
| 270 |
|
| { |
| 271 |
237 |
▶ | if (frx_obj_magic(aTHX_ sv, &frx_doc_vtbl)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (frx_obj_magic(aTHX_ sv, &frx_doc_vtbl)) { |
235 |
2 |
237 |
|
| 272 |
235 |
| frx_doc *d = frx_doc_from_sv(aTHX_ sv, meth); |
| 273 |
235 |
| *doc = d; |
| 274 |
235 |
| *doc_iv = SvRV(sv); |
| 275 |
235 |
| return d->document; |
| 276 |
|
| } |
| 277 |
|
| { |
| 278 |
2 |
| const frx_node *n = frx_node_from_sv(aTHX_ sv, meth, doc_iv); |
| 279 |
2 |
| *doc = frx_doc_of_iv(aTHX_ *doc_iv, meth); |
| 280 |
2 |
| return n; |
| 281 |
|
| } |
| 282 |
|
| } |
| 283 |
|
| |
| 284 |
|
| /* one node-set entry as a mortal SV: a Node, an Attr or a Namespace */ |
| 285 |
|
| static SV * |
| 286 |
105 |
| frx_xp_item_sv(pTHX_ SV *doc_iv, const frx_xp_item *it) |
| 287 |
|
| { |
| 288 |
105 |
| switch (it->kind) { |
| 289 |
10 |
| case FRX_XP_KATTR: return frx_entry_bless(aTHX_ doc_iv, it->node, it->index, 0); |
| 290 |
6 |
| case FRX_XP_KNS: return frx_entry_bless(aTHX_ doc_iv, it->node, it->index, 1); |
| 291 |
89 |
| default: return frx_node_bless(aTHX_ doc_iv, it->node); |
| 292 |
|
| } |
| 293 |
|
| } |
| 294 |
|
| |
| 295 |
|
| /* Compile expr under the options HV: ns, vars, max_expr_depth. Croaks in |
| 296 |
|
| * the one message shape, with the offset counted in the expression. Every |
| 297 |
|
| * variable the expression names must be in `vars`, and it is named in the |
| 298 |
|
| * refusal when it is not. */ |
| 299 |
|
| static frx_xpath * |
| 300 |
234 |
| frx_xpath_from_opts(pTHX_ SV *expr_sv, HV *opts, const char *who) |
| 301 |
|
| { |
| 302 |
|
| STRLEN elen; |
| 303 |
234 |
| const char *expr = frx_input(aTHX_ expr_sv, &elen); |
| 304 |
|
| frx_ns_map map; |
| 305 |
234 |
| frx_ns_binding *bind = NULL; |
| 306 |
234 |
| HV *vars = NULL; |
| 307 |
234 |
| int max_depth = 0; |
| 308 |
|
| frx_xpath *x; |
| 309 |
234 |
| const char *err = NULL; |
| 310 |
234 |
| size_t at = 0; |
| 311 |
|
| int i, n_vars; |
| 312 |
|
| |
| 313 |
234 |
| map.v = NULL; |
| 314 |
234 |
| map.n = 0; |
| 315 |
|
| |
| 316 |
234 |
▶ | if (opts) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts) { |
234 |
0 |
234 |
|
| 317 |
|
| HE *he; |
| 318 |
234 |
| hv_iterinit(opts); |
| 319 |
426 |
▶ | while ((he = hv_iternext(opts))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(opts))) { |
193 |
233 |
426 |
|
| 320 |
|
| I32 klen; |
| 321 |
193 |
| const char *k = hv_iterkey(he, &klen); |
| 322 |
193 |
| SV *v = hv_iterval(opts, he); |
| 323 |
193 |
▶ | if (klen == 2 && memcmp(k, "ns", 2) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (klen == 2 && memcmp(k, "ns", 2) == 0) { |
185 |
8 |
193 |
| if (klen == 2 && memcmp(k, "ns", 2) == 0) { |
185 |
0 |
185 |
|
| 324 |
|
| HV *nh; |
| 325 |
|
| HE *nhe; |
| 326 |
|
| I32 n; |
| 327 |
185 |
▶ | if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV) |
185 |
0 |
185 |
| if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV) |
0 |
185 |
185 |
|
| 328 |
0 |
| croak("File::Raw::XML: %s: ns must be a hashref of prefix => uri", who); |
| 329 |
185 |
| nh = (HV *)SvRV(v); |
| 330 |
185 |
| n = hv_iterinit(nh); |
| 331 |
185 |
▶ | if (!n) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) continue; |
0 |
185 |
185 |
|
| 332 |
185 |
| Newx(bind, n, frx_ns_binding); |
| 333 |
185 |
| SAVEFREEPV(bind); |
| 334 |
185 |
| i = 0; |
| 335 |
554 |
▶ | while ((nhe = hv_iternext(nh)) && i < (int)n) { |
Branch: 2 branches
| Branch | True | False | Total |
| while ((nhe = hv_iternext(nh)) && i < (int)n) { |
369 |
185 |
554 |
| while ((nhe = hv_iternext(nh)) && i < (int)n) { |
369 |
0 |
369 |
|
| 336 |
|
| I32 plen; |
| 337 |
369 |
| bind[i].prefix = hv_iterkey(nhe, &plen); |
| 338 |
369 |
| bind[i].uri = SvPV_nolen(hv_iterval(nh, nhe)); |
| 339 |
369 |
| i++; |
| 340 |
|
| } |
| 341 |
185 |
| map.v = bind; |
| 342 |
185 |
| map.n = i; |
| 343 |
8 |
▶ | } else if (klen == 4 && memcmp(k, "vars", 4) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (klen == 4 && memcmp(k, "vars", 4) == 0) { |
6 |
2 |
8 |
| } else if (klen == 4 && memcmp(k, "vars", 4) == 0) { |
5 |
1 |
6 |
|
| 344 |
5 |
▶ | if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV) |
5 |
0 |
5 |
| if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV) |
0 |
5 |
5 |
|
| 345 |
0 |
| croak("File::Raw::XML: %s: vars must be a hashref of name => value", who); |
| 346 |
5 |
| vars = (HV *)SvRV(v); |
| 347 |
3 |
▶ | } else if (klen == 14 && memcmp(k, "max_expr_depth", 14) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (klen == 14 && memcmp(k, "max_expr_depth", 14) == 0) { |
2 |
1 |
3 |
| } else if (klen == 14 && memcmp(k, "max_expr_depth", 14) == 0) { |
2 |
0 |
2 |
|
| 348 |
2 |
| max_depth = (int)SvIV(v); |
| 349 |
|
| } else { |
| 350 |
1 |
| croak("File::Raw::XML: %s: unknown option '%.*s'", who, (int)klen, k); |
| 351 |
|
| } |
| 352 |
|
| } |
| 353 |
|
| } |
| 354 |
|
| |
| 355 |
233 |
| x = frx_xpath_compile(expr, (size_t)elen, &map, max_depth, &err, &at); |
| 356 |
233 |
▶ | if (!x) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x) { |
17 |
216 |
233 |
|
| 357 |
|
| char msg[512]; |
| 358 |
17 |
| size_t n = frx_xpath_err_format(err, expr, (size_t)elen, at, msg, sizeof msg); |
| 359 |
17 |
| croak("%.*s", (int)n, msg); |
| 360 |
|
| } |
| 361 |
|
| |
| 362 |
216 |
| n_vars = frx_xpath_var_count(x); |
| 363 |
219 |
▶ | for (i = 0; i < n_vars; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n_vars; i++) { |
7 |
212 |
219 |
|
| 364 |
|
| STRLEN nlen; |
| 365 |
7 |
| const char *name = frx_xpath_var_name(x, i, &nlen); |
| 366 |
7 |
▶ | SV **slot = vars ? hv_fetch(vars, name, (I32)nlen, 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **slot = vars ? hv_fetch(vars, name, (I32)nlen, 0) : NULL; |
5 |
2 |
7 |
|
| 367 |
|
| SV *val; |
| 368 |
|
| /* The name is in the compiled expression's arena, so a message |
| 369 |
|
| * that quotes it has to be built before the expression is freed: |
| 370 |
|
| * croak reads its arguments after the free otherwise, which is a |
| 371 |
|
| * use-after-free ASan finds and a smoker reports as a bad |
| 372 |
|
| * message. Truncated at 200 bytes, which no NCName reaches. */ |
| 373 |
|
| char held[256]; |
| 374 |
7 |
▶ | int nheld = (int)(nlen > 200 ? 200 : nlen); |
Branch: 1 branch
| Branch | True | False | Total |
| int nheld = (int)(nlen > 200 ? 200 : nlen); |
7 |
0 |
7 |
|
| 375 |
7 |
▶ | if (!name) nheld = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!name) nheld = 0; |
0 |
7 |
7 |
|
| 376 |
7 |
▶ | else if (nheld) memcpy(held, name, (size_t)nheld); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (nheld) memcpy(held, name, (size_t)nheld); |
7 |
0 |
7 |
|
| 377 |
7 |
| held[nheld] = '\0'; |
| 378 |
7 |
▶ | if (!slot || !SvOK(*slot)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!slot || !SvOK(*slot)) { |
5 |
2 |
7 |
| if (!slot || !SvOK(*slot)) { |
0 |
5 |
5 |
|
| 379 |
2 |
| frx_xpath_free(x); |
| 380 |
2 |
| croak("File::Raw::XML: %s: the expression uses $%s and no value was given for it", |
| 381 |
|
| who, held); |
| 382 |
|
| } |
| 383 |
5 |
| val = *slot; |
| 384 |
5 |
▶ | if (SvROK(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(val)) { |
2 |
3 |
5 |
|
| 385 |
2 |
| frx_xpath_free(x); |
| 386 |
2 |
| croak("File::Raw::XML: %s: the value of $%s must be a string or a number", |
| 387 |
|
| who, held); |
| 388 |
|
| } |
| 389 |
3 |
▶ | if (!SvPOK(val) && (SvIOK(val) || SvNOK(val))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvPOK(val) && (SvIOK(val) || SvNOK(val))) { |
1 |
2 |
3 |
| if (!SvPOK(val) && (SvIOK(val) || SvNOK(val))) { |
0 |
1 |
1 |
| if (!SvPOK(val) && (SvIOK(val) || SvNOK(val))) { |
0 |
0 |
0 |
|
| 390 |
1 |
| frx_xpath_bind_num(x, i, (double)SvNV(val)); |
| 391 |
|
| } else { |
| 392 |
|
| STRLEN vlen; |
| 393 |
2 |
| const char *vp = frx_input(aTHX_ val, &vlen); |
| 394 |
2 |
▶ | if (!frx_xpath_bind_str(x, i, vp, (size_t)vlen)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_xpath_bind_str(x, i, vp, (size_t)vlen)) { |
0 |
2 |
2 |
|
| 395 |
0 |
| frx_xpath_free(x); |
| 396 |
0 |
| croak("File::Raw::XML: %s: out of memory", who); |
| 397 |
|
| } |
| 398 |
|
| } |
| 399 |
|
| } |
| 400 |
212 |
| return x; |
| 401 |
|
| } |
| 402 |
|
| |
| 403 |
|
| /* The result as a mortal AV the XSUB pushes from: a node-set is every |
| 404 |
|
| * entry in document order, or just the first in scalar context, and each |
| 405 |
|
| * of the other three types is one scalar. The value is freed here, so |
| 406 |
|
| * every SV in the AV owns its own copy of what it holds. */ |
| 407 |
|
| static AV * |
| 408 |
213 |
| frx_xp_result_av(pTHX_ frx_xp_val *v, SV *doc_iv, I32 gimme) |
| 409 |
|
| { |
| 410 |
213 |
| AV *av = (AV *)sv_2mortal((SV *)newAV()); |
| 411 |
|
| int i; |
| 412 |
|
| |
| 413 |
213 |
| switch (v->kind) { |
| 414 |
65 |
| case FRX_XV_NODESET: |
| 415 |
65 |
▶ | if (gimme == G_LIST) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gimme == G_LIST) { |
59 |
6 |
65 |
|
| 416 |
159 |
▶ | for (i = 0; i < v->set.n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < v->set.n; i++) |
100 |
59 |
159 |
|
| 417 |
100 |
| av_push(av, frx_xp_item_sv(aTHX_ doc_iv, &v->set.v[i])); |
| 418 |
|
| } else { |
| 419 |
6 |
▶ | av_push(av, v->set.n ? frx_xp_item_sv(aTHX_ doc_iv, &v->set.v[0]) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(av, v->set.n ? frx_xp_item_sv(aTHX_ doc_iv, &v->set.v[0]) : newSV(0)); |
5 |
1 |
6 |
|
| 420 |
|
| } |
| 421 |
65 |
| break; |
| 422 |
1 |
| case FRX_XV_BOOLEAN: |
| 423 |
1 |
| av_push(av, newSViv(v->bl ? 1 : 0)); |
| 424 |
1 |
| break; |
| 425 |
11 |
| case FRX_XV_NUMBER: |
| 426 |
11 |
| av_push(av, newSVnv(v->num)); |
| 427 |
11 |
| break; |
| 428 |
136 |
| default: { |
| 429 |
136 |
▶ | SV *s = newSVpvn(v->str.p ? v->str.p : "", v->str.len); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *s = newSVpvn(v->str.p ? v->str.p : "", v->str.len); |
136 |
0 |
136 |
|
| 430 |
136 |
| SvUTF8_on(s); |
| 431 |
136 |
| av_push(av, s); |
| 432 |
136 |
| break; |
| 433 |
|
| } |
| 434 |
|
| } |
| 435 |
213 |
| return av; |
| 436 |
|
| } |
| 437 |
|
| |
| 438 |
|
| MODULE = File::Raw::XML PACKAGE = File::Raw::XML |
| 439 |
|
| |
| 440 |
|
| PROTOTYPES: DISABLE |
| 441 |
|
| |
| 442 |
|
| BOOT: |
| 443 |
|
| /* the `xml` plugin, against File::Raw's registry; Raw.so is resident |
| 444 |
|
| * because XML.pm says `use File::Raw` before XSLoader::load */ |
| 445 |
37 |
| frx_plugin_register(aTHX); |
| 446 |
|
| |
| 447 |
|
| INCLUDE: xs/internal.xs |
| 448 |
|
| INCLUDE: xs/codec.xs |
| 449 |
|
| INCLUDE: xs/document.xs |
| 450 |
|
| INCLUDE: xs/node.xs |
| 451 |
|
| INCLUDE: xs/reader.xs |
| 452 |
|
| INCLUDE: xs/xpath.xs |
| 453 |
|
| INCLUDE: xs/abi.xs |