include/frx/frx_obj.h

filter: Statementstmt 97.5% 446 / 457 Branchbran 72.4% 361 / 498 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.5% 807 / 955 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_entity.h ↑ Summary xs/internal.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 767740 frx_doc_mg_free(pTHX_ SV *sv, MAGIC *mg)
48 {
49 767740 frx_doc *d = (frx_doc *)mg->mg_ptr;
50 PERL_UNUSED_ARG(sv);
51 767740 ▶ if (d) {
Branch: 1 branch
BranchTrueFalseTotal
if (d) { 767740 0 767740
52 767740 frx_doc_free(d);
53 767740 mg->mg_ptr = NULL;
54 }
55 767740 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 767740 frx_doc_bless(pTHX_ frx_doc *d)
65 {
66 767740 SV *iv = newSViv(0);
67 767740 sv_magicext(iv, NULL, PERL_MAGIC_ext, &frx_doc_vtbl, (const char *)d, 0);
68 767740 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 863825 frx_node_bless(pTHX_ SV *doc_iv, const frx_node *n)
74 {
75 863825 SV *iv = newSViv(0);
76 863825 sv_magicext(iv, doc_iv, PERL_MAGIC_ext, &frx_node_vtbl, (const char *)n, 0);
77 863825 return sv_bless(newRV_noinc(iv), gv_stashpv(FRX_NODE_CLASS, GV_ADD));
78 }
79  
80 static MAGIC *
81 2021515 frx_obj_magic(pTHX_ SV *sv, const MGVTBL *vtbl)
82 {
83 2021515 ▶ if (!sv || !SvROK(sv)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv)) return NULL; 2021515 0 2021515
if (!sv || !SvROK(sv)) return NULL; 4 2021511 2021515
84 2021511 sv = SvRV(sv);
85 2021511 ▶ if (!SvOBJECT(sv) || SvTYPE(sv) >= SVt_PVAV) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT(sv) || SvTYPE(sv) >= SVt_PVAV) return NULL; 2021510 1 2021511
if (!SvOBJECT(sv) || SvTYPE(sv) >= SVt_PVAV) return NULL; 1 2021509 2021510
86 2021509 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 /* ---- the C ABI's SV bridge ------------------------------------------------
118 *
119 * Declared in frx_abi_impl.h, which the table needs and which is included
120 * before this file. These are the entries a consumer crosses between a
121 * handle and a blessed object with.
122 *
123 * The two unwrapping entries answer NULL where the invocant forms above
124 * croak. A consumer uses them as the type test - Punk asks "is this thing a
125 * document?" of every object a handler returns - and a croak is the wrong
126 * answer to a question. It is also the rule the whole table keeps: the core
127 * fills an frx_err and returns NULL, it does not longjmp through a
128 * consumer's C. */
129  
130 static SV *
131 1 frx_abi_doc_to_sv(pTHX_ frx_doc *d)
132 {
133 1 ▶ if (!d) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return NULL; 0 1 1
134 1 return frx_doc_bless(aTHX_ d); /* takes ownership; magic frees it */
135 }
136  
137 static frx_doc *
138 3 frx_abi_doc_from_sv(pTHX_ SV *sv)
139 {
140 3 MAGIC *mg = frx_obj_magic(aTHX_ sv, &frx_doc_vtbl);
141 3 ▶ return (mg && mg->mg_ptr) ? (frx_doc *)mg->mg_ptr : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (mg && mg->mg_ptr) ? (frx_doc *)mg->mg_ptr : NULL; 1 2 3
return (mg && mg->mg_ptr) ? (frx_doc *)mg->mg_ptr : NULL; 1 0 1
142 }
143  
144 static const frx_node *
145 4 frx_abi_node_from_sv(pTHX_ SV *sv, frx_doc **owner)
146 {
147 4 MAGIC *mg = frx_obj_magic(aTHX_ sv, &frx_node_vtbl);
148 4 ▶ if (owner) *owner = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (owner) *owner = NULL; 3 1 4
149 4 ▶ if (!mg || !mg->mg_ptr || !mg->mg_obj) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr || !mg->mg_obj) return NULL; 2 2 4
if (!mg || !mg->mg_ptr || !mg->mg_obj) return NULL; 2 0 2
if (!mg || !mg->mg_ptr || !mg->mg_obj) return NULL; 0 2 2
150 2 ▶ if (owner) {
Branch: 1 branch
BranchTrueFalseTotal
if (owner) { 2 0 2
151 /* mg_obj is the document's inner IV, which carries the doc magic */
152 2 MAGIC *dm = mg_findext(mg->mg_obj, PERL_MAGIC_ext, &frx_doc_vtbl);
153 2 ▶ if (!dm || !dm->mg_ptr) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!dm || !dm->mg_ptr) return NULL; 2 0 2
if (!dm || !dm->mg_ptr) return NULL; 0 2 2
154 2 *owner = (frx_doc *)dm->mg_ptr;
155 }
156 2 return (const frx_node *)mg->mg_ptr;
157 }
158  
159 static SV *
160 1 frx_abi_node_to_sv(pTHX_ SV *doc_sv, const frx_node *n)
161 {
162 MAGIC *mg;
163 1 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 1 1
164 1 mg = frx_obj_magic(aTHX_ doc_sv, &frx_doc_vtbl);
165 1 ▶ if (!mg || !mg->mg_ptr) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr) return NULL; 1 0 1
if (!mg || !mg->mg_ptr) return NULL; 0 1 1
166 /* the node's magic holds the document's inner IV, not the RV, which is
167 * what keeps the document alive for exactly as long as the node */
168 1 return frx_node_bless(aTHX_ SvRV(doc_sv), n);
169 }
170  
171 /* a string across the seam: characters */
172 static SV *
173 864017 frx_str_sv(pTHX_ const frx_str *s)
174 {
175 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
176 864017 SvUTF8_on(sv);
177 864017 return sv;
178 }
179  
180 /* the bytes of an input SV: UTF-8 when flagged, raw otherwise; the lexer
181 * refuses what is not UTF-8 */
182 static const char *
183 30148 frx_input(pTHX_ SV *sv, STRLEN *len)
184 {
185 30154 ▶ if (SvUTF8(sv)) return SvPVutf8(sv, *len);
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(sv)) return SvPVutf8(sv, *len); 6 30142 30148
186 30142 return SvPV(sv, *len);
187 }
188  
189 /* the tail of an XSUB's arguments as an options HV, mortal; odd croaks */
190 static HV *
191 33517 frx_opts_hv(pTHX_ SV **st, I32 first, I32 items, const char *who)
192 {
193 HV *hv;
194 I32 i;
195 33517 ▶ if (((items - first) & 1) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (((items - first) & 1) != 0) 2 33515 33517
196 2 croak("File::Raw::XML: %s: options must be key/value pairs", who);
197 33515 hv = (HV *)sv_2mortal((SV *)newHV());
198 80012 ▶ for (i = first; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = first; i < items; i += 2) { 46497 33515 80012
199 STRLEN klen;
200 46497 const char *k = SvPV(st[i], klen);
201 46497 (void)hv_store(hv, k, (I32)klen, newSVsv(st[i + 1]), 0);
202 }
203 33515 return hv;
204 }
205  
206 /* ---- resolvers ---------------------------------------------------------
207 *
208 * The core fetches nothing: an external subset or entity reaches it as
209 * bytes only through frx_opts_ex.resolve, a C function the caller
210 * supplies. Two live here. The coderef resolver calls a Perl sub with
211 * kind, public_id, system_id and base and takes the string it returns as
212 * the bytes; a die inside it becomes the refusal, with the message, at
213 * the reference's offset, and undef is a refusal too. The file resolver
214 * is confined to the document's own directory: the system identifier
215 * (already resolved against the document's path) must have no scheme or
216 * the file scheme, must be a regular file, and its real path, symbolic
217 * links followed, must lie under the real path of the document's
218 * directory. It exists only for file_slurp, which has a path; the codec
219 * has none and refuses it. */
220  
221 typedef struct frx_resolver_ud {
222 #ifdef PERL_IMPLICIT_CONTEXT
223 PerlInterpreter *my_perl;
224 #endif
225 SV *cv; /* the coderef; NULL for the file resolver */
226 char *dir; /* the document's directory, real path, malloc'd; file resolver */
227 size_t dir_len;
228 char msg[512]; /* a refusal message built here; static for the parse's life */
229 } frx_resolver_ud;
230  
231 typedef struct frx_fetch_holder {
232 frx_resolver_ud *ud;
233 SV *sv; /* the bytes, +1 */
234 } frx_fetch_holder;
235  
236 static void
237 1585 frx_perl_release(void *vh, frx_fetched *f)
238 {
239 1585 frx_fetch_holder *h = (frx_fetch_holder *)vh;
240 #ifdef PERL_IMPLICIT_CONTEXT
241 dTHXa(h->ud->my_perl);
242 #endif
243 PERL_UNUSED_ARG(f);
244 1585 SvREFCNT_dec(h->sv);
245 1585 free(h);
246 1585 }
247  
248 static const char *
249 1614 frx_kind_name(int kind)
250 {
251 1614 switch (kind) {
252 1031 case FRX_REF_SUBSET: return "subset";
253 154 case FRX_REF_XINCLUDE: return "xinclude";
254 429 default: return "entity";
255 }
256 }
257  
258 /* a C string as a character SV, or undef for NULL */
259 static SV *
260 4842 frx_cstr_sv(pTHX_ const char *p)
261 {
262 SV *sv;
263 4842 ▶ if (!p) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return newSV(0); 1781 3061 4842
264 3061 sv = newSVpv(p, 0);
265 3061 SvUTF8_on(sv);
266 3061 return sv;
267 }
268  
269 static const char *
270 1614 frx_perl_resolve(void *vud, const char *pub, const char *sys, const char *base,
271 int kind, frx_fetched *out)
272 {
273 1614 frx_resolver_ud *ud = (frx_resolver_ud *)vud;
274 #ifdef PERL_IMPLICIT_CONTEXT
275 dTHXa(ud->my_perl);
276 #endif
277 1614 dSP;
278 int count;
279 1614 SV *ret = NULL, *keep = NULL;
280 1614 const char *result = NULL;
281  
282 1614 ENTER;
283 1614 SAVETMPS;
284 1614 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1614 1614
285 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
286 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
287 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
288 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
289 1614 PUTBACK;
290 1614 count = call_sv(ud->cv, G_SCALAR | G_EVAL);
291 1614 SPAGAIN;
292 1614 ▶ if (count) ret = POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count) ret = POPs; 1614 0 1614
293 1614 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 1614 0 1614
if (SvTRUE(ERRSV)) { 28 1586 1614
294 STRLEN n;
295 28 ▶ const char *m = SvPV(ERRSV, n);
Branch: 1 branch
BranchTrueFalseTotal
const char *m = SvPV(ERRSV, n); 28 0 28
296 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
297 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
298 28 result = ud->msg;
299 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
300 1 my_snprintf(ud->msg, sizeof ud->msg, "the resolver returned undef");
301 1 result = ud->msg;
302 } else {
303 1585 keep = newSVsv(ret);
304 }
305 1614 PUTBACK;
306 1614 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1614 0 1614
307 1614 LEAVE;
308 1614 ▶ if (result) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (result) return result; 29 1585 1614
309 {
310 1585 frx_fetch_holder *h = (frx_fetch_holder *)malloc(sizeof *h);
311 STRLEN n;
312 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
313 1585 h->ud = ud;
314 1585 h->sv = keep;
315 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
316 1585 out->len = (size_t)n;
317 1585 out->release = frx_perl_release;
318 1585 out->ud = h;
319 }
320 1585 return NULL;
321 }
322  
323 static void
324 3 frx_file_release(void *vp, frx_fetched *f)
325 {
326 PERL_UNUSED_ARG(f);
327 3 free(vp);
328 3 }
329  
330 #ifndef PATH_MAX
331 #define PATH_MAX 4096
332 #endif
333  
334 /* the real, absolute path of p into a malloc'd string, or NULL */
335 static char *
336 28 frx_realpath(const char *p)
337 {
338 #ifdef _WIN32
339 char *buf = (char *)malloc(4096);
340 if (!buf) return NULL;
341 if (!_fullpath(buf, p, 4096)) { free(buf); return NULL; }
342 return buf;
343 #else
344 28 char *buf = (char *)malloc(PATH_MAX + 1);
345 28 ▶ if (!buf) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) return NULL; 0 28 28
346 28 ▶ if (!realpath(p, buf)) { free(buf); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!realpath(p, buf)) { free(buf); return NULL; } 2 26 28
347 26 return buf;
348 #endif
349 }
350  
351 static int
352 89 frx_is_dir_sep(char c)
353 {
354 #ifdef _WIN32
355 return c == '/' || c == '\\';
356 #else
357 89 return c == '/';
358 #endif
359 }
360  
361 /* "C:" and what follows it: an absolute path with its drive, on the one
362 * platform that has drives */
363 static int
364 9 frx_is_drive(const char *p)
365 {
366 #ifdef _WIN32
367 return ((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z')) && p[1] == ':';
368 #else
369 PERL_UNUSED_ARG(p);
370 9 return 0;
371 #endif
372 }
373  
374 /* the real path of p as a base a URI join can use (RFC 3986 section 5.2).
375 * A POSIX path already is one. A Windows path is not: its drive letter
376 * parses as a scheme, and its separators are not the path separator a
377 * merge looks for, so "doc.dtd" against "C:\d\doc.xml" would come out
378 * "C:doc.dtd" and be read against the process directory. It becomes
379 * /C:/d/doc.xml, which merges as any absolute path does; frx_file_resolve
380 * reads that form, and a file: identifier, back to a native path. */
381 static char *
382 9 frx_realpath_as_base(const char *p)
383 {
384 9 char *real = frx_realpath(p);
385 #ifdef _WIN32
386 char *uri;
387 size_t n, i, j = 0;
388 if (!real) return NULL;
389 n = strlen(real);
390 uri = (char *)malloc(n + 2);
391 if (!uri) { free(real); return NULL; }
392 if (frx_is_drive(real)) uri[j++] = '/';
393 for (i = 0; i < n; i++) uri[j++] = real[i] == '\\' ? '/' : real[i];
394 uri[j] = '\0';
395 free(real);
396 return uri;
397 #else
398 9 return real;
399 #endif
400 }
401  
402 /* real starts with the dir_len bytes of dir, as this platform names files */
403 static int
404 7 frx_path_under(const char *real, const char *dir, size_t dir_len)
405 {
406 #ifdef _WIN32
407 size_t i;
408 for (i = 0; i < dir_len; i++) {
409 char a = real[i], b = dir[i];
410 if (!a) return 0;
411 if (a >= 'A' && a <= 'Z') a = (char)(a + 32);
412 if (b >= 'A' && b <= 'Z') b = (char)(b + 32);
413 if (a == '/') a = '\\';
414 if (b == '/') b = '\\';
415 if (a != b) return 0;
416 }
417 return 1;
418 #else
419 7 return strncmp(real, dir, dir_len) == 0;
420 #endif
421 }
422  
423 static const char *
424 10 frx_file_resolve(void *vud, const char *pub, const char *sys, const char *base,
425 int kind, frx_fetched *out)
426 {
427 10 frx_resolver_ud *ud = (frx_resolver_ud *)vud;
428 #ifdef PERL_IMPLICIT_CONTEXT
429 dTHXa(ud->my_perl);
430 #endif
431 const char *path;
432 char *real;
433 PerlIO *fp;
434 Stat_t st;
435 char *bytes;
436 size_t n;
437 PERL_UNUSED_ARG(pub);
438 PERL_UNUSED_ARG(base);
439 PERL_UNUSED_ARG(kind);
440  
441 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
442 10 path = sys;
443 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
444 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
445 2 path = sys + 5;
446 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
447 2 path += 2; /* file://host/path: the host must be empty */
448 2 ▶ if (*path != '/' && !frx_is_drive(path)) /* file://C:/... names a drive, not a host */
Branch: 2 branches
BranchTrueFalseTotal
if (*path != '/' && !frx_is_drive(path)) /* file://C:/... names a drive, not a host */ 0 2 2
if (*path != '/' && !frx_is_drive(path)) /* file://C:/... names a drive, not a host */ 0 0 0
449 0 return "a file: identifier with a host is not read";
450 }
451 } else {
452 8 const char *q = sys;
453 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
454 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
455 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
456 1 return "only a file path or a file: identifier is read by the file resolver";
457 }
458 9 ▶ if (path[0] == '/' && frx_is_drive(path + 1)) path++; /* /C:/d/x: the slash is the URI's */
Branch: 2 branches
BranchTrueFalseTotal
if (path[0] == '/' && frx_is_drive(path + 1)) path++; /* /C:/d/x: the slash is the URI's */ 9 0 9
if (path[0] == '/' && frx_is_drive(path + 1)) path++; /* /C:/d/x: the slash is the URI's */ 0 9 9
459 9 real = frx_realpath(path);
460 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
461 7 ▶ if (!frx_path_under(real, ud->dir, ud->dir_len) || !frx_is_dir_sep(real[ud->dir_len])) {
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_path_under(real, ud->dir, ud->dir_len) || !frx_is_dir_sep(real[ud->dir_len])) { 4 3 7
if (!frx_path_under(real, ud->dir, ud->dir_len) || !frx_is_dir_sep(real[ud->dir_len])) { 0 4 4
462 3 free(real);
463 3 return "the system identifier escapes the document's directory";
464 }
465 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
466 1 free(real);
467 1 return "the system identifier is not a regular file";
468 }
469 3 fp = PerlIO_open(real, "rb");
470 3 free(real);
471 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
472 3 n = (size_t)st.st_size;
473 3 ▶ bytes = (char *)malloc(n ? n : 1);
Branch: 1 branch
BranchTrueFalseTotal
bytes = (char *)malloc(n ? n : 1); 3 0 3
474 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
475 {
476 3 size_t got = 0;
477 6 ▶ while (got < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (got < n) { 3 3 6
478 3 SSize_t r = PerlIO_read(fp, bytes + got, n - got);
479 3 ▶ if (r <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (r <= 0) break; 0 3 3
480 3 got += (size_t)r;
481 }
482 3 n = got;
483 }
484 3 PerlIO_close(fp);
485 3 out->bytes = bytes;
486 3 out->len = n;
487 3 out->release = frx_file_release;
488 3 out->ud = bytes;
489 3 return NULL;
490 }
491  
492 /* the directory of path as a real path, for the file resolver */
493 static char *
494 10 frx_dir_of(const char *path)
495 {
496 10 char *real = frx_realpath(path);
497 size_t n;
498 10 ▶ if (!real) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!real) return NULL; 0 10 10
499 10 n = strlen(real);
500 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
501 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
502 10 real[n] = '\0';
503 10 return real;
504 }
505  
506 /* frx_opts_ex from an options HV. Unknown keys croak; `plugin` is known
507 * because file_plugin_build_opts puts it there. The id_attrs names are
508 * freed with the current pseudo-block, so parse before it ends. `profile`
509 * is 'strict' (the default) or 'full'; anything else croaks naming both.
510 * ud receives a resolver's state; path is the document's file when there
511 * is one (the plugin) and NULL for the codec. */
512 static void
513 28883 frx_opts_from_hv(pTHX_ HV *hv, frx_opts_ex *oe, frx_resolver_ud *ud, const char *path)
514 {
515 HE *he;
516 28883 frx_opts *o = &oe->base;
517 28883 memset(oe, 0, sizeof *oe);
518 28883 oe->size = sizeof *oe;
519 28883 oe->profile = FRX_PROFILE_STRICT;
520 28883 memset(ud, 0, sizeof *ud);
521 #ifdef PERL_IMPLICIT_CONTEXT
522 ud->my_perl = aTHX;
523 #endif
524 28883 ▶ if (!hv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hv) return; 0 28883 28883
525 28883 hv_iterinit(hv);
526 72918 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 44050 28868 72918
527 I32 klen;
528 44050 const char *k = hv_iterkey(he, &klen);
529 44050 SV *v = hv_iterval(hv, he);
530 44077 ▶ if (klen == 6 && memcmp(k, "plugin", 6) == 0) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 6 && memcmp(k, "plugin", 6) == 0) continue; 73 43977 44050
if (klen == 6 && memcmp(k, "plugin", 6) == 0) continue; 48 25 73
531 44002 ▶ if (klen == 6 && memcmp(k, "record", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 6 && memcmp(k, "record", 6) == 0) { 25 43977 44002
if (klen == 6 && memcmp(k, "record", 6) == 0) { 25 0 25
532 /* the stream phase's own option, read by frx_plugin.h */
533 25 ▶ 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 24 25
534 24 continue;
535 }
536 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
537 STRLEN n;
538 6651 const char *s = SvPV(v, n);
539 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
540 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
541 4 else croak("File::Raw::XML: profile must be 'strict' or 'full', not '%.*s'", (int)n, s);
542 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
543 /* the string lives in the options HV, which outlives the parse */
544 8 oe->encoding = SvPV_nolen(v);
545 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
546 7 o->max_bytes = (size_t)SvUV(v);
547 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
548 3 o->max_depth = (int)SvIV(v);
549 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
550 5 oe->max_entity_depth = (int)SvIV(v);
551 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
552 5 oe->max_expansion_bytes = (size_t)SvUV(v);
553 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
554 5 oe->max_expansion_ratio = (int)SvIV(v);
555 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
556 /* 1 stops at the first violation, 'collect' gathers them all */
557 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
558 STRLEN n;
559 2096 const char *s = SvPV(v, n);
560 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
561 1 ▶ else if (!n) oe->validate = 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (!n) oe->validate = 0; 0 1 1
562 1 else croak("File::Raw::XML: validate must be 1, 0 or 'collect', not '%.*s'", (int)n, s);
563 } else {
564 111 oe->validate = SvTRUE(v) ? 1 : 0;
565 }
566 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
567 2 oe->max_token_bytes = (size_t)SvUV(v); /* a reader's bound; both profiles */
568 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
569 4 oe->max_fetches = (int)SvIV(v);
570 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
571 5432 oe->base_uri = SvPV_nolen(v); /* lives in the options HV */
572 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
573 1084 oe->xinclude = SvTRUE(v) ? 1 : 0;
574 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
575 3 oe->max_xinclude_depth = (int)SvIV(v);
576 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
577 5499 ▶ if (!SvOK(v)) continue; /* undef: no resolver */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) continue; /* undef: no resolver */ 2 5497 5499
578 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
579 5485 ud->cv = SvRV(v); /* the HV holds the reference */
580 5485 oe->resolve = frx_perl_resolve;
581 5485 oe->resolve_ud = ud;
582 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
583 11 ▶ if (!path)
Branch: 1 branch
BranchTrueFalseTotal
if (!path) 1 10 11
584 1 croak("File::Raw::XML: resolve => 'file' needs a document with a path; "
585 "use file_slurp($path, plugin => 'xml', resolve => 'file')");
586 {
587 10 char *d = frx_dir_of(path);
588 10 ▶ if (!d)
Branch: 1 branch
BranchTrueFalseTotal
if (!d) 0 10 10
589 0 croak("File::Raw::XML: resolve => 'file': cannot resolve the directory of '%s'", path);
590 10 ud->dir = savepv(d); /* freed with the pseudo-block, croak or not */
591 10 SAVEFREEPV(ud->dir);
592 10 free(d);
593 }
594 10 ud->dir_len = strlen(ud->dir);
595 10 oe->resolve = frx_file_resolve;
596 10 oe->resolve_ud = ud;
597 } else {
598 1 croak("File::Raw::XML: resolve must be a coderef or 'file'");
599 }
600 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
601 AV *av;
602 I32 n, i;
603 const char **names;
604 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
605 2 croak("File::Raw::XML: id_attrs must be an arrayref of attribute names");
606 23056 av = (AV *)SvRV(v);
607 23056 n = (I32)(av_len(av) + 1);
608 23056 ▶ if (!n) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) continue; 1 23055 23056
609 23055 Newx(names, n, const char *);
610 23055 SAVEFREEPV(names);
611 46109 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 23055 23054 46109
612 23055 SV **e = av_fetch(av, i, 0);
613 23055 ▶ if (!e || !SvOK(*e))
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) 23055 0 23055
if (!e || !SvOK(*e)) 1 23054 23055
614 1 croak("File::Raw::XML: id_attrs must be an arrayref of attribute names");
615 46108 names[i] = SvPV_nolen(*e);
616 }
617 23054 o->id_attrs = names;
618 23054 o->n_id_attrs = (int)n;
619 } else {
620 4 croak("File::Raw::XML: unknown option '%.*s'", (int)klen, k);
621 }
622 }
623 /* an option the strict profile would silently ignore is refused instead */
624 28868 ▶ if (oe->profile == FRX_PROFILE_STRICT) {
Branch: 1 branch
BranchTrueFalseTotal
if (oe->profile == FRX_PROFILE_STRICT) { 22224 6644 28868
625 22224 ▶ if (oe->encoding)
Branch: 1 branch
BranchTrueFalseTotal
if (oe->encoding) 1 22223 22224
626 1 croak("File::Raw::XML: encoding is an option of profile => 'full'; strict takes UTF-8 only");
627 22223 ▶ 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) 22222 1 22223
if (oe->max_entity_depth || oe->max_expansion_bytes || oe->max_expansion_ratio) 22221 1 22222
if (oe->max_entity_depth || oe->max_expansion_bytes || oe->max_expansion_ratio) 1 22220 22221
628 3 croak("File::Raw::XML: the expansion budgets are options of profile => 'full'; strict expands no entity");
629 22220 ▶ if (oe->validate)
Branch: 1 branch
BranchTrueFalseTotal
if (oe->validate) 2 22218 22220
630 2 croak("File::Raw::XML: validate is an option of profile => 'full'; strict reads no DOCTYPE");
631 22218 ▶ if (oe->resolve || oe->max_fetches || oe->base_uri)
Branch: 3 branches
BranchTrueFalseTotal
if (oe->resolve || oe->max_fetches || oe->base_uri) 22216 2 22218
if (oe->resolve || oe->max_fetches || oe->base_uri) 22215 1 22216
if (oe->resolve || oe->max_fetches || oe->base_uri) 1 22214 22215
632 4 croak("File::Raw::XML: resolve, base and max_fetches are options of profile => 'full'; strict reads nothing external");
633 22214 ▶ if (oe->xinclude || oe->max_xinclude_depth)
Branch: 2 branches
BranchTrueFalseTotal
if (oe->xinclude || oe->max_xinclude_depth) 22213 1 22214
if (oe->xinclude || oe->max_xinclude_depth) 1 22212 22213
634 2 croak("File::Raw::XML: xinclude is an option of profile => 'full'; strict includes nothing");
635 }
636 }
637  
638 /* parse bytes under an options HV into a blessed document, or croak in
639 * the one message shape. path is the file the bytes came from, or NULL. */
640 static SV *
641 27995 frx_parse_to_sv(pTHX_ const char *in, STRLEN len, HV *opts, const char *path)
642 {
643 frx_opts_ex o;
644 frx_err e;
645 frx_doc *d;
646 frx_resolver_ud ud;
647 27995 char *real = NULL;
648 27995 frx_opts_from_hv(aTHX_ opts, &o, &ud, path);
649 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
650 9 real = frx_realpath_as_base(path);
651 9 ▶ if (real) o.base_uri = real;
Branch: 1 branch
BranchTrueFalseTotal
if (real) o.base_uri = real; 9 0 9
652 }
653 27973 d = frx_parse_doc_ex(in, (size_t)len, &o, &e);
654 27973 free(real);
655 27973 ▶ if (!d) {
Branch: 1 branch
BranchTrueFalseTotal
if (!d) { 20999 6974 27973
656 char msg[512];
657 20999 size_t n = frx_err_format(&e, in, (size_t)len, msg, sizeof msg);
658 20999 croak("%.*s", (int)n, msg);
659 }
660 6974 return frx_doc_bless(aTHX_ d);
661 }
662  
663 /* ns as the ABI wants it: NULL for undef (any), the string otherwise */
664 static const char *
665 36 frx_ns_arg(pTHX_ SV *sv)
666 {
667 50 ▶ return SvOK(sv) ? SvPV_nolen(sv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvOK(sv) ? SvPV_nolen(sv) : NULL; 14 22 36
668 }
669  
670 /* ---- the writer's options and result ---------------------------------- */
671  
672 /* the document behind a node's inner IV (the mg_obj of a node) */
673 static frx_doc *
674 129 frx_doc_from_iv(pTHX_ SV *iv)
675 {
676 129 MAGIC *mg = mg_findext(iv, PERL_MAGIC_ext, &frx_doc_vtbl);
677 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
678 129 return (frx_doc *)mg->mg_ptr;
679 }
680  
681 /* to_string(%opts) from an options HV. is_doc sets the defaults a whole
682 * document wants (a declaration, the DOCTYPE); *perl_chars is set by
683 * encoding => 'perl', which asks for a character string. The preserve
684 * names live on the savestack for the call. */
685 static void
686 1738 frx_write_opts_from_hv(pTHX_ HV *hv, frx_write_opts *o, int *perl_chars, const char *who, int is_doc)
687 {
688 HE *he;
689 1738 frx_write_opts_init(o);
690 1738 o->declaration = is_doc;
691 1738 o->doctype = is_doc;
692 1738 *perl_chars = 0;
693 1738 ▶ if (!hv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hv) return; 0 1738 1738
694 1738 hv_iterinit(hv);
695 3876 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 2141 1735 3876
696 I32 klen;
697 2141 const char *k = hv_iterkey(he, &klen);
698 2141 SV *v = hv_iterval(hv, he);
699 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
700 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
701 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
702 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
703 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
704 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
705 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
706 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
707 STRLEN n;
708 421 const char *q = SvPV(v, n);
709 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
710 1 croak("File::Raw::XML: %s: quote must be \" or '", who);
711 420 o->quote = q[0];
712 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
713 STRLEN n;
714 429 const char *e = SvPV(v, n);
715 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
716 2 o->encoding = FRX_ENC_UTF8;
717 2 o->encoding_attr = 0;
718 2 *perl_chars = 1;
719 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
720 420 o->encoding = FRX_ENC_UTF16LE;
721 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
722 1 o->encoding = FRX_ENC_UTF16BE;
723 } else {
724 6 int kind = frx_enc_kind_by_name(e, n);
725 6 ▶ if (kind < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (kind < 0) 1 5 6
726 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);
727 5 o->encoding = kind;
728 }
729 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
730 AV *av;
731 I32 n, i;
732 const char **names;
733 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
734 0 croak("File::Raw::XML: %s: preserve must be an arrayref of element names", who);
735 1 av = (AV *)SvRV(v);
736 1 n = (I32)(av_len(av) + 1);
737 1 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 1 0 1
738 1 Newx(names, n, const char *);
739 1 SAVEFREEPV(names);
740 2 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 1 2
741 1 SV **e = av_fetch(av, i, 0);
742 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
743 2 names[i] = SvPV_nolen(*e);
744 }
745 1 o->preserve = names;
746 1 o->n_preserve = (int)n;
747 }
748 } else {
749 1 croak("File::Raw::XML: %s: unknown option '%.*s'", who, (int)klen, k);
750 }
751 }
752 }
753  
754 /* the markup of apex under o, as a +1 SV: bytes, or characters for
755 * encoding => 'perl' */
756 static SV *
757 1734 frx_write_sv(pTHX_ const frx_node *apex, const frx_doc *d, const frx_write_opts *o, int perl_chars, const char *who)
758 {
759 frx_buf b;
760 frx_err e;
761 SV *out;
762 1734 frx_buf_init(&b);
763 1734 ▶ if (!frx_write(apex, d, o, &b, &e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_write(apex, d, o, &b, &e)) { 1 1733 1734
764 1 frx_buf_free(&b);
765 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
766 }
767 1733 ▶ out = newSVpvn(b.p ? b.p : "", b.len);
Branch: 1 branch
BranchTrueFalseTotal
out = newSVpvn(b.p ? b.p : "", b.len); 1733 0 1733
768 1733 frx_buf_free(&b);
769 1733 ▶ if (perl_chars) SvUTF8_on(out);
Branch: 1 branch
BranchTrueFalseTotal
if (perl_chars) SvUTF8_on(out); 1 1732 1733
770 1733 return out;
771 }
772  
773 /* ---- the reader object ---------------------------------------------------
774 *
775 * A File::Raw::XML::Reader is a blessed IV with ext magic whose free hook
776 * frees this record: the reader itself, the resolver state it may hold
777 * (the coderef kept at +1, the confined directory), and durable copies
778 * of the option strings the options HV would otherwise own for one call. */
779  
780 #define FRX_READER_CLASS "File::Raw::XML::Reader"
781  
782 /* The event kinds a caller can name a callback for. FRX_DOCTYPE (7) is
783 * the highest, so a table indexed by kind is eight wide, and the push
784 * form dispatches by indexing it rather than by looking a name up per
785 * event. Returns -1 for a key that is not an event name, which is how
786 * file_xml_events tells a callback from a reader option. */
787 #define FRX_EVENT_KINDS 8
788  
789 static int
790 46 frx_event_kind_named(const char *k, STRLEN n)
791 {
792 46 switch (n) {
793 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
794 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
795 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
796 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
797 14 case 7:
798 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
799 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
800 8 break;
801 0 default: break;
802 }
803 8 return -1;
804 }
805  
806 /* what from_file pulls from the file each time the parser wants more */
807 #define FRX_READER_CHUNK (64 * 1024)
808  
809 typedef struct frx_reader_xs {
810 frx_reader r;
811 frx_resolver_ud ud;
812 SV *cv;
813 char *base;
814 char *encoding;
815 char *dir;
816 /* a reader made by from_file owns a File::Raw chunk handle and pulls
817 * from it in next, in C: -1 when the bytes come from feed instead.
818 * Zero is a valid handle, so this is set after the memset, not by
819 * it. file_chunk.h comes in through XML.xs, before this header. */
820 IV chunk;
821 } frx_reader_xs;
822  
823 static char *
824 49 frx_strdup(const char *s)
825 {
826 49 size_t n = strlen(s);
827 49 char *p = (char *)malloc(n + 1);
828 49 ▶ if (p) memcpy(p, s, n + 1);
Branch: 1 branch
BranchTrueFalseTotal
if (p) memcpy(p, s, n + 1); 49 0 49
829 49 return p;
830 }
831  
832 static void
833 883 frx_reader_xs_free(pTHX_ frx_reader_xs *x)
834 {
835 883 ▶ if (!x) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!x) return; 0 883 883
836 883 ▶ 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 81 883
837 883 frx_reader_free(&x->r);
838 883 ▶ if (x->cv) SvREFCNT_dec(x->cv);
Branch: 1 branch
BranchTrueFalseTotal
if (x->cv) SvREFCNT_dec(x->cv); 0 883 883
839 883 free(x->base);
840 883 free(x->encoding);
841 883 free(x->dir);
842 883 free(x);
843 }
844  
845 /* a reader over the options HV; path is the file it reads, when it is
846 * one (the plugin), for the file resolver and the base */
847 static frx_reader_xs *
848 888 frx_reader_xs_new(pTHX_ HV *opts, const char *path)
849 {
850 frx_opts_ex oe;
851 frx_resolver_ud ud;
852 frx_reader_xs *x;
853 888 frx_opts_from_hv(aTHX_ opts, &oe, &ud, path);
854 883 x = (frx_reader_xs *)malloc(sizeof *x);
855 883 ▶ if (!x) croak("File::Raw::XML: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!x) croak("File::Raw::XML: out of memory"); 0 883 883
856 883 memset(x, 0, sizeof *x);
857 883 x->chunk = -1; /* 0 is a chunk handle, not "no handle" */
858 883 x->ud = ud;
859 883 ▶ 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 883 883
860 883 ▶ 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 883 883
861 883 ▶ if (oe.resolve) oe.resolve_ud = &x->ud;
Branch: 1 branch
BranchTrueFalseTotal
if (oe.resolve) oe.resolve_ud = &x->ud; 0 883 883
862 883 ▶ if (oe.base_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (oe.base_uri) { 0 883 883
863 0 x->base = frx_strdup(oe.base_uri);
864 0 oe.base_uri = x->base;
865 883 ▶ } else if (oe.resolve && path) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (oe.resolve && path) { 0 883 883
} else if (oe.resolve && path) { 0 0 0
866 0 x->base = frx_realpath_as_base(path);
867 0 oe.base_uri = x->base;
868 }
869 883 ▶ if (oe.encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (oe.encoding) { 0 883 883
870 0 x->encoding = frx_strdup(oe.encoding);
871 0 oe.encoding = x->encoding;
872 }
873 883 oe.base.id_attrs = NULL; /* a reader builds no index */
874 883 oe.base.n_id_attrs = 0;
875 883 frx_reader_init(&x->r, &oe);
876 883 return x;
877 }
878  
879 static int
880 857 frx_reader_mg_free(pTHX_ SV *sv, MAGIC *mg)
881 {
882 857 frx_reader_xs *x = (frx_reader_xs *)mg->mg_ptr;
883 PERL_UNUSED_ARG(sv);
884 857 ▶ if (x) {
Branch: 1 branch
BranchTrueFalseTotal
if (x) { 857 0 857
885 857 frx_reader_xs_free(aTHX_ x);
886 857 mg->mg_ptr = NULL;
887 }
888 857 return 0;
889 }
890  
891 static MGVTBL frx_reader_vtbl = { NULL, NULL, NULL, NULL, frx_reader_mg_free, NULL, NULL, NULL };
892  
893 static SV *
894 857 frx_reader_bless(pTHX_ frx_reader_xs *x, const char *class_name)
895 {
896 857 SV *iv = newSViv(0);
897 857 sv_magicext(iv, NULL, PERL_MAGIC_ext, &frx_reader_vtbl, (const char *)x, 0);
898 857 return sv_bless(newRV_noinc(iv), gv_stashpv(class_name, GV_ADD));
899 }
900  
901 static frx_reader_xs *
902 486874 frx_reader_from_sv(pTHX_ SV *sv, const char *meth)
903 {
904 486874 MAGIC *mg = frx_obj_magic(aTHX_ sv, &frx_reader_vtbl);
905 486874 ▶ if (!mg || !mg->mg_ptr)
Branch: 2 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr) 486874 0 486874
if (!mg || !mg->mg_ptr) 0 486874 486874
906 0 croak("File::Raw::XML: %s: the invocant is not a " FRX_READER_CLASS, meth);
907 486874 return (frx_reader_xs *)mg->mg_ptr;
908 }
909  
910 /* the reader's refusal, in the one message shape, over the bytes it
911 * still holds */
912 static void
913 10 frx_reader_croak(pTHX_ frx_reader_xs *x)
914 {
915 char msg[512];
916 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);
917 10 croak("%.*s", (int)n, msg);
918 }
919  
920 /* ---- edits from Perl ----------------------------------------------------
921 *
922 * A node an edit creates is blessed exactly as a parsed one: the same
923 * magic, the document's inner IV as mg_obj at +1, so it holds its
924 * document and a detached node keeps the document alive. A node passed to
925 * an edit must belong to the invocant's document, checked by the inner
926 * IV; one from another document is refused before anything is touched,
927 * and import is the way across. */
928  
929 /* a node of the same document as doc_iv, writable */
930 static frx_node *
931 41 frx_node_arg(pTHX_ SV *sv, SV *doc_iv, const char *meth)
932 {
933 41 SV *their = NULL;
934 41 const frx_node *n = frx_node_from_sv(aTHX_ sv, meth, &their);
935 41 ▶ if (their != doc_iv)
Branch: 1 branch
BranchTrueFalseTotal
if (their != doc_iv) 1 40 41
936 1 croak("File::Raw::XML: %s: the node belongs to another document; use import", meth);
937 40 return (frx_node *)n;
938 }
939  
940 static void
941 26 frx_edit_croak(pTHX_ const char *meth, const frx_err *e)
942 {
943 26 ▶ if (e->offset)
Branch: 1 branch
BranchTrueFalseTotal
if (e->offset) 7 19 26
944 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
945 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
946 }
947  
948 /* the bytes of a string argument, or "" for undef */
949 static const char *
950 168 frx_arg_bytes(pTHX_ SV *sv, STRLEN *len)
951 {
952 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
953 167 return frx_input(aTHX_ sv, len);
954 }
955  
956 /* prefix and local from a name that may carry one colon */
957 static void
958 53 frx_split_arg(const char *name, STRLEN nlen, const char **prefix, STRLEN *plen, const char **local, STRLEN *llen)
959 {
960 53 const char *colon = (const char *)memchr(name, ':', nlen);
961 53 ▶ if (colon) {
Branch: 1 branch
BranchTrueFalseTotal
if (colon) { 20 33 53
962 20 *prefix = name; *plen = (STRLEN)(colon - name);
963 20 *local = colon + 1; *llen = nlen - *plen - 1;
964 } else {
965 33 *prefix = ""; *plen = 0;
966 33 *local = name; *llen = nlen;
967 }
968 53 }
969  
970 /* the current event's doctype as the Document method renders one */
971 static SV *
972 22 frx_doctype_sv(pTHX_ const frx_doctype *dt)
973 {
974 HV *hv;
975 22 ▶ if (!dt) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!dt) return newSV(0); 1 21 22
976 21 hv = newHV();
977 21 (void)hv_stores(hv, "name", frx_str_sv(aTHX_ &dt->name));
978 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
979 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
980 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
981 21 return newRV_noinc((SV *)hv);
982 }
983  
984 #endif /* FRX_OBJ_H */
« include/frx/frx_entity.h ↑ Summary xs/internal.xs »