Codec.xs

filter: Statementstmt 100% 36 / 36 Branchbran 71.4% 30 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.6% 66 / 78 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/sc/sc_codec.h ↑ Summary blib/lib/Struct/Codec.pm »
1 /*
2 * Codec.xs - root XS file for Struct::Codec.
3 *
4 * The perl headers, then the format, the codec, the public ABI header and the
5 * table behind it, then the per-package XS fragments from xs/ via INCLUDE:.
6 *
7 * This file sits at the TOP of the dist rather than under lib/. An XSMULTI
8 * build from lib/Struct/Codec.xs writes its linker export list to
9 * lib/Struct/Codec.def, while the import-library rule Strawberry adds for MinGW
10 * reads $(EXPORT_LIST), which is always $(BASEEXT).def in the top directory -
11 * the two names never meet and dlltool fails the build.
12 *
13 * The rules every header under include/sc/ follows:
14 *
15 * 1. C89 declarations at block top. No VLAs, no designated initialisers, no
16 * %zu, no //.
17 * 2. Every function takes pTHX_ - there is no perl-free half here, because a
18 * codec that builds SVs has nothing to do without an interpreter.
19 * 3. Corrupt input is a croak with a reason and a byte offset, never a crash.
20 * Every read is bounds-checked first and every container is attached to
21 * its parent before it is filled, so a croak frees what was built.
22 */
23  
24 #define PERL_NO_GET_CONTEXT
25 #include "EXTERN.h"
26 #include "perl.h"
27 #include "XSUB.h"
28  
29 #include "sc/sc_format.h" /* the tags and the header: what the bytes mean */
30 #include "sc/sc_codec.h" /* the encoder and the decoder */
31 #include "sc_abi.h" /* the PUBLIC table a consumer resolves */
32 #include "sc/sc_abi_impl.h" /* and what it points at */
33  
34 /* GvCV_set arrived after 5.12, and before it the glob's CV slot was assigned
35 * directly. Without this the import XSUB compiles as an IMPLICIT DECLARATION
36 * on 5.10 and 5.12 - a warning today, an error on a stricter compiler, and a
37 * call through a guessed prototype either way. Found by the docker matrix;
38 * the Mac's 5.42 has the macro and said nothing. */
39 #ifndef GvCV_set
40 # define GvCV_set(gv, cv) (GvCV(gv) = (cv))
41 #endif
42  
43 MODULE = Struct::Codec PACKAGE = Struct::Codec
44  
45 PROTOTYPES: DISABLE
46  
47 # An owned byte string. The UTF-8 flag is off: this is a byte string whatever
48 # the value held, and a caller that treats it as characters gets it wrong.
49 #
50 # struct_encode is the name; encode is the same XSUB under the short name for
51 # a caller who writes Struct::Codec::encode in full. Only the long names are
52 # ever exported, so neither collides with Encode's in a caller's namespace.
53 #
54 # Options follow the value as pairs. The only one is strip_pointers, and an
55 # unknown name is an error rather than a silently ignored typo.
56 SV *
57 struct_encode(value, ...)
58 SV *value
59 ALIAS:
60 encode = 1
61 PREINIT:
62 4952 U32 flags = 0;
63 I32 i;
64 CODE:
65 PERL_UNUSED_VAR(ix);
66 4952 ▶ if (!(items & 1)) croak("Struct::Codec: options come in pairs");
Branch: 1 branch
BranchTrueFalseTotal
if (!(items & 1)) croak("Struct::Codec: options come in pairs"); 1 4951 4952
67 4960 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 10 4950 4960
68 STRLEN n;
69 10 const char *k = SvPV(ST(i), n);
70 10 ▶ if (n == 14 && memEQ(k, "strip_pointers", 14)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n == 14 && memEQ(k, "strip_pointers", 14)) { 9 1 10
if (n == 14 && memEQ(k, "strip_pointers", 14)) { 9 0 9
71 11 ▶ if (SvTRUE(ST(i + 1))) flags |= SC_STRIP_POINTERS;
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(ST(i + 1))) flags |= SC_STRIP_POINTERS; 7 2 9
72 2 else flags &= ~SC_STRIP_POINTERS;
73 }
74 else {
75 1 croak("Struct::Codec: unknown option '%" SVf "'", SVfARG(ST(i)));
76 }
77 }
78 4950 RETVAL = sc_encode_flags(aTHX_ value, flags);
79 OUTPUT:
80 RETVAL
81  
82 # The value back. Croaks on anything that is not a stream this build wrote:
83 # the message names the reason and the byte it was found at.
84 SV *
85 struct_decode(bytes)
86 SV *bytes
87 ALIAS:
88 decode = 1
89 PREINIT:
90 const char *p;
91 STRLEN len;
92 CODE:
93 PERL_UNUSED_VAR(ix);
94 4648 p = SvPV(bytes, len);
95 4648 RETVAL = sc_decode(aTHX_ p, len);
96 OUTPUT:
97 RETVAL
98  
99 # `use Struct::Codec qw(struct_encode struct_decode)`. Exports on request only,
100 # and only those two names: an unknown name is an error at compile time of the
101 # caller, not a silent no-op. The caller's package is the one compiling the
102 # `use`, which is what PL_curcop names while import runs.
103 void
104 import(class, ...)
105 SV *class
106 PREINIT:
107 const char *pkg;
108 I32 i;
109 CODE:
110 PERL_UNUSED_VAR(class);
111 37 ▶ pkg = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
pkg = CopSTASHPV(PL_curcop); 37 0 37
pkg = CopSTASHPV(PL_curcop); 37 0 37
pkg = CopSTASHPV(PL_curcop); 37 0 37
pkg = CopSTASHPV(PL_curcop); 0 37 37
pkg = CopSTASHPV(PL_curcop); 0 0 0
pkg = CopSTASHPV(PL_curcop); 37 0 37
pkg = CopSTASHPV(PL_curcop); 0 37 37
112 61 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 25 36 61
113 STRLEN nlen;
114 25 const char *name = SvPV(ST(i), nlen);
115 GV *gv;
116 CV *cv;
117 25 ▶ if (!(nlen == 13 && memEQ(name, "struct_encode", 13))
Branch: 2 branches
BranchTrueFalseTotal
if (!(nlen == 13 && memEQ(name, "struct_encode", 13)) 24 1 25
if (!(nlen == 13 && memEQ(name, "struct_encode", 13)) 12 12 24
118 13 ▶ && !(nlen == 13 && memEQ(name, "struct_decode", 13)))
Branch: 2 branches
BranchTrueFalseTotal
&& !(nlen == 13 && memEQ(name, "struct_decode", 13))) 12 1 13
&& !(nlen == 13 && memEQ(name, "struct_decode", 13))) 0 12 12
119 1 croak("Struct::Codec does not export '%" SVf "'", SVfARG(ST(i)));
120 24 ▶ cv = get_cv(name[7] == 'e' ? "Struct::Codec::struct_encode"
Branch: 1 branch
BranchTrueFalseTotal
cv = get_cv(name[7] == 'e' ? "Struct::Codec::struct_encode" 12 12 24
121 : "Struct::Codec::struct_decode", 0);
122 24 gv = gv_fetchpv(Perl_form(aTHX_ "%s::%s", pkg, name), GV_ADD, SVt_PVCV);
123 24 SvREFCNT_inc_simple_void_NN((SV *)cv);
124 24 GvCV_set(gv, cv);
125 24 GvIMPORTED_CV_on(gv);
126 }
127  
128 # Private. Compiles a pattern with RXf_PMf_* flags for the decoder, which
129 # calls it under G_EVAL: that is the one clean way to catch the regexp
130 # engine's croak from C, so a pattern that will not compile is reported with
131 # the byte it was found at like every other refusal. A reference to the
132 # compiled regexp, unblessed; the decoder blesses it.
133 SV *
134 _regcomp(pattern, flags)
135 SV *pattern
136 UV flags
137 PREINIT:
138 REGEXP *rx;
139 CODE:
140 #if SC_HAVE_REGEXP
141 232 rx = pregcomp(pattern, (U32)flags);
142 230 ▶ if (!rx) croak("Struct::Codec: regexp did not compile");
Branch: 1 branch
BranchTrueFalseTotal
if (!rx) croak("Struct::Codec: regexp did not compile"); 0 230 230
143 230 RETVAL = newRV_noinc((SV *)rx);
144 #else
145 PERL_UNUSED_VAR(pattern);
146 PERL_UNUSED_VAR(flags);
147 rx = NULL;
148 croak("Struct::Codec: a regexp needs perl 5.12 or later");
149 RETVAL = &PL_sv_undef;
150 #endif
151 OUTPUT:
152 RETVAL
153  
154 # Private, for the tests: encode_to into a buffer of exactly `cap` bytes, as a
155 # fixed-slot consumer does through the ABI. (bytes, need) when it fit, (undef,
156 # need) when it did not.
157 void
158 _encode_to(value, cap)
159 SV *value
160 UV cap
161 PREINIT:
162 SV *buf;
163 75 STRLEN need = 0, got;
164 PPCODE:
165 75 buf = sv_2mortal(newSV((STRLEN)cap + 1));
166 75 got = sc_encode_to(aTHX_ value, SvPVX(buf), (STRLEN)cap, &need);
167 74 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 74 74
168 74 ▶ if (got) {
Branch: 1 branch
BranchTrueFalseTotal
if (got) { 28 46 74
169 28 SvPOK_only(buf);
170 28 SvCUR_set(buf, got);
171 28 SvPVX(buf)[got] = '\0';
172 28 PUSHs(buf);
173 }
174 else {
175 46 PUSHs(&PL_sv_undef);
176 }
177 74 mPUSHu((UV)need);
178  
179 INCLUDE: xs/abi.xs
« include/sc/sc_codec.h ↑ Summary blib/lib/Struct/Codec.pm »