include/frx/frx_obj.h

filter: Statementstmt 97.6% 418 / 428 Branchbran 73.3% 342 / 466 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.0% 760 / 894 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/internal.xs ↑ Summary XML.xs »
1 #ifndef FRX_OBJ_H
2 #define FRX_OBJ_H
3  
4 /* The SV layer: blessing, the owner reference, the invocant checks, the
5 * options, and strings across the seam.
6 *
7 * THE DOCUMENT is a blessed IV whose referent carries PERL_MAGIC_ext with
8 * the frx_doc * in mg_ptr and a vtable whose free hook calls frx_doc_free.
9 * Scope exit, undef and global destruction are all covered with no Perl
10 * DESTROY, the way Markdown::Simple holds its session.
11 *
12 * THE NODE is a blessed IV whose referent carries PERL_MAGIC_ext with the
13 * frx_node * in mg_ptr AND THE DOCUMENT'S INNER IV AS mg_obj. This is the
14 * owner reference, and it is the pattern no other dist in the family has:
15 *
16 * sv_magicext(iv, doc_iv, PERL_MAGIC_ext, &frx_node_vtbl, (const char *)node, 0);
17 *
18 * sv_magicext with a non-NULL obj that is not the SV itself increments
19 * the obj's reference count and sets MGf_REFCOUNTED, and mg_free
20 * decrements it when the node's referent is freed. So a node holds its
21 * document at +1 with no DESTROY, no AV to carry, and a free order the
22 * reference counts enforce: the document cannot be freed while a node is
23 * alive, because the node is what keeps it alive. $node->doc is
24 * newRV_inc(mg->mg_obj): the referent is what sv_bless blessed, so the RV
25 * is the same object. PDF::Make blesses child nodes into a tree with
26 * nothing keeping the tree alive, and that is the use after free this
27 * exists to make impossible.
28 *
29 * ithreads: the magic is not marked MGf_DUP, so a cloned interpreter's
30 * copy of a document or node is an IV with no magic, and every method on
31 * it croaks "not a File::Raw::XML::Document". The objects are not
32 * shareable across threads, and the POD says so.
33 *
34 * STRINGS: names, attribute values and text cross the seam as character
35 * strings, SvUTF8 on, because they are validated UTF-8 and a Perl caller
36 * expects characters. Canonical output crosses as bytes, flag off,
37 * because a signature is over bytes, not characters. Input is
38 * taken as the SV's UTF-8 bytes when flagged and its raw bytes otherwise;
39 * the lexer refuses anything that is not UTF-8, with an offset.
40 *
41 * Needs the perl headers, frx_abi.h, frx_tree.h, frx_parse.h. */
42  
43 #define FRX_DOC_CLASS "File::Raw::XML::Document"
44 #define FRX_NODE_CLASS "File::Raw::XML::Node"
45  
46 static int
47 767719 frx_doc_mg_free(pTHX_ SV *sv, MAGIC *mg)
48 {
49 767719 frx_doc *d = (frx_doc *)mg->mg_ptr;
50 PERL_UNUSED_ARG(sv);
51 767719 ▶ if (d) {
Branch: 1 branch
BranchTrueFalseTotal
if (d) { 767719 0 767719
52 767719 frx_doc_free(d);
53 767719 mg->mg_ptr = NULL;
54 }
55 767719 return 0;
56 }
57  
58 static MGVTBL frx_doc_vtbl = { NULL, NULL, NULL, NULL, frx_doc_mg_free, NULL, NULL, NULL };
59 /* a node frees nothing of its own; mg_free releases mg_obj, the document */
60 static MGVTBL frx_node_vtbl = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
61  
62 /* the document, +1, blessed */
63 static SV *
64 767719 frx_doc_bless(pTHX_ frx_doc *d)
65 {
66 767719 SV *iv = newSViv(0);
67 767719 sv_magicext(iv, NULL, PERL_MAGIC_ext, &frx_doc_vtbl, (const char *)d, 0);
68 767719 return sv_bless(newRV_noinc(iv), gv_stashpv(FRX_DOC_CLASS, GV_ADD));
69 }
70  
71 /* a node of the document whose inner IV is doc_iv, +1, blessed */
72 static SV *
73 863824 frx_node_bless(pTHX_ SV *doc_iv, const frx_node *n)
74 {
75 863824 SV *iv = newSViv(0);
76 863824 sv_magicext(iv, doc_iv, PERL_MAGIC_ext, &frx_node_vtbl, (const char *)n, 0);
77 863824 return sv_bless(newRV_noinc(iv), gv_stashpv(FRX_NODE_CLASS, GV_ADD));
78 }
79  
80 static MAGIC *
81 2021501 frx_obj_magic(pTHX_ SV *sv, const MGVTBL *vtbl)
82 {
83 2021501 ▶ if (!sv || !SvROK(sv)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv)) return NULL; 2021501 0 2021501
if (!sv || !SvROK(sv)) return NULL; 3 2021498 2021501
84 2021498 sv = SvRV(sv);
85 2021498 ▶ if (!SvOBJECT(sv) || SvTYPE(sv) >= SVt_PVAV) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT(sv) || SvTYPE(sv) >= SVt_PVAV) return NULL; 2021498 0 2021498
if (!SvOBJECT(sv) || SvTYPE(sv) >= SVt_PVAV) return NULL; 1 2021497 2021498
86 2021497 return mg_findext(sv, PERL_MAGIC_ext, vtbl);
87 }
88  
89 /* the frx_doc behind a Document invocant; croaks naming the method */
90 static frx_doc *
91 771051 frx_doc_from_sv(pTHX_ SV *sv, const char *meth)
92 {
93 771051 MAGIC *mg = frx_obj_magic(aTHX_ sv, &frx_doc_vtbl);
94 771051 ▶ if (!mg || !mg->mg_ptr)
Branch: 2 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr) 771048 3 771051
if (!mg || !mg->mg_ptr) 0 771048 771048
95 3 croak("File::Raw::XML: %s: the invocant is not a " FRX_DOC_CLASS, meth);
96 771048 return (frx_doc *)mg->mg_ptr;
97 }
98  
99 /* the frx_node behind a Node invocant, and its document's inner IV */
100 static const frx_node *
101 763290 frx_node_from_sv(pTHX_ SV *sv, const char *meth, SV **doc_iv)
102 {
103 763290 MAGIC *mg = frx_obj_magic(aTHX_ sv, &frx_node_vtbl);
104 763290 ▶ if (!mg || !mg->mg_ptr || !mg->mg_obj)
Branch: 3 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr || !mg->mg_obj) 763286 4 763290
if (!mg || !mg->mg_ptr || !mg->mg_obj) 763286 0 763286
if (!mg || !mg->mg_ptr || !mg->mg_obj) 0 763286 763286
105 4 croak("File::Raw::XML: %s: the invocant is not a " FRX_NODE_CLASS, meth);
106 763286 ▶ if (doc_iv) *doc_iv = mg->mg_obj;
Branch: 1 branch
BranchTrueFalseTotal
if (doc_iv) *doc_iv = mg->mg_obj; 703 762583 763286
107 763286 return (const frx_node *)mg->mg_ptr;
108 }
109  
110 /* the document behind a node, as a +1 blessed RV */
111 static SV *
112 8 frx_node_doc_rv(pTHX_ SV *doc_iv)
113 {
114 8 return newRV_inc(doc_iv);
115 }
116  
117 /* a string across the seam: characters */
118 static SV *
119 864017 frx_str_sv(pTHX_ const frx_str *s)
120 {
121 864017 ▶ SV *sv = newSVpvn(s->p ? s->p : "", s->len);
Branch: 1 branch
BranchTrueFalseTotal
SV *sv = newSVpvn(s->p ? s->p : "", s->len); 864017 0 864017
122 864017 SvUTF8_on(sv);
123 864017 return sv;
124 }
125  
126 /* the bytes of an input SV: UTF-8 when flagged, raw otherwise; the lexer
127 * refuses what is not UTF-8 */
128 static const char *
129 30145 frx_input(pTHX_ SV *sv, STRLEN *len)
130 {
131 30151 ▶ if (SvUTF8(sv)) return SvPVutf8(sv, *len);
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(sv)) return SvPVutf8(sv, *len); 6 30139 30145
132 30139 return SvPV(sv, *len);
133 }
134  
135 /* the tail of an XSUB's arguments as an options HV, mortal; odd croaks */
136 static HV *
137 33517 frx_opts_hv(pTHX_ SV **st, I32 first, I32 items, const char *who)
138 {
139 HV *hv;
140 I32 i;
141 33517 ▶ if (((items - first) & 1) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (((items - first) & 1) != 0) 2 33515 33517
142 2 croak("File::Raw::XML: %s: options must be key/value pairs", who);
143 33515 hv = (HV *)sv_2mortal((SV *)newHV());
144 80012 ▶ for (i = first; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = first; i < items; i += 2) { 46497 33515 80012
145 STRLEN klen;
146 46497 const char *k = SvPV(st[i], klen);
147 46497 (void)hv_store(hv, k, (I32)klen, newSVsv(st[i + 1]), 0);
148 }
149 33515 return hv;
150 }
151  
152 /* ---- resolvers ---------------------------------------------------------
153 *
154 * The core fetches nothing: an external subset or entity reaches it as
155 * bytes only through frx_opts_ex.resolve, a C function the caller
156 * supplies. Two live here. The coderef resolver calls a Perl sub with
157 * kind, public_id, system_id and base and takes the string it returns as
158 * the bytes; a die inside it becomes the refusal, with the message, at
159 * the reference's offset, and undef is a refusal too. The file resolver
160 * is confined to the document's own directory: the system identifier
161 * (already resolved against the document's path) must have no scheme or
162 * the file scheme, must be a regular file, and its real path, symbolic
163 * links followed, must lie under the real path of the document's
164 * directory. It exists only for file_slurp, which has a path; the codec
165 * has none and refuses it. */
166  
167 typedef struct frx_resolver_ud {
168 #ifdef PERL_IMPLICIT_CONTEXT
169 PerlInterpreter *my_perl;
170 #endif
171 SV *cv; /* the coderef; NULL for the file resolver */
172 char *dir; /* the document's directory, real path, malloc'd; file resolver */
173 size_t dir_len;
174 char msg[512]; /* a refusal message built here; static for the parse's life */
175 } frx_resolver_ud;
176  
177 typedef struct frx_fetch_holder {
178 frx_resolver_ud *ud;
179 SV *sv; /* the bytes, +1 */
180 } frx_fetch_holder;
181  
182 static void
183 1585 frx_perl_release(void *vh, frx_fetched *f)
184 {
185 1585 frx_fetch_holder *h = (frx_fetch_holder *)vh;
186 #ifdef PERL_IMPLICIT_CONTEXT
187 dTHXa(h->ud->my_perl);
188 #endif
189 PERL_UNUSED_ARG(f);
190 1585 SvREFCNT_dec(h->sv);
191 1585 free(h);
192 1585 }
193  
194 static const char *
195 1614 frx_kind_name(int kind)
196 {
197 1614 switch (kind) {
198 1031 case FRX_REF_SUBSET: return "subset";
199 154 case FRX_REF_XINCLUDE: return "xinclude";
200 429 default: return "entity";
201 }
202 }
203  
204 /* a C string as a character SV, or undef for NULL */
205 static SV *
206 4842 frx_cstr_sv(pTHX_ const char *p)
207 {
208 SV *sv;
209 4842 ▶ if (!p) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return newSV(0); 1781 3061 4842
210 3061 sv = newSVpv(p, 0);
211 3061 SvUTF8_on(sv);
212 3061 return sv;
213 }
214  
215 static const char *
216 1614 frx_perl_resolve(void *vud, const char *pub, const char *sys, const char *base,
217 int kind, frx_fetched *out)
218 {
219 1614 frx_resolver_ud *ud = (frx_resolver_ud *)vud;
220 #ifdef PERL_IMPLICIT_CONTEXT
221 dTHXa(ud->my_perl);
222 #endif
223 1614 dSP;
224 int count;
225 1614 SV *ret = NULL, *keep = NULL;
226 1614 const char *result = NULL;
227  
228 1614 ENTER;
229 1614 SAVETMPS;
230 1614 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1614 1614
231 1614 ▶ mXPUSHs(newSVpvs("kind")); mXPUSHs(newSVpv(frx_kind_name(kind), 0));
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("kind")); mXPUSHs(newSVpv(frx_kind_name(kind), 0)); 0 1614 1614
mXPUSHs(newSVpvs("kind")); mXPUSHs(newSVpv(frx_kind_name(kind), 0)); 0 1614 1614
232 1614 ▶ mXPUSHs(newSVpvs("public_id")); mXPUSHs(frx_cstr_sv(aTHX_ pub));
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("public_id")); mXPUSHs(frx_cstr_sv(aTHX_ pub)); 0 1614 1614
mXPUSHs(newSVpvs("public_id")); mXPUSHs(frx_cstr_sv(aTHX_ pub)); 0 1614 1614
233 1614 ▶ mXPUSHs(newSVpvs("system_id")); mXPUSHs(frx_cstr_sv(aTHX_ sys));
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("system_id")); mXPUSHs(frx_cstr_sv(aTHX_ sys)); 0 1614 1614
mXPUSHs(newSVpvs("system_id")); mXPUSHs(frx_cstr_sv(aTHX_ sys)); 0 1614 1614
234 1614 ▶ mXPUSHs(newSVpvs("base")); mXPUSHs(frx_cstr_sv(aTHX_ base));
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHs(newSVpvs("base")); mXPUSHs(frx_cstr_sv(aTHX_ base)); 0 1614 1614
mXPUSHs(newSVpvs("base")); mXPUSHs(frx_cstr_sv(aTHX_ base)); 0 1614 1614
235 1614 PUTBACK;
236 1614 count = call_sv(ud->cv, G_SCALAR | G_EVAL);
237 1614 SPAGAIN;
238 1614 ▶ if (count) ret = POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count) ret = POPs; 1614 0 1614
239 1614 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 1614 0 1614
if (SvTRUE(ERRSV)) { 28 1586 1614
240 STRLEN n;
241 28 ▶ const char *m = SvPV(ERRSV, n);
Branch: 1 branch
BranchTrueFalseTotal
const char *m = SvPV(ERRSV, n); 28 0 28
242 56 ▶ while (n && (m[n - 1] == '\n' || m[n - 1] == '\r')) n--;
Branch: 3 branches
BranchTrueFalseTotal
while (n && (m[n - 1] == '\n' || m[n - 1] == '\r')) n--; 56 0 56
while (n && (m[n - 1] == '\n' || m[n - 1] == '\r')) n--; 28 28 56
while (n && (m[n - 1] == '\n' || m[n - 1] == '\r')) n--; 0 28 28
243 28 ▶ my_snprintf(ud->msg, sizeof ud->msg, "the resolver died: %.*s", (int)(n > 400 ? 400 : n), m);
Branch: 1 branch
BranchTrueFalseTotal
my_snprintf(ud->msg, sizeof ud->msg, "the resolver died: %.*s", (int)(n > 400 ? 400 : n), m); 28 0 28
244 28 result = ud->msg;
245 1586 ▶ } else if (!ret || !SvOK(ret)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (!ret || !SvOK(ret)) { 1586 0 1586
} else if (!ret || !SvOK(ret)) { 1 1585 1586
246 1 my_snprintf(ud->msg, sizeof ud->msg, "the resolver returned undef");
247 1 result = ud->msg;
248 } else {
249 1585 keep = newSVsv(ret);
250 }
251 1614 PUTBACK;
252 1614 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1614 0 1614
253 1614 LEAVE;
254 1614 ▶ if (result) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (result) return result; 29 1585 1614
255 {
256 1585 frx_fetch_holder *h = (frx_fetch_holder *)malloc(sizeof *h);
257 STRLEN n;
258 1585 ▶ if (!h) { SvREFCNT_dec(keep); return "out of memory"; }
Branch: 1 branch
BranchTrueFalseTotal
if (!h) { SvREFCNT_dec(keep); return "out of memory"; } 0 1585 1585
259 1585 h->ud = ud;
260 1585 h->sv = keep;
261 3170 ▶ out->bytes = SvUTF8(keep) ? SvPVutf8(keep, n) : SvPV(keep, n);
Branch: 1 branch
BranchTrueFalseTotal
out->bytes = SvUTF8(keep) ? SvPVutf8(keep, n) : SvPV(keep, n); 0 1585 1585
262 1585 out->len = (size_t)n;
263 1585 out->release = frx_perl_release;
264 1585 out->ud = h;
265 }
266 1585 return NULL;
267 }
268  
269 static void
270 3 frx_file_release(void *vp, frx_fetched *f)
271 {
272 PERL_UNUSED_ARG(f);
273 3 free(vp);
274 3 }
275  
276 #ifndef PATH_MAX
277 #define PATH_MAX 4096
278 #endif
279  
280 /* the real, absolute path of p into a malloc'd string, or NULL */
281 static char *
282 28 frx_realpath(const char *p)
283 {
284 #ifdef _WIN32
285 char *buf = (char *)malloc(4096);
286 if (!buf) return NULL;
287 if (!_fullpath(buf, p, 4096)) { free(buf); return NULL; }
288 return buf;
289 #else
290 28 char *buf = (char *)malloc(PATH_MAX + 1);
291 28 ▶ if (!buf) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) return NULL; 0 28 28
292 28 ▶ if (!realpath(p, buf)) { free(buf); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!realpath(p, buf)) { free(buf); return NULL; } 2 26 28
293 26 return buf;
294 #endif
295 }
296  
297 static int
298 89 frx_is_dir_sep(char c)
299 {
300 #ifdef _WIN32
301 return c == '/' || c == '\\';
302 #else
303 89 return c == '/';
304 #endif
305 }
306  
307 static const char *
308 10 frx_file_resolve(void *vud, const char *pub, const char *sys, const char *base,
309 int kind, frx_fetched *out)
310 {
311 10 frx_resolver_ud *ud = (frx_resolver_ud *)vud;
312 #ifdef PERL_IMPLICIT_CONTEXT
313 dTHXa(ud->my_perl);
314 #endif
315 const char *path;
316 char *real;
317 PerlIO *fp;
318 Stat_t st;
319 char *bytes;
320 size_t n;
321 PERL_UNUSED_ARG(pub);
322 PERL_UNUSED_ARG(base);
323 PERL_UNUSED_ARG(kind);
324  
325 10 ▶ if (!sys || !*sys) return "the reference has no system identifier to read";
Branch: 2 branches
BranchTrueFalseTotal
if (!sys || !*sys) return "the reference has no system identifier to read"; 10 0 10
if (!sys || !*sys) return "the reference has no system identifier to read"; 0 10 10
326 10 path = sys;
327 10 ▶ if ((sys[0] == 'f' || sys[0] == 'F') && strlen(sys) > 5 && (sys[1] | 0x20) == 'i' && (sys[2] | 0x20) == 'l'
Branch: 5 branches
BranchTrueFalseTotal
if ((sys[0] == 'f' || sys[0] == 'F') && strlen(sys) > 5 && (sys[1] | 0x20) == 'i' && (sys[2] | 0x20) == 'l' 8 2 10
if ((sys[0] == 'f' || sys[0] == 'F') && strlen(sys) > 5 && (sys[1] | 0x20) == 'i' && (sys[2] | 0x20) == 'l' 0 8 8
if ((sys[0] == 'f' || sys[0] == 'F') && strlen(sys) > 5 && (sys[1] | 0x20) == 'i' && (sys[2] | 0x20) == 'l' 2 0 2
if ((sys[0] == 'f' || sys[0] == 'F') && strlen(sys) > 5 && (sys[1] | 0x20) == 'i' && (sys[2] | 0x20) == 'l' 2 0 2
if ((sys[0] == 'f' || sys[0] == 'F') && strlen(sys) > 5 && (sys[1] | 0x20) == 'i' && (sys[2] | 0x20) == 'l' 2 0 2
328 2 ▶ && (sys[3] | 0x20) == 'e' && sys[4] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
&& (sys[3] | 0x20) == 'e' && sys[4] == ':') { 2 0 2
&& (sys[3] | 0x20) == 'e' && sys[4] == ':') { 2 0 2
329 2 path = sys + 5;
330 2 ▶ if (path[0] == '/' && path[1] == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (path[0] == '/' && path[1] == '/') { 2 0 2
if (path[0] == '/' && path[1] == '/') { 2 0 2
331 2 path += 2; /* file://host/path: the host must be empty */
332 2 ▶ if (*path != '/') return "a file: identifier with a host is not read";
Branch: 1 branch
BranchTrueFalseTotal
if (*path != '/') return "a file: identifier with a host is not read"; 0 2 2
333 }
334 } else {
335 8 const char *q = sys;
336 20 ▶ while ((*q >= 'a' && *q <= 'z') || (*q >= 'A' && *q <= 'Z') || (*q >= '0' && *q <= '9')
Branch: 5 branches
BranchTrueFalseTotal
while ((*q >= 'a' && *q <= 'z') || (*q >= 'A' && *q <= 'Z') || (*q >= '0' && *q <= '9') 4 0 4
while ((*q >= 'a' && *q <= 'z') || (*q >= 'A' && *q <= 'Z') || (*q >= '0' && *q <= '9') 0 8 8
while ((*q >= 'a' && *q <= 'z') || (*q >= 'A' && *q <= 'Z') || (*q >= '0' && *q <= '9') 0 0 0
while ((*q >= 'a' && *q <= 'z') || (*q >= 'A' && *q <= 'Z') || (*q >= '0' && *q <= '9') 1 7 8
while ((*q >= 'a' && *q <= 'z') || (*q >= 'A' && *q <= 'Z') || (*q >= '0' && *q <= '9') 0 1 1
337 20 ▶ || *q == '+' || *q == '-' || *q == '.') q++;
Branch: 4 branches
BranchTrueFalseTotal
|| *q == '+' || *q == '-' || *q == '.') q++; 4 8 12
|| *q == '+' || *q == '-' || *q == '.') q++; 0 8 8
|| *q == '+' || *q == '-' || *q == '.') q++; 0 8 8
|| *q == '+' || *q == '-' || *q == '.') q++; 0 8 8
338 8 ▶ if (q > sys && *q == ':' && q[1] == '/' && q[2] == '/')
Branch: 4 branches
BranchTrueFalseTotal
if (q > sys && *q == ':' && q[1] == '/' && q[2] == '/') 1 7 8
if (q > sys && *q == ':' && q[1] == '/' && q[2] == '/') 1 0 1
if (q > sys && *q == ':' && q[1] == '/' && q[2] == '/') 1 0 1
if (q > sys && *q == ':' && q[1] == '/' && q[2] == '/') 1 0 1
339 1 return "only a file path or a file: identifier is read by the file resolver";
340 }
341 9 real = frx_realpath(path);
342 9 ▶ if (!real) return "the system identifier does not name a readable file";
Branch: 1 branch
BranchTrueFalseTotal
if (!real) return "the system identifier does not name a readable file"; 2 7 9
343 7 ▶ if (strncmp(real, ud->dir, ud->dir_len) != 0 || !frx_is_dir_sep(real[ud->dir_len])) {
Branch: 2 branches
BranchTrueFalseTotal
if (strncmp(real, ud->dir, ud->dir_len) != 0 || !frx_is_dir_sep(real[ud->dir_len])) { 4 3 7
if (strncmp(real, ud->dir, ud->dir_len) != 0 || !frx_is_dir_sep(real[ud->dir_len])) { 0 4 4
344 3 free(real);
345 3 return "the system identifier escapes the document's directory";
346 }
347 4 ▶ if (PerlLIO_stat(real, &st) != 0 || !S_ISREG(st.st_mode)) {
Branch: 2 branches
BranchTrueFalseTotal
if (PerlLIO_stat(real, &st) != 0 || !S_ISREG(st.st_mode)) { 4 0 4
if (PerlLIO_stat(real, &st) != 0 || !S_ISREG(st.st_mode)) { 1 3 4
348 1 free(real);
349 1 return "the system identifier is not a regular file";
350 }
351 3 fp = PerlIO_open(real, "rb");
352 3 free(real);
353 3 ▶ if (!fp) return "the system identifier cannot be opened";
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) return "the system identifier cannot be opened"; 0 3 3
354 3 n = (size_t)st.st_size;
355 3 ▶ bytes = (char *)malloc(n ? n : 1);
Branch: 1 branch
BranchTrueFalseTotal
bytes = (char *)malloc(n ? n : 1); 3 0 3
356 3 ▶ if (!bytes) { PerlIO_close(fp); return "out of memory"; }
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) { PerlIO_close(fp); return "out of memory"; } 0 3 3
357 {
358 3 size_t got = 0;
359 6 ▶ while (got < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (got < n) { 3 3 6
360 3 SSize_t r = PerlIO_read(fp, bytes + got, n - got);
361 3 ▶ if (r <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (r <= 0) break; 0 3 3
362 3 got += (size_t)r;
363 }
364 3 n = got;
365 }
366 3 PerlIO_close(fp);
367 3 out->bytes = bytes;
368 3 out->len = n;
369 3 out->release = frx_file_release;
370 3 out->ud = bytes;
371 3 return NULL;
372 }
373  
374 /* the directory of path as a real path, for the file resolver */
375 static char *
376 10 frx_dir_of(const char *path)
377 {
378 10 char *real = frx_realpath(path);
379 size_t n;
380 10 ▶ if (!real) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!real) return NULL; 0 10 10
381 10 n = strlen(real);
382 85 ▶ while (n && !frx_is_dir_sep(real[n - 1])) n--;
Branch: 2 branches
BranchTrueFalseTotal
while (n && !frx_is_dir_sep(real[n - 1])) n--; 85 0 85
while (n && !frx_is_dir_sep(real[n - 1])) n--; 75 10 85
383 10 ▶ if (n > 1) n--; /* drop the separator, keep a root */
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) n--; /* drop the separator, keep a root */ 10 0 10
384 10 real[n] = '\0';
385 10 return real;
386 }
387  
388 /* frx_opts_ex from an options HV. Unknown keys croak; `plugin` is known
389 * because file_plugin_build_opts puts it there. The id_attrs names are
390 * freed with the current pseudo-block, so parse before it ends. `profile`
391 * is 'strict' (the default) or 'full'; anything else croaks naming both.
392 * ud receives a resolver's state; path is the document's file when there
393 * is one (the plugin) and NULL for the codec. */
394 static void
395 28863 frx_opts_from_hv(pTHX_ HV *hv, frx_opts_ex *oe, frx_resolver_ud *ud, const char *path)
396 {
397 HE *he;
398 28863 frx_opts *o = &oe->base;
399 28863 memset(oe, 0, sizeof *oe);
400 28863 oe->size = sizeof *oe;
401 28863 oe->profile = FRX_PROFILE_STRICT;
402 28863 memset(ud, 0, sizeof *ud);
403 #ifdef PERL_IMPLICIT_CONTEXT
404 ud->my_perl = aTHX;
405 #endif
406 28863 ▶ if (!hv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hv) return; 0 28863 28863
407 28863 hv_iterinit(hv);
408 72858 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 44010 28848 72858
409 I32 klen;
410 44010 const char *k = hv_iterkey(he, &klen);
411 44010 SV *v = hv_iterval(hv, he);
412 44017 ▶ if (klen == 6 && memcmp(k, "plugin", 6) == 0) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 6 && memcmp(k, "plugin", 6) == 0) continue; 33 43977 44010
if (klen == 6 && memcmp(k, "plugin", 6) == 0) continue; 28 5 33
413 43982 ▶ if (klen == 6 && memcmp(k, "record", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 6 && memcmp(k, "record", 6) == 0) { 5 43977 43982
if (klen == 6 && memcmp(k, "record", 6) == 0) { 5 0 5
414 /* the stream phase's own option, read by frx_plugin.h */
415 5 ▶ if (!path) croak("File::Raw::XML: record is an option of the xml plugin's stream phase; use File::Raw::each_line");
Branch: 1 branch
BranchTrueFalseTotal
if (!path) croak("File::Raw::XML: record is an option of the xml plugin's stream phase; use File::Raw::each_line"); 1 4 5
416 4 continue;
417 }
418 50624 ▶ if (klen == 7 && memcmp(k, "profile", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 7 && memcmp(k, "profile", 7) == 0) { 12150 31827 43977
if (klen == 7 && memcmp(k, "profile", 7) == 0) { 6651 5499 12150
419 STRLEN n;
420 6651 const char *s = SvPV(v, n);
421 6651 ▶ if (n == 6 && memcmp(s, "strict", 6) == 0) oe->profile = FRX_PROFILE_STRICT;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 6 && memcmp(s, "strict", 6) == 0) oe->profile = FRX_PROFILE_STRICT; 1 6650 6651
if (n == 6 && memcmp(s, "strict", 6) == 0) oe->profile = FRX_PROFILE_STRICT; 1 0 1
422 6650 ▶ else if (n == 4 && memcmp(s, "full", 4) == 0) oe->profile = FRX_PROFILE_FULL;
Branch: 2 branches
BranchTrueFalseTotal
else if (n == 4 && memcmp(s, "full", 4) == 0) oe->profile = FRX_PROFILE_FULL; 6646 4 6650
else if (n == 4 && memcmp(s, "full", 4) == 0) oe->profile = FRX_PROFILE_FULL; 6646 0 6646
423 4 else croak("File::Raw::XML: profile must be 'strict' or 'full', not '%.*s'", (int)n, s);
424 37326 ▶ } else if (klen == 8 && memcmp(k, "encoding", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 8 && memcmp(k, "encoding", 8) == 0) { 26357 10969 37326
} else if (klen == 8 && memcmp(k, "encoding", 8) == 0) { 8 26349 26357
425 /* the string lives in the options HV, which outlives the parse */
426 8 oe->encoding = SvPV_nolen(v);
427 37318 ▶ } else if (klen == 9 && memcmp(k, "max_bytes", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 9 && memcmp(k, "max_bytes", 9) == 0) { 10 37308 37318
} else if (klen == 9 && memcmp(k, "max_bytes", 9) == 0) { 7 3 10
428 7 o->max_bytes = (size_t)SvUV(v);
429 37311 ▶ } else if (klen == 9 && memcmp(k, "max_depth", 9) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 9 && memcmp(k, "max_depth", 9) == 0) { 3 37308 37311
} else if (klen == 9 && memcmp(k, "max_depth", 9) == 0) { 3 0 3
430 3 o->max_depth = (int)SvIV(v);
431 37308 ▶ } else if (klen == 16 && memcmp(k, "max_entity_depth", 16) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 16 && memcmp(k, "max_entity_depth", 16) == 0) { 5 37303 37308
} else if (klen == 16 && memcmp(k, "max_entity_depth", 16) == 0) { 5 0 5
432 5 oe->max_entity_depth = (int)SvIV(v);
433 37303 ▶ } else if (klen == 19 && memcmp(k, "max_expansion_bytes", 19) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 19 && memcmp(k, "max_expansion_bytes", 19) == 0) { 10 37293 37303
} else if (klen == 19 && memcmp(k, "max_expansion_bytes", 19) == 0) { 5 5 10
434 5 oe->max_expansion_bytes = (size_t)SvUV(v);
435 37298 ▶ } else if (klen == 19 && memcmp(k, "max_expansion_ratio", 19) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 19 && memcmp(k, "max_expansion_ratio", 19) == 0) { 5 37293 37298
} else if (klen == 19 && memcmp(k, "max_expansion_ratio", 19) == 0) { 5 0 5
436 5 oe->max_expansion_ratio = (int)SvIV(v);
437 37293 ▶ } else if (klen == 8 && memcmp(k, "validate", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 8 && memcmp(k, "validate", 8) == 0) { 26349 10944 37293
} else if (klen == 8 && memcmp(k, "validate", 8) == 0) { 2207 24142 26349
438 /* 1 stops at the first violation, 'collect' gathers them all */
439 4302 ▶ if (SvPOK(v) && !SvIOK(v) && !SvNOK(v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvPOK(v) && !SvIOK(v) && !SvNOK(v)) { 2096 111 2207
if (SvPOK(v) && !SvIOK(v) && !SvNOK(v)) { 2096 0 2096
if (SvPOK(v) && !SvIOK(v) && !SvNOK(v)) { 2096 0 2096
440 STRLEN n;
441 2096 const char *s = SvPV(v, n);
442 2096 ▶ if (n == 7 && memcmp(s, "collect", 7) == 0) oe->validate = 2;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 7 && memcmp(s, "collect", 7) == 0) oe->validate = 2; 2095 1 2096
if (n == 7 && memcmp(s, "collect", 7) == 0) oe->validate = 2; 2095 0 2095
443 1 ▶ else if (!n) oe->validate = 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (!n) oe->validate = 0; 0 1 1
444 1 else croak("File::Raw::XML: validate must be 1, 0 or 'collect', not '%.*s'", (int)n, s);
445 } else {
446 111 oe->validate = SvTRUE(v) ? 1 : 0;
447 }
448 35086 ▶ } else if (klen == 15 && memcmp(k, "max_token_bytes", 15) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 15 && memcmp(k, "max_token_bytes", 15) == 0) { 2 35084 35086
} else if (klen == 15 && memcmp(k, "max_token_bytes", 15) == 0) { 2 0 2
449 2 oe->max_token_bytes = (size_t)SvUV(v); /* a reader's bound; both profiles */
450 35084 ▶ } else if (klen == 11 && memcmp(k, "max_fetches", 11) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 11 && memcmp(k, "max_fetches", 11) == 0) { 4 35080 35084
} else if (klen == 11 && memcmp(k, "max_fetches", 11) == 0) { 4 0 4
451 4 oe->max_fetches = (int)SvIV(v);
452 35080 ▶ } else if (klen == 4 && memcmp(k, "base", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 4 && memcmp(k, "base", 4) == 0) { 5432 29648 35080
} else if (klen == 4 && memcmp(k, "base", 4) == 0) { 5432 0 5432
453 5432 oe->base_uri = SvPV_nolen(v); /* lives in the options HV */
454 29648 ▶ } else if (klen == 8 && memcmp(k, "xinclude", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 8 && memcmp(k, "xinclude", 8) == 0) { 24142 5506 29648
} else if (klen == 8 && memcmp(k, "xinclude", 8) == 0) { 1084 23058 24142
455 1084 oe->xinclude = SvTRUE(v) ? 1 : 0;
456 28564 ▶ } else if (klen == 18 && memcmp(k, "max_xinclude_depth", 18) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 18 && memcmp(k, "max_xinclude_depth", 18) == 0) { 3 28561 28564
} else if (klen == 18 && memcmp(k, "max_xinclude_depth", 18) == 0) { 3 0 3
457 3 oe->max_xinclude_depth = (int)SvIV(v);
458 28561 ▶ } else if (klen == 7 && memcmp(k, "resolve", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 7 && memcmp(k, "resolve", 7) == 0) { 5499 23062 28561
} else if (klen == 7 && memcmp(k, "resolve", 7) == 0) { 5499 0 5499
459 5499 ▶ if (!SvOK(v)) continue; /* undef: no resolver */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) continue; /* undef: no resolver */ 2 5497 5499
460 5497 ▶ if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) { 5485 12 5497
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) { 5485 0 5485
461 5485 ud->cv = SvRV(v); /* the HV holds the reference */
462 5485 oe->resolve = frx_perl_resolve;
463 5485 oe->resolve_ud = ud;
464 12 ▶ } else if (SvPOK(v) && SvCUR(v) == 4 && memcmp(SvPVX(v), "file", 4) == 0) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (SvPOK(v) && SvCUR(v) == 4 && memcmp(SvPVX(v), "file", 4) == 0) { 12 0 12
} else if (SvPOK(v) && SvCUR(v) == 4 && memcmp(SvPVX(v), "file", 4) == 0) { 11 1 12
} else if (SvPOK(v) && SvCUR(v) == 4 && memcmp(SvPVX(v), "file", 4) == 0) { 11 0 11
465 11 ▶ if (!path)
Branch: 1 branch
BranchTrueFalseTotal
if (!path) 1 10 11
466 1 croak("File::Raw::XML: resolve => 'file' needs a document with a path; "
467 "use file_slurp($path, plugin => 'xml', resolve => 'file')");
468 {
469 10 char *d = frx_dir_of(path);
470 10 ▶ if (!d)
Branch: 1 branch
BranchTrueFalseTotal
if (!d) 0 10 10
471 0 croak("File::Raw::XML: resolve => 'file': cannot resolve the directory of '%s'", path);
472 10 ud->dir = savepv(d); /* freed with the pseudo-block, croak or not */
473 10 SAVEFREEPV(ud->dir);
474 10 free(d);
475 }
476 10 ud->dir_len = strlen(ud->dir);
477 10 oe->resolve = frx_file_resolve;
478 10 oe->resolve_ud = ud;
479 } else {
480 1 croak("File::Raw::XML: resolve must be a coderef or 'file'");
481 }
482 23062 ▶ } else if (klen == 8 && memcmp(k, "id_attrs", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 8 && memcmp(k, "id_attrs", 8) == 0) { 23058 4 23062
} else if (klen == 8 && memcmp(k, "id_attrs", 8) == 0) { 23058 0 23058
483 AV *av;
484 I32 n, i;
485 const char **names;
486 23058 ▶ if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) 23056 2 23058
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) 0 23056 23056
487 2 croak("File::Raw::XML: id_attrs must be an arrayref of attribute names");
488 23056 av = (AV *)SvRV(v);
489 23056 n = (I32)(av_len(av) + 1);
490 23056 ▶ if (!n) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) continue; 1 23055 23056
491 23055 Newx(names, n, const char *);
492 23055 SAVEFREEPV(names);
493 46109 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 23055 23054 46109
494 23055 SV **e = av_fetch(av, i, 0);
495 23055 ▶ if (!e || !SvOK(*e))
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) 23055 0 23055
if (!e || !SvOK(*e)) 1 23054 23055
496 1 croak("File::Raw::XML: id_attrs must be an arrayref of attribute names");
497 46108 names[i] = SvPV_nolen(*e);
498 }
499 23054 o->id_attrs = names;
500 23054 o->n_id_attrs = (int)n;
501 } else {
502 4 croak("File::Raw::XML: unknown option '%.*s'", (int)klen, k);
503 }
504 }
505 /* an option the strict profile would silently ignore is refused instead */
506 28848 ▶ if (oe->profile == FRX_PROFILE_STRICT) {
Branch: 1 branch
BranchTrueFalseTotal
if (oe->profile == FRX_PROFILE_STRICT) { 22204 6644 28848
507 22204 ▶ if (oe->encoding)
Branch: 1 branch
BranchTrueFalseTotal
if (oe->encoding) 1 22203 22204
508 1 croak("File::Raw::XML: encoding is an option of profile => 'full'; strict takes UTF-8 only");
509 22203 ▶ if (oe->max_entity_depth || oe->max_expansion_bytes || oe->max_expansion_ratio)
Branch: 3 branches
BranchTrueFalseTotal
if (oe->max_entity_depth || oe->max_expansion_bytes || oe->max_expansion_ratio) 22202 1 22203
if (oe->max_entity_depth || oe->max_expansion_bytes || oe->max_expansion_ratio) 22201 1 22202
if (oe->max_entity_depth || oe->max_expansion_bytes || oe->max_expansion_ratio) 1 22200 22201
510 3 croak("File::Raw::XML: the expansion budgets are options of profile => 'full'; strict expands no entity");
511 22200 ▶ if (oe->validate)
Branch: 1 branch
BranchTrueFalseTotal
if (oe->validate) 2 22198 22200
512 2 croak("File::Raw::XML: validate is an option of profile => 'full'; strict reads no DOCTYPE");
513 22198 ▶ if (oe->resolve || oe->max_fetches || oe->base_uri)
Branch: 3 branches
BranchTrueFalseTotal
if (oe->resolve || oe->max_fetches || oe->base_uri) 22196 2 22198
if (oe->resolve || oe->max_fetches || oe->base_uri) 22195 1 22196
if (oe->resolve || oe->max_fetches || oe->base_uri) 1 22194 22195
514 4 croak("File::Raw::XML: resolve, base and max_fetches are options of profile => 'full'; strict reads nothing external");
515 22194 ▶ if (oe->xinclude || oe->max_xinclude_depth)
Branch: 2 branches
BranchTrueFalseTotal
if (oe->xinclude || oe->max_xinclude_depth) 22193 1 22194
if (oe->xinclude || oe->max_xinclude_depth) 1 22192 22193
516 2 croak("File::Raw::XML: xinclude is an option of profile => 'full'; strict includes nothing");
517 }
518 }
519  
520 /* parse bytes under an options HV into a blessed document, or croak in
521 * the one message shape. path is the file the bytes came from, or NULL. */
522 static SV *
523 27995 frx_parse_to_sv(pTHX_ const char *in, STRLEN len, HV *opts, const char *path)
524 {
525 frx_opts_ex o;
526 frx_err e;
527 frx_doc *d;
528 frx_resolver_ud ud;
529 27995 char *real = NULL;
530 27995 frx_opts_from_hv(aTHX_ opts, &o, &ud, path);
531 27973 ▶ if (o.resolve && !o.base_uri && path) {
Branch: 3 branches
BranchTrueFalseTotal
if (o.resolve && !o.base_uri && path) { 5493 22480 27973
if (o.resolve && !o.base_uri && path) { 68 5425 5493
if (o.resolve && !o.base_uri && path) { 9 59 68
532 9 real = frx_realpath(path);
533 9 ▶ if (real) o.base_uri = real;
Branch: 1 branch
BranchTrueFalseTotal
if (real) o.base_uri = real; 9 0 9
534 }
535 27973 d = frx_parse_doc_ex(in, (size_t)len, &o, &e);
536 27973 free(real);
537 27973 ▶ if (!d) {
Branch: 1 branch
BranchTrueFalseTotal
if (!d) { 20999 6974 27973
538 char msg[512];
539 20999 size_t n = frx_err_format(&e, in, (size_t)len, msg, sizeof msg);
540 20999 croak("%.*s", (int)n, msg);
541 }
542 6974 return frx_doc_bless(aTHX_ d);
543 }
544  
545 /* ns as the ABI wants it: NULL for undef (any), the string otherwise */
546 static const char *
547 36 frx_ns_arg(pTHX_ SV *sv)
548 {
549 50 ▶ return SvOK(sv) ? SvPV_nolen(sv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvOK(sv) ? SvPV_nolen(sv) : NULL; 14 22 36
550 }
551  
552 /* ---- the writer's options and result ---------------------------------- */
553  
554 /* the document behind a node's inner IV (the mg_obj of a node) */
555 static frx_doc *
556 129 frx_doc_from_iv(pTHX_ SV *iv)
557 {
558 129 MAGIC *mg = mg_findext(iv, PERL_MAGIC_ext, &frx_doc_vtbl);
559 129 ▶ if (!mg || !mg->mg_ptr) croak("File::Raw::XML: the node's document is gone");
Branch: 2 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr) croak("File::Raw::XML: the node's document is gone"); 129 0 129
if (!mg || !mg->mg_ptr) croak("File::Raw::XML: the node's document is gone"); 0 129 129
560 129 return (frx_doc *)mg->mg_ptr;
561 }
562  
563 /* to_string(%opts) from an options HV. is_doc sets the defaults a whole
564 * document wants (a declaration, the DOCTYPE); *perl_chars is set by
565 * encoding => 'perl', which asks for a character string. The preserve
566 * names live on the savestack for the call. */
567 static void
568 1738 frx_write_opts_from_hv(pTHX_ HV *hv, frx_write_opts *o, int *perl_chars, const char *who, int is_doc)
569 {
570 HE *he;
571 1738 frx_write_opts_init(o);
572 1738 o->declaration = is_doc;
573 1738 o->doctype = is_doc;
574 1738 *perl_chars = 0;
575 1738 ▶ if (!hv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hv) return; 0 1738 1738
576 1738 hv_iterinit(hv);
577 3876 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 2141 1735 3876
578 I32 klen;
579 2141 const char *k = hv_iterkey(he, &klen);
580 2141 SV *v = hv_iterval(hv, he);
581 2141 ▶ if (klen == 6 && memcmp(k, "plugin", 6) == 0) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 6 && memcmp(k, "plugin", 6) == 0) continue; 428 1713 2141
if (klen == 6 && memcmp(k, "plugin", 6) == 0) continue; 2 426 428
582 2139 ▶ if (klen == 11 && memcmp(k, "declaration", 11) == 0) o->declaration = SvTRUE(v) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 11 && memcmp(k, "declaration", 11) == 0) o->declaration = SvTRUE(v) ? 1 : 0; 439 1700 2139
if (klen == 11 && memcmp(k, "declaration", 11) == 0) o->declaration = SvTRUE(v) ? 1 : 0; 19 420 439
583 2120 ▶ else if (klen == 6 && memcmp(k, "indent", 6) == 0) o->indent = (int)SvIV(v);
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 6 && memcmp(k, "indent", 6) == 0) o->indent = (int)SvIV(v); 426 1694 2120
else if (klen == 6 && memcmp(k, "indent", 6) == 0) o->indent = (int)SvIV(v); 426 0 426
584 1694 ▶ else if (klen == 11 && memcmp(k, "empty_short", 11) == 0) o->empty_short = SvTRUE(v) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 11 && memcmp(k, "empty_short", 11) == 0) o->empty_short = SvTRUE(v) ? 1 : 0; 420 1274 1694
else if (klen == 11 && memcmp(k, "empty_short", 11) == 0) o->empty_short = SvTRUE(v) ? 1 : 0; 420 0 420
585 1274 ▶ else if (klen == 10 && memcmp(k, "escape_all", 10) == 0) o->escape_all = SvTRUE(v) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 10 && memcmp(k, "escape_all", 10) == 0) o->escape_all = SvTRUE(v) ? 1 : 0; 1 1273 1274
else if (klen == 10 && memcmp(k, "escape_all", 10) == 0) o->escape_all = SvTRUE(v) ? 1 : 0; 1 0 1
586 1273 ▶ else if (klen == 7 && memcmp(k, "doctype", 7) == 0) o->doctype = SvTRUE(v) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 7 && memcmp(k, "doctype", 7) == 0) o->doctype = SvTRUE(v) ? 1 : 0; 421 852 1273
else if (klen == 7 && memcmp(k, "doctype", 7) == 0) o->doctype = SvTRUE(v) ? 1 : 0; 1 420 421
587 1272 ▶ else if (klen == 7 && memcmp(k, "drop_ws", 7) == 0) o->drop_ws = SvTRUE(v) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 7 && memcmp(k, "drop_ws", 7) == 0) o->drop_ws = SvTRUE(v) ? 1 : 0; 420 852 1272
else if (klen == 7 && memcmp(k, "drop_ws", 7) == 0) o->drop_ws = SvTRUE(v) ? 1 : 0; 420 0 420
588 852 ▶ else if (klen == 5 && memcmp(k, "quote", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 5 && memcmp(k, "quote", 5) == 0) { 422 430 852
else if (klen == 5 && memcmp(k, "quote", 5) == 0) { 421 1 422
589 STRLEN n;
590 421 const char *q = SvPV(v, n);
591 421 ▶ if (n != 1 || (q[0] != '"' && q[0] != '\''))
Branch: 3 branches
BranchTrueFalseTotal
if (n != 1 || (q[0] != '"' && q[0] != '\'')) 421 0 421
if (n != 1 || (q[0] != '"' && q[0] != '\'')) 421 0 421
if (n != 1 || (q[0] != '"' && q[0] != '\'')) 1 420 421
592 1 croak("File::Raw::XML: %s: quote must be \" or '", who);
593 420 o->quote = q[0];
594 859 ▶ } else if (klen == 8 && memcmp(k, "encoding", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 8 && memcmp(k, "encoding", 8) == 0) { 430 1 431
} else if (klen == 8 && memcmp(k, "encoding", 8) == 0) { 429 1 430
595 STRLEN n;
596 429 const char *e = SvPV(v, n);
597 429 ▶ if (n == 4 && memcmp(e, "perl", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (n == 4 && memcmp(e, "perl", 4) == 0) { 2 427 429
if (n == 4 && memcmp(e, "perl", 4) == 0) { 2 0 2
598 2 o->encoding = FRX_ENC_UTF8;
599 2 o->encoding_attr = 0;
600 2 *perl_chars = 1;
601 427 ▶ } else if (frx_enc_ieq(e, n, "utf-16") || frx_enc_ieq(e, n, "utf-16le")) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (frx_enc_ieq(e, n, "utf-16") || frx_enc_ieq(e, n, "utf-16le")) { 7 420 427
} else if (frx_enc_ieq(e, n, "utf-16") || frx_enc_ieq(e, n, "utf-16le")) { 0 7 7
602 420 o->encoding = FRX_ENC_UTF16LE;
603 7 ▶ } else if (frx_enc_ieq(e, n, "utf-16be")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (frx_enc_ieq(e, n, "utf-16be")) { 1 6 7
604 1 o->encoding = FRX_ENC_UTF16BE;
605 } else {
606 6 int kind = frx_enc_kind_by_name(e, n);
607 6 ▶ if (kind < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (kind < 0) 1 5 6
608 1 croak("File::Raw::XML: %s: encoding must be UTF-8, UTF-16, UTF-16LE, UTF-16BE, ISO-8859-1, US-ASCII or perl", who);
609 5 o->encoding = kind;
610 }
611 3 ▶ } else if (klen == 8 && memcmp(k, "preserve", 8) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 8 && memcmp(k, "preserve", 8) == 0) { 1 1 2
} else if (klen == 8 && memcmp(k, "preserve", 8) == 0) { 1 0 1
612 AV *av;
613 I32 n, i;
614 const char **names;
615 1 ▶ if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) 1 0 1
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVAV) 0 1 1
616 0 croak("File::Raw::XML: %s: preserve must be an arrayref of element names", who);
617 1 av = (AV *)SvRV(v);
618 1 n = (I32)(av_len(av) + 1);
619 1 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 1 0 1
620 1 Newx(names, n, const char *);
621 1 SAVEFREEPV(names);
622 2 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 1 2
623 1 SV **e = av_fetch(av, i, 0);
624 1 ▶ if (!e || !SvOK(*e)) croak("File::Raw::XML: %s: preserve must be an arrayref of element names", who);
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) croak("File::Raw::XML: %s: preserve must be an arrayref of element names", who); 1 0 1
if (!e || !SvOK(*e)) croak("File::Raw::XML: %s: preserve must be an arrayref of element names", who); 0 1 1
625 2 names[i] = SvPV_nolen(*e);
626 }
627 1 o->preserve = names;
628 1 o->n_preserve = (int)n;
629 }
630 } else {
631 1 croak("File::Raw::XML: %s: unknown option '%.*s'", who, (int)klen, k);
632 }
633 }
634 }
635  
636 /* the markup of apex under o, as a +1 SV: bytes, or characters for
637 * encoding => 'perl' */
638 static SV *
639 1734 frx_write_sv(pTHX_ const frx_node *apex, const frx_doc *d, const frx_write_opts *o, int perl_chars, const char *who)
640 {
641 frx_buf b;
642 frx_err e;
643 SV *out;
644 1734 frx_buf_init(&b);
645 1734 ▶ if (!frx_write(apex, d, o, &b, &e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_write(apex, d, o, &b, &e)) { 1 1733 1734
646 1 frx_buf_free(&b);
647 1 ▶ croak("File::Raw::XML: %s: %s at the node at byte offset %lu", who, e.what ? e.what : "cannot write", (unsigned long)e.offset);
Branch: 1 branch
BranchTrueFalseTotal
croak("File::Raw::XML: %s: %s at the node at byte offset %lu", who, e.what ? e.what : "cannot write", (unsigned long)e.offset); 1 0 1
648 }
649 1733 ▶ out = newSVpvn(b.p ? b.p : "", b.len);
Branch: 1 branch
BranchTrueFalseTotal
out = newSVpvn(b.p ? b.p : "", b.len); 1733 0 1733
650 1733 frx_buf_free(&b);
651 1733 ▶ if (perl_chars) SvUTF8_on(out);
Branch: 1 branch
BranchTrueFalseTotal
if (perl_chars) SvUTF8_on(out); 1 1732 1733
652 1733 return out;
653 }
654  
655 /* ---- the reader object ---------------------------------------------------
656 *
657 * A File::Raw::XML::Reader is a blessed IV with ext magic whose free hook
658 * frees this record: the reader itself, the resolver state it may hold
659 * (the coderef kept at +1, the confined directory), and durable copies
660 * of the option strings the options HV would otherwise own for one call. */
661  
662 #define FRX_READER_CLASS "File::Raw::XML::Reader"
663  
664 /* The event kinds a caller can name a callback for. FRX_DOCTYPE (7) is
665 * the highest, so a table indexed by kind is eight wide, and the push
666 * form dispatches by indexing it rather than by looking a name up per
667 * event. Returns -1 for a key that is not an event name, which is how
668 * file_xml_events tells a callback from a reader option. */
669 #define FRX_EVENT_KINDS 8
670  
671 static int
672 46 frx_event_kind_named(const char *k, STRLEN n)
673 {
674 46 switch (n) {
675 2 ▶ case 2: if (memcmp(k, "pi", 2) == 0) return FRX_PI; break;
Branch: 1 branch
BranchTrueFalseTotal
case 2: if (memcmp(k, "pi", 2) == 0) return FRX_PI; break; 2 0 2
676 4 ▶ case 3: if (memcmp(k, "end", 3) == 0) return FRX_END; break;
Branch: 1 branch
BranchTrueFalseTotal
case 3: if (memcmp(k, "end", 3) == 0) return FRX_END; break; 4 0 4
677 9 ▶ case 4: if (memcmp(k, "text", 4) == 0) return FRX_TEXT; break;
Branch: 1 branch
BranchTrueFalseTotal
case 4: if (memcmp(k, "text", 4) == 0) return FRX_TEXT; break; 9 0 9
678 17 ▶ case 5: if (memcmp(k, "start", 5) == 0) return FRX_START; break;
Branch: 1 branch
BranchTrueFalseTotal
case 5: if (memcmp(k, "start", 5) == 0) return FRX_START; break; 17 0 17
679 14 case 7:
680 14 ▶ if (memcmp(k, "comment", 7) == 0) return FRX_COMMENT;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(k, "comment", 7) == 0) return FRX_COMMENT; 2 12 14
681 12 ▶ if (memcmp(k, "doctype", 7) == 0) return FRX_DOCTYPE;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(k, "doctype", 7) == 0) return FRX_DOCTYPE; 4 8 12
682 8 break;
683 0 default: break;
684 }
685 8 return -1;
686 }
687  
688 /* what from_file pulls from the file each time the parser wants more */
689 #define FRX_READER_CHUNK (64 * 1024)
690  
691 typedef struct frx_reader_xs {
692 frx_reader r;
693 frx_resolver_ud ud;
694 SV *cv;
695 char *base;
696 char *encoding;
697 char *dir;
698 /* a reader made by from_file owns a File::Raw chunk handle and pulls
699 * from it in next, in C: -1 when the bytes come from feed instead.
700 * Zero is a valid handle, so this is set after the memset, not by
701 * it. file_chunk.h comes in through XML.xs, before this header. */
702 IV chunk;
703 } frx_reader_xs;
704  
705 static char *
706 9 frx_strdup(const char *s)
707 {
708 9 size_t n = strlen(s);
709 9 char *p = (char *)malloc(n + 1);
710 9 ▶ if (p) memcpy(p, s, n + 1);
Branch: 1 branch
BranchTrueFalseTotal
if (p) memcpy(p, s, n + 1); 9 0 9
711 9 return p;
712 }
713  
714 static void
715 863 frx_reader_xs_free(pTHX_ frx_reader_xs *x)
716 {
717 863 ▶ if (!x) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!x) return; 0 863 863
718 863 ▶ if (x->chunk >= 0) file_chunk_close(aTHX_ x->chunk);
Branch: 1 branch
BranchTrueFalseTotal
if (x->chunk >= 0) file_chunk_close(aTHX_ x->chunk); 802 61 863
719 863 frx_reader_free(&x->r);
720 863 ▶ if (x->cv) SvREFCNT_dec(x->cv);
Branch: 1 branch
BranchTrueFalseTotal
if (x->cv) SvREFCNT_dec(x->cv); 0 863 863
721 863 free(x->base);
722 863 free(x->encoding);
723 863 free(x->dir);
724 863 free(x);
725 }
726  
727 /* a reader over the options HV; path is the file it reads, when it is
728 * one (the plugin), for the file resolver and the base */
729 static frx_reader_xs *
730 868 frx_reader_xs_new(pTHX_ HV *opts, const char *path)
731 {
732 frx_opts_ex oe;
733 frx_resolver_ud ud;
734 frx_reader_xs *x;
735 868 frx_opts_from_hv(aTHX_ opts, &oe, &ud, path);
736 863 x = (frx_reader_xs *)malloc(sizeof *x);
737 863 ▶ if (!x) croak("File::Raw::XML: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!x) croak("File::Raw::XML: out of memory"); 0 863 863
738 863 memset(x, 0, sizeof *x);
739 863 x->chunk = -1; /* 0 is a chunk handle, not "no handle" */
740 863 x->ud = ud;
741 863 ▶ if (ud.cv) { x->cv = SvREFCNT_inc(ud.cv); x->ud.cv = x->cv; }
Branch: 1 branch
BranchTrueFalseTotal
if (ud.cv) { x->cv = SvREFCNT_inc(ud.cv); x->ud.cv = x->cv; } 0 863 863
742 863 ▶ if (ud.dir) { x->dir = frx_strdup(ud.dir); x->ud.dir = x->dir; }
Branch: 1 branch
BranchTrueFalseTotal
if (ud.dir) { x->dir = frx_strdup(ud.dir); x->ud.dir = x->dir; } 0 863 863
743 863 ▶ if (oe.resolve) oe.resolve_ud = &x->ud;
Branch: 1 branch
BranchTrueFalseTotal
if (oe.resolve) oe.resolve_ud = &x->ud; 0 863 863
744 863 ▶ if (oe.base_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (oe.base_uri) { 0 863 863
745 0 x->base = frx_strdup(oe.base_uri);
746 0 oe.base_uri = x->base;
747 863 ▶ } else if (oe.resolve && path) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (oe.resolve && path) { 0 863 863
} else if (oe.resolve && path) { 0 0 0
748 0 x->base = frx_realpath(path);
749 0 oe.base_uri = x->base;
750 }
751 863 ▶ if (oe.encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (oe.encoding) { 0 863 863
752 0 x->encoding = frx_strdup(oe.encoding);
753 0 oe.encoding = x->encoding;
754 }
755 863 oe.base.id_attrs = NULL; /* a reader builds no index */
756 863 oe.base.n_id_attrs = 0;
757 863 frx_reader_init(&x->r, &oe);
758 863 return x;
759 }
760  
761 static int
762 857 frx_reader_mg_free(pTHX_ SV *sv, MAGIC *mg)
763 {
764 857 frx_reader_xs *x = (frx_reader_xs *)mg->mg_ptr;
765 PERL_UNUSED_ARG(sv);
766 857 ▶ if (x) {
Branch: 1 branch
BranchTrueFalseTotal
if (x) { 857 0 857
767 857 frx_reader_xs_free(aTHX_ x);
768 857 mg->mg_ptr = NULL;
769 }
770 857 return 0;
771 }
772  
773 static MGVTBL frx_reader_vtbl = { NULL, NULL, NULL, NULL, frx_reader_mg_free, NULL, NULL, NULL };
774  
775 static SV *
776 857 frx_reader_bless(pTHX_ frx_reader_xs *x, const char *class_name)
777 {
778 857 SV *iv = newSViv(0);
779 857 sv_magicext(iv, NULL, PERL_MAGIC_ext, &frx_reader_vtbl, (const char *)x, 0);
780 857 return sv_bless(newRV_noinc(iv), gv_stashpv(class_name, GV_ADD));
781 }
782  
783 static frx_reader_xs *
784 486868 frx_reader_from_sv(pTHX_ SV *sv, const char *meth)
785 {
786 486868 MAGIC *mg = frx_obj_magic(aTHX_ sv, &frx_reader_vtbl);
787 486868 ▶ if (!mg || !mg->mg_ptr)
Branch: 2 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr) 486868 0 486868
if (!mg || !mg->mg_ptr) 0 486868 486868
788 0 croak("File::Raw::XML: %s: the invocant is not a " FRX_READER_CLASS, meth);
789 486868 return (frx_reader_xs *)mg->mg_ptr;
790 }
791  
792 /* the reader's refusal, in the one message shape, over the bytes it
793 * still holds */
794 static void
795 10 frx_reader_croak(pTHX_ frx_reader_xs *x)
796 {
797 char msg[512];
798 10 size_t n = frx_err_format_at(&x->r.err, x->r.in.p, x->r.in.len, x->r.dropped, msg, sizeof msg);
799 10 croak("%.*s", (int)n, msg);
800 }
801  
802 /* ---- edits from Perl ----------------------------------------------------
803 *
804 * A node an edit creates is blessed exactly as a parsed one: the same
805 * magic, the document's inner IV as mg_obj at +1, so it holds its
806 * document and a detached node keeps the document alive. A node passed to
807 * an edit must belong to the invocant's document, checked by the inner
808 * IV; one from another document is refused before anything is touched,
809 * and import is the way across. */
810  
811 /* a node of the same document as doc_iv, writable */
812 static frx_node *
813 41 frx_node_arg(pTHX_ SV *sv, SV *doc_iv, const char *meth)
814 {
815 41 SV *their = NULL;
816 41 const frx_node *n = frx_node_from_sv(aTHX_ sv, meth, &their);
817 41 ▶ if (their != doc_iv)
Branch: 1 branch
BranchTrueFalseTotal
if (their != doc_iv) 1 40 41
818 1 croak("File::Raw::XML: %s: the node belongs to another document; use import", meth);
819 40 return (frx_node *)n;
820 }
821  
822 static void
823 26 frx_edit_croak(pTHX_ const char *meth, const frx_err *e)
824 {
825 26 ▶ if (e->offset)
Branch: 1 branch
BranchTrueFalseTotal
if (e->offset) 7 19 26
826 7 ▶ croak("File::Raw::XML: %s: %s (the node at byte offset %lu)", meth, e->what ? e->what : "refused", (unsigned long)e->offset);
Branch: 1 branch
BranchTrueFalseTotal
croak("File::Raw::XML: %s: %s (the node at byte offset %lu)", meth, e->what ? e->what : "refused", (unsigned long)e->offset); 7 0 7
827 19 ▶ croak("File::Raw::XML: %s: %s", meth, e->what ? e->what : "refused");
Branch: 1 branch
BranchTrueFalseTotal
croak("File::Raw::XML: %s: %s", meth, e->what ? e->what : "refused"); 19 0 19
828 }
829  
830 /* the bytes of a string argument, or "" for undef */
831 static const char *
832 168 frx_arg_bytes(pTHX_ SV *sv, STRLEN *len)
833 {
834 168 ▶ if (!sv || !SvOK(sv)) { *len = 0; return ""; }
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) { *len = 0; return ""; } 167 1 168
if (!sv || !SvOK(sv)) { *len = 0; return ""; } 0 167 167
835 167 return frx_input(aTHX_ sv, len);
836 }
837  
838 /* prefix and local from a name that may carry one colon */
839 static void
840 53 frx_split_arg(const char *name, STRLEN nlen, const char **prefix, STRLEN *plen, const char **local, STRLEN *llen)
841 {
842 53 const char *colon = (const char *)memchr(name, ':', nlen);
843 53 ▶ if (colon) {
Branch: 1 branch
BranchTrueFalseTotal
if (colon) { 20 33 53
844 20 *prefix = name; *plen = (STRLEN)(colon - name);
845 20 *local = colon + 1; *llen = nlen - *plen - 1;
846 } else {
847 33 *prefix = ""; *plen = 0;
848 33 *local = name; *llen = nlen;
849 }
850 53 }
851  
852 /* the current event's doctype as the Document method renders one */
853 static SV *
854 22 frx_doctype_sv(pTHX_ const frx_doctype *dt)
855 {
856 HV *hv;
857 22 ▶ if (!dt) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!dt) return newSV(0); 1 21 22
858 21 hv = newHV();
859 21 (void)hv_stores(hv, "name", frx_str_sv(aTHX_ &dt->name));
860 21 ▶ (void)hv_stores(hv, "public_id", dt->public_id.len ? frx_str_sv(aTHX_ &dt->public_id) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(hv, "public_id", dt->public_id.len ? frx_str_sv(aTHX_ &dt->public_id) : newSV(0)); 0 21 21
861 21 ▶ (void)hv_stores(hv, "system_id", dt->system_id.len ? frx_str_sv(aTHX_ &dt->system_id) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(hv, "system_id", dt->system_id.len ? frx_str_sv(aTHX_ &dt->system_id) : newSV(0)); 1 20 21
862 21 ▶ (void)hv_stores(hv, "internal_subset", dt->has_subset ? frx_str_sv(aTHX_ &dt->subset) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(hv, "internal_subset", dt->has_subset ? frx_str_sv(aTHX_ &dt->subset) : newSV(0)); 19 2 21
863 21 return newRV_noinc((SV *)hv);
864 }
865  
866 #endif /* FRX_OBJ_H */
« xs/internal.xs ↑ Summary XML.xs »