include/sc/sc_codec.h

filter: Statementstmt 88.1% 635 / 720 Branchbran 68.1% 522 / 766 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.8% 1157 / 1486 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/abi.xs ↑ Summary Codec.xs »
1 #ifndef SC_CODEC_H
2 #define SC_CODEC_H
3  
4 /* HeUTF8 arrived in 5.10.1 and the floor is 5.010: an OmniOS smoker's 5.10.0
5 * refused Frozen for want of it and would refuse sc_enc_key the same way. This
6 * is ppport.h's definition, taken only where perl.h has not provided one. */
7 #ifndef HeUTF8
8 # define HeUTF8(he) ((HeKLEN(he) == HEf_SVKEY) \
9 ? SvUTF8(HeKEY_sv(he)) : (U32)HeKUTF8(he))
10 #endif
11  
12 /* sc_codec.h - the encoder and the decoder. Needs perl.h and sc_format.h.
13 *
14 * ---- the encoder's seen table is the design ---------------------------------
15 *
16 * A round-trip codec has to notice a reference it has already written. The
17 * obvious way is a pointer-keyed hash of every SV met, and it costs a hash
18 * insert per value, which on a five-key hash is more than the encoding. The
19 * observation that removes it: a referent reachable only once has a refcount
20 * of exactly one from the structure being encoded, so ONLY an SV with a
21 * refcount above one can ever be met twice, and only those go into the table.
22 * A plain tree does no table work at all.
23 *
24 * Immortals - PL_sv_undef, PL_sv_yes, PL_sv_no, PL_sv_zero - have enormous
25 * refcounts and an identity nobody wants preserved; they are written by kind
26 * and never tracked.
27 *
28 * ---- what is tracked, and under which offset -----------------------------------
29 *
30 * Two things can be shared: a REFERENT (two references to one hash) and a slot
31 * SCALAR (the same SV in two array elements, which aliasing can produce). Both
32 * are keyed by address in one table, and each entry records which it is:
33 *
34 * - a referent is entered under the offset of the REF or OBJECT tag that
35 * introduced it. Meeting it again behind another reference writes REFP to
36 * that offset (a new reference to it); meeting it as a slot value writes
37 * ALIAS to that offset (the scalar itself, placed in the slot).
38 * - a slot scalar is entered under its own value tag's offset. Meeting it
39 * again as a slot value writes ALIAS; meeting it behind a reference writes
40 * REFP (a reference to it).
41 *
42 * A reference SV that is itself in two slots is NOT tracked as a scalar; it
43 * comes back as two references to one referent, which is what a reader of the
44 * structure sees anyway. That is the one identity this format gives up, and
45 * the POD says so.
46 *
47 * ---- the decoder builds nothing it cannot free ---------------------------------
48 *
49 * Every container is stored into its parent, or made the mortal root, BEFORE
50 * its elements are decoded, and a scalar is made, attached, then filled the
51 * same way. A croak anywhere inside then frees the whole partial structure
52 * through the root's mortality: nothing is leaked and nothing is unwound by
53 * hand. The one thing that cannot be filled in place is an ALIAS - the slot
54 * must hold an SV that already exists - so sc_dec_value returns that SV when it
55 * meets one and the parent swaps it into the slot it had already attached.
56 *
57 * The rules for input that lies are numbered in plan_struct_codec/04, and each
58 * is marked where it is enforced below with its number.
59 */
60  
61 #include "sc/sc_format.h"
62  
63 #ifdef SvIsBOOL
64 # define SC_HAVE_BOOL 1
65 #else
66 # define SC_HAVE_BOOL 0
67 #endif
68  
69 /* A regexp became its own SV type in 5.12, and SVt_REGEXP is an enumerator
70 * rather than a macro, so this is a version test and not an #ifdef. */
71 #if PERL_REVISION > 5 || (PERL_REVISION == 5 && PERL_VERSION >= 12)
72 # define SC_HAVE_REGEXP 1
73 #else
74 # define SC_HAVE_REGEXP 0
75 #endif
76 /* The character-set modifiers, /a /aa /l /u /d, came in 5.14, and their
77 * names are enumerators too. */
78 #if PERL_REVISION > 5 || (PERL_REVISION == 5 && PERL_VERSION >= 14)
79 # define SC_HAVE_CHARSET 1
80 #else
81 # define SC_HAVE_CHARSET 0
82 #endif
83  
84 /* A reference was its own SV type until 5.12 merged it into SVt_IV. An SV
85 * upgraded to SVt_IV with ROK set is not a reference on 5.10: `ref` answers
86 * the empty string and every dereference finds nothing. */
87 #if PERL_REVISION > 5 || (PERL_REVISION == 5 && PERL_VERSION >= 12)
88 # define SC_SVt_REF SVt_IV
89 #else
90 # define SC_SVt_REF SVt_RV
91 #endif
92  
93 #ifndef HvNAMEUTF8
94 # define HvNAMEUTF8(hv) 0
95 #endif
96 #ifndef HvNAMELEN_get
97 # define HvNAMELEN_get(hv) (HvNAME(hv) ? strlen(HvNAME(hv)) : 0)
98 #endif
99  
100 #define SC_UV_BITS ((int)(sizeof(UV) * 8))
101  
102 /* ---- floats on the wire ------------------------------------------------------
103 *
104 * An IEEE-754 double in little-endian order, so a stream reads the same on every
105 * machine. Little-endian is the common case and there the conversion is a
106 * memcpy; a big-endian host swaps. perl.h defines BYTEORDER on every build.
107 *
108 * A perl whose NV is wider than a double writes SC_T_NV_STR instead for the
109 * values these 8 bytes cannot hold - see below. */
110 typedef char sc_assert_double[(sizeof(double) == SC_NV_BYTES) ? 1 : -1];
111  
112 #if defined(BYTEORDER) && (BYTEORDER == 0x4321 || BYTEORDER == 0x87654321)
113 # define SC_BIG_ENDIAN 1
114 #else
115 # define SC_BIG_ENDIAN 0
116 #endif
117  
118 1084 static void sc_nv_to_wire(NV nv, unsigned char *out) {
119 1084 double dv = (double)nv;
120 1084 memcpy(out, &dv, SC_NV_BYTES);
121 #if SC_BIG_ENDIAN
122 {
123 int i;
124 for (i = 0; i < SC_NV_BYTES / 2; i++) {
125 unsigned char t = out[i];
126 out[i] = out[SC_NV_BYTES - 1 - i];
127 out[SC_NV_BYTES - 1 - i] = t;
128 }
129 }
130 #endif
131 1084 }
132  
133 1749 static NV sc_nv_from_wire(const unsigned char *in) {
134 double dv;
135 #if SC_BIG_ENDIAN
136 unsigned char tmp[SC_NV_BYTES];
137 int i;
138 for (i = 0; i < SC_NV_BYTES; i++) tmp[i] = in[SC_NV_BYTES - 1 - i];
139 memcpy(&dv, tmp, SC_NV_BYTES);
140 #else
141 1749 memcpy(&dv, in, SC_NV_BYTES);
142 #endif
143 1749 return (NV)dv;
144 }
145  
146 /* ---- an NV that a double cannot hold -------------------------------------------
147 *
148 * Written as its decimal digits, not as the native bytes, because "an NV wider
149 * than a double" is three different types across the perls that exist - the x87
150 * 80-bit extended one, IEEE binary128, and on some PowerPC builds a pair of
151 * doubles - and the bytes of one are not readable as another. Digits are
152 * readable by all three, and by a plain double perl, which reads them to the
153 * nearest double and so lands on the same value the 8-byte form would have
154 * given it.
155 *
156 * NV_DIG + 3 is DECIMAL_DIG for the NV this perl was built with: 21 digits for
157 * x87 extended, 36 for binary128. That is the shortest precision at which a
158 * binary float of that width survives the trip out to decimal and back, which
159 * t/25-wide-nv.t asserts directly rather than trusting the arithmetic.
160 *
161 * Both ends of that trip have to be the RIGHT primitive, and the obvious
162 * spelling of each is wrong on one of the two wide widths. Gconvert is wrong on
163 * both - config.h defines it as a plain "%.*g" even on a quadmath perl, where it
164 * reads a __float128 through a double conversion - and perl's own sv.c goes
165 * around it for that reason. What replaces it:
166 *
167 * OUT. my_snprintf with the precision written INTO the format string, never
168 * passed as "%.*". Perl_my_snprintf hands any format ending "Qg" straight to
169 * quadmath_snprintf with ONE variadic argument (util.c, guarded by
170 * quadmath_format_valid, which does not reject a "*"), so a "*" there takes
171 * the precision from a slot nothing was pushed into. 0.03 wrote "%.*" NVgf
172 * and on the quadmath smokers printed ONE significant digit - a third came
173 * back 0.3 - while on the 32-bit one the garbage precision overran the buffer
174 * and perl panicked "my_snprintf buffer overflow". Perl's sv.c builds the
175 * digits into the format for the same reason; sc_nv_fmt is that, in one line.
176 *
177 * BACK. strtod's family, not Atof. Atof is perl's own numifier and only
178 * became correctly rounded in 5.30: before that, on a perl whose NV is a long
179 * double, my_atof accumulates the digits and scales by a power of ten built
180 * from repeated squaring (S_mulexp10 in numeric.c), which is a ULP or two out
181 * at the full width of the NV, so the encoder's own DECIMAL_DIG digits did not
182 * read back as the number they were printed from. Perl fixed that for itself
183 * in 5.30 by numifying through strtod (perl RT #41202); every
184 * -Duselongdouble smoker below it failed eight subtests of t/25-wide-nv.t on
185 * 0.03. A quadmath perl was never affected - my_atof3 has read through
186 * strtoflt128 since USE_QUADMATH existed - which is why the two widths
187 * reported different failures from the one tag.
188 *
189 * The radix is always '.' on the wire. my_snprintf formats under the locale
190 * perl would print a number with, so inside "use locale" with a comma radix it
191 * writes 0,333; the stream promises to say the same thing on every machine, and
192 * the reader below parses a '.'.
193 *
194 * All of this is compiled out where NV is a double, so that encoder is the same
195 * function it was before, byte for byte. */
196 #if defined(NVSIZE)
197 # define SC_NV_WIDE (NVSIZE > 8)
198 #elif defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
199 # define SC_NV_WIDE 1
200 #else
201 # define SC_NV_WIDE 0
202 #endif
203  
204 #if SC_NV_WIDE
205  
206 /* True when the 8-byte form would come back as a different number.
207 *
208 * A NaN is answered first because nv != nv is true of it, which would otherwise
209 * send every NaN down the decimal path, and a NaN has no decimal spelling that
210 * reads back as a NaN on every platform; narrowing one loses nothing anyway. An
211 * infinity and a negative zero convert exactly and keep the 8-byte path.
212 *
213 * The double is volatile so the narrowing happens to a real 64-bit location: on
214 * an x87 build without it the compiler may keep the value in an 80-bit register
215 * and compare it against itself, which is always equal and would answer no. */
216 static int sc_nv_needs_str(NV nv) {
217 volatile double dv;
218 /* NVgf is a bare "g" on a -Duselongdouble build whose Configure found no
219 * long double format, and then there is no way to print the NV at all:
220 * "%g" would read a double out of a long double argument. Narrowing is
221 * wrong, garbage digits are worse, so such a build keeps the 8-byte form.
222 * Perl's own sv.c carries a FIXME for the same hole. Folded at compile
223 * time; every build anyone smokes has the format. */
224 if (sizeof(NVgf) == 2) return 0;
225 if (nv != nv) return 0;
226 dv = (double)nv;
227 return (NV)dv != nv;
228 }
229  
230 /* "%.21Lg" or "%.36Qg", whichever this build is. NV_DIG is a float.h value and
231 * not a token the preprocessor can paste into a literal, so the precision is
232 * printed in; THIS format has no float conversion of its own, which is what
233 * keeps it off quadmath_snprintf's one-argument path. */
234 static void sc_nv_fmt(char *fmt, STRLEN cap) {
235 my_snprintf(fmt, cap, "%s%d%s", "%.", (int)(NV_DIG + 3), NVgf);
236 }
237  
238 /* Whatever the locale wrote for a radix becomes a single '.'. In the C locale,
239 * which is where perl keeps LC_NUMERIC unless "use locale" is in effect, there
240 * is nothing to do. A radix is more than one byte in some locales, so the run
241 * is replaced and not the byte, and %g of a finite number has at most one.
242 * Returns the length, which the replacement can shorten. */
243 static int sc_nv_radix(char *s, int n) {
244 int i, j = 0;
245 for (i = 0; i < n; i++) {
246 char c = s[i];
247 if (isDIGIT(c) || c == '-' || c == '+' || c == 'e' || c == 'E') continue;
248 for (j = i + 1; j < n; j++) {
249 c = s[j];
250 if (isDIGIT(c) || c == '-' || c == '+' || c == 'e' || c == 'E') break;
251 }
252 s[i] = '.';
253 if (j > i + 1) {
254 Move(s + j, s + i + 1, n - j, char);
255 n -= j - i - 1;
256 }
257 break;
258 }
259 s[n] = '\0';
260 return n;
261 }
262  
263 #else
264  
265 #define sc_nv_needs_str(nv) (0)
266  
267 #endif
268  
269 /* The parser named directly, because Atof is not one below 5.30 (above). Where
270 * a build has no wide parser Atof is still all there is, and it is at least as
271 * wide as the NV - a bare strtod there would narrow, which is the bug 0.02 was
272 * written to fix. Reached on a double perl too, reading a wide perl's stream. */
273 #if defined(USE_QUADMATH)
274 # define SC_NV_FROM_STR(s) strtoflt128((s), (char **)NULL)
275 #elif defined(USE_LONG_DOUBLE) && defined(HAS_STRTOLD)
276 # define SC_NV_FROM_STR(s) strtold((s), (char **)NULL)
277 #elif !SC_NV_WIDE && defined(HAS_STRTOD)
278 # define SC_NV_FROM_STR(s) strtod((s), (char **)NULL)
279 #else
280 # define SC_NV_FROM_STR(s) Atof(s)
281 #endif
282  
283 /* ============================================================================
284 * the encoder
285 * ==========================================================================*/
286  
287 typedef struct {
288 SV *sv; /* tracked because its refcount is above one */
289 STRLEN off; /* the tag a REFP or ALIAS names */
290 int isref; /* entered as a referent (under a REF tag) or as a slot */
291 } sc_seen;
292  
293 /* ---- everything the encoder allocates is a mortal ----------------------------
294 *
295 * The output is built INSIDE the SV that will be returned, so there is no
296 * private buffer and no copy at the end, and the seen table lives in the
297 * body of a mortal SV. A croak anywhere then frees both at the next FREETMPS
298 * with no destructor to register: measured, the ENTER/SAVEDESTRUCTOR pair, the
299 * malloc and the final copy were most of the 86ns it took to encode one
300 * integer, against 34ns to decode it. */
301 /* struct_encode(..., strip_pointers => 1): a pointer object is written as
302 * undef instead of refused. See sc_is_pointer_obj. */
303 #define SC_STRIP_POINTERS 1u
304  
305 typedef struct {
306 char *buf;
307 STRLEN len; /* bytes written, or that WOULD have been in a fixed buffer */
308 STRLEN cap;
309 SV *bufsv; /* the SV `buf` points into, NULL for a fixed buffer */
310 int fixed; /* encode_to: never grow, keep counting past cap */
311 U32 flags; /* SC_STRIP_POINTERS */
312 sc_seen *seen; /* open addressing keyed on the SV's address */
313 STRLEN nseen;
314 STRLEN seen_cap; /* a power of two, 0 until the first shared SV */
315 int depth;
316 } sc_enc;
317  
318 /* Room for n more bytes. In a fixed buffer the length keeps counting past the
319 * capacity so the caller learns what would have fit, and nothing is written
320 * once it is exceeded. */
321 10884690 static void sc_enc_grow(pTHX_ sc_enc *e, STRLEN n) {
322 10884690 STRLEN want = e->len + n;
323 10884690 ▶ if (want <= e->cap || e->fixed) return;
Branch: 2 branches
BranchTrueFalseTotal
if (want <= e->cap || e->fixed) return; 9203 10875487 10884690
if (want <= e->cap || e->fixed) return; 938 8265 9203
324 8265 ▶ if (!e->bufsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!e->bufsv) { 2166 6099 8265
325 /* Outgrowing the stack buffer: the first and usually only allocation,
326 * a mortal so a croak from here on frees it. */
327 2166 e->bufsv = sv_2mortal(newSV(want * 2 + 1));
328 2166 SvPOK_only(e->bufsv);
329 2166 memcpy(SvPVX(e->bufsv), e->buf, e->len);
330 2166 e->buf = SvPVX(e->bufsv);
331 }
332 else {
333 6099 ▶ e->buf = SvGROW(e->bufsv, want * 2 + 1);
Branch: 2 branches
BranchTrueFalseTotal
e->buf = SvGROW(e->bufsv, want * 2 + 1); 6099 0 6099
e->buf = SvGROW(e->bufsv, want * 2 + 1); 6099 0 6099
334 }
335 8265 e->cap = SvLEN(e->bufsv) - 1;
336 }
337  
338 /* Below this the encoder allocates nothing at all until the one SV it
339 * returns. Every fixture in the benchmark fits it. */
340 #define SC_ENC_STACK 512
341  
342 #define SC_PUT(e, c) \
343 do { \
344 sc_enc_grow(aTHX_ (e), 1); \
345 if ((e)->len < (e)->cap) (e)->buf[(e)->len] = (char)(c); \
346 (e)->len++; \
347 } while (0)
348  
349 612024 static void sc_enc_bytes(pTHX_ sc_enc *e, const char *p, STRLEN n) {
350 612024 sc_enc_grow(aTHX_ e, n);
351 612024 ▶ if (e->len + n <= e->cap) memcpy(e->buf + e->len, p, n);
Branch: 1 branch
BranchTrueFalseTotal
if (e->len + n <= e->cap) memcpy(e->buf + e->len, p, n); 611690 334 612024
352 334 ▶ else if (e->len < e->cap) memcpy(e->buf + e->len, p, e->cap - e->len);
Branch: 1 branch
BranchTrueFalseTotal
else if (e->len < e->cap) memcpy(e->buf + e->len, p, e->cap - e->len); 9 325 334
353 612024 e->len += n;
354 612024 }
355  
356 4175970 static void sc_enc_varint(pTHX_ sc_enc *e, UV v) {
357 5911990 ▶ while (v >= 0x80) { SC_PUT(e, (unsigned char)(v | 0x80)); v >>= 7; }
Branch: 2 branches
BranchTrueFalseTotal
while (v >= 0x80) { SC_PUT(e, (unsigned char)(v | 0x80)); v >>= 7; } 1736018 2 1736020
while (v >= 0x80) { SC_PUT(e, (unsigned char)(v | 0x80)); v >>= 7; } 1736020 4175970 5911990
358 4175970 ▶ SC_PUT(e, (unsigned char)v);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, (unsigned char)v); 4175711 259 4175970
359 4175970 }
360  
361 /* A key or a class name: one varint of (len << 1 | utf8), then the bytes. */
362 606702 static void sc_enc_key(pTHX_ sc_enc *e, const char *p, STRLEN n, int utf8) {
363 606702 ▶ sc_enc_varint(aTHX_ e, ((UV)n << 1) | (utf8 ? 1u : 0u));
Branch: 1 branch
BranchTrueFalseTotal
sc_enc_varint(aTHX_ e, ((UV)n << 1) | (utf8 ? 1u : 0u)); 1270 605432 606702
364 606702 sc_enc_bytes(aTHX_ e, p, n);
365 606702 }
366  
367 4238 static void sc_enc_str(pTHX_ sc_enc *e, const char *p, STRLEN n, int utf8) {
368 4238 ▶ if (n <= SC_SHORT_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= SC_SHORT_MAX) { 3427 811 4238
369 3427 ▶ SC_PUT(e, (utf8 ? SC_T_SHORT_UTF8 : SC_T_SHORT_BYTES) | (unsigned char)n);
Branch: 2 branches
BranchTrueFalseTotal
SC_PUT(e, (utf8 ? SC_T_SHORT_UTF8 : SC_T_SHORT_BYTES) | (unsigned char)n); 3268 159 3427
SC_PUT(e, (utf8 ? SC_T_SHORT_UTF8 : SC_T_SHORT_BYTES) | (unsigned char)n); 158 3110 3268
370 }
371 else {
372 811 ▶ SC_PUT(e, utf8 ? SC_T_UTF8 : SC_T_BYTES);
Branch: 2 branches
BranchTrueFalseTotal
SC_PUT(e, utf8 ? SC_T_UTF8 : SC_T_BYTES); 806 5 811
SC_PUT(e, utf8 ? SC_T_UTF8 : SC_T_BYTES); 473 333 806
373 811 sc_enc_varint(aTHX_ e, (UV)n);
374 }
375 4238 sc_enc_bytes(aTHX_ e, p, n);
376 4238 }
377  
378 /* ---- the seen table --------------------------------------------------------
379 *
380 * Open addressing on the address, mixed so consecutive arena slots do not all
381 * land in one run. Consulted only for an SV whose refcount is above one, so on
382 * most encodes it is never even allocated. */
383 105870 static UV sc_addr_mix(const SV *sv) {
384 105870 UV h = PTR2UV(sv) >> 4;
385 105870 h ^= h >> 17; h *= (UV)0x9E3779B1u; h ^= h >> 13;
386 105870 return h;
387 }
388  
389 717766 static sc_seen *sc_seen_find(sc_enc *e, const SV *sv) {
390 UV i, mask;
391 717766 ▶ if (!e->seen_cap) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e->seen_cap) return NULL; 616294 101472 717766
392 101472 mask = e->seen_cap - 1;
393 102897 ▶ for (i = sc_addr_mix(sv) & mask; e->seen[i].sv; i = (i + 1) & mask)
Branch: 1 branch
BranchTrueFalseTotal
for (i = sc_addr_mix(sv) & mask; e->seen[i].sv; i = (i + 1) & mask) 100215 2682 102897
394 100215 ▶ if (e->seen[i].sv == sv) return &e->seen[i];
Branch: 1 branch
BranchTrueFalseTotal
if (e->seen[i].sv == sv) return &e->seen[i]; 98790 1425 100215
395 2682 return NULL;
396 }
397  
398 static void sc_seen_put(pTHX_ sc_enc *e, SV *sv, STRLEN off, int isref);
399  
400 /* The table's storage is the body of a mortal SV, so a croak frees it. The old
401 * table is simply left to its own mortality when a bigger one replaces it. */
402 2552 static void sc_seen_grow(pTHX_ sc_enc *e) {
403 2552 sc_seen *old = e->seen;
404 2552 STRLEN oldcap = e->seen_cap, i;
405 SV *holder;
406 2552 ▶ e->seen_cap = oldcap ? oldcap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
e->seen_cap = oldcap ? oldcap * 2 : 8; 126 2426 2552
407 2552 holder = sv_2mortal(newSV(e->seen_cap * sizeof(sc_seen)));
408 2552 e->seen = (sc_seen *)SvPVX(holder);
409 2552 memset(e->seen, 0, e->seen_cap * sizeof(sc_seen));
410 2552 e->nseen = 0;
411 4424 ▶ for (i = 0; i < oldcap; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < oldcap; i++) 1872 2552 4424
412 1872 ▶ if (old[i].sv) sc_seen_put(aTHX_ e, old[i].sv, old[i].off, old[i].isref);
Branch: 1 branch
BranchTrueFalseTotal
if (old[i].sv) sc_seen_put(aTHX_ e, old[i].sv, old[i].off, old[i].isref); 936 936 1872
413 2552 }
414  
415 4398 static void sc_seen_put(pTHX_ sc_enc *e, SV *sv, STRLEN off, int isref) {
416 UV i, mask;
417 4398 ▶ if ((e->nseen + 1) * 2 > e->seen_cap) sc_seen_grow(aTHX_ e);
Branch: 1 branch
BranchTrueFalseTotal
if ((e->nseen + 1) * 2 > e->seen_cap) sc_seen_grow(aTHX_ e); 2552 1846 4398
418 4398 mask = e->seen_cap - 1;
419 5053 ▶ for (i = sc_addr_mix(sv) & mask; e->seen[i].sv; i = (i + 1) & mask) ;
Branch: 1 branch
BranchTrueFalseTotal
for (i = sc_addr_mix(sv) & mask; e->seen[i].sv; i = (i + 1) & mask) ; 655 4398 5053
420 4398 e->seen[i].sv = sv;
421 4398 e->seen[i].off = off;
422 4398 e->seen[i].isref = isref;
423 4398 e->nseen++;
424 4398 }
425  
426 /* Mark the earlier tag as referenced again. It is inside the buffer already,
427 * so this works in a fixed buffer too - unless that tag fell past the
428 * capacity, in which case nothing was written there and the caller is
429 * refusing the whole value anyway. */
430 98790 static void sc_enc_track(sc_enc *e, STRLEN off) {
431 98790 ▶ if (off < e->cap) e->buf[off] = (char)((unsigned char)e->buf[off] | SC_TRACK);
Branch: 1 branch
BranchTrueFalseTotal
if (off < e->cap) e->buf[off] = (char)((unsigned char)e->buf[off] | SC_TRACK); 98747 43 98790
432 98790 }
433  
434 0 static void sc_enc_croak_type(pTHX_ const char *what) {
435 0 croak("Struct::Codec: cannot encode a %s", what);
436 }
437  
438 1555 static int sc_is_regexp(pTHX_ SV *sv) {
439 #if SC_HAVE_REGEXP
440 1555 ▶ if (SvTYPE(sv) == SVt_REGEXP) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(sv) == SVt_REGEXP) return 1; 469 1086 1555
441 #endif
442 1086 ▶ return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp");
Branch: 15 branches
BranchTrueFalseTotal
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 675 411 1086
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 675 0 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 675 0 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 0 675 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 0 0 0
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 675 0 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 0 675 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 675 0 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 675 0 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 675 0 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 0 675 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 0 0 0
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 675 0 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 0 675 675
return SvOBJECT(sv) && HvNAME(SvSTASH(sv)) && strEQ(HvNAME(SvSTASH(sv)), "Regexp"); 0 675 675
443 }
444  
445 /* The magic that ties a container or a scalar, or NULL. Found by kind rather
446 * than by SvRMAGICAL alone, because a scalar with other magic is not tied. */
447 4345345 static MAGIC *sc_tie_magic(pTHX_ SV *sv) {
448 4345345 ▶ if (!SvRMAGICAL(sv)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvRMAGICAL(sv)) return NULL; 4345293 52 4345345
449 52 ▶ if (SvTYPE(sv) == SVt_PVAV || SvTYPE(sv) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(sv) == SVt_PVAV || SvTYPE(sv) == SVt_PVHV) 48 4 52
if (SvTYPE(sv) == SVt_PVAV || SvTYPE(sv) == SVt_PVHV) 9 39 48
450 13 return mg_find(sv, PERL_MAGIC_tied);
451 39 return mg_find(sv, PERL_MAGIC_tiedscalar);
452 }
453  
454 /* An object whose whole state is one integer, in a class whose DESTROY is an
455 * XSUB, is the shape the typemap gives a handle: T_PTROBJ stores the address
456 * of a C struct as the IV and the generated DESTROY frees it. Compress::Raw::
457 * Zlib's streams and XML::LibXML's nodes are this shape. The SV itself says
458 * nothing, which is why Storable carries one happily unless the class
459 * installs a STORABLE_freeze that croaks; a faithful copy in another process
460 * hands its DESTROY an address that was never allocated there, and a copy in
461 * the same process frees the original's struct under it. So the encoder
462 * refuses the shape (or, asked to, writes undef) and the decoder refuses to
463 * bless it, and no DESTROY ever sees the integer.
464 *
465 * The DESTROY must be XS: a blessed integer with a DESTROY written in Perl is
466 * an ordinary object - Tie::StdScalar's tie object is exactly that, and its
467 * DESTROY only undefs the scalar - and Perl code cannot free C memory, so a
468 * Perl DESTROY that does so calls an XSUB to do it, which is the one shape
469 * this cannot see. A class without a DESTROY frees nothing and is carried as
470 * before; a string, a float, a reference or a container in any class is
471 * data, not an address. The DESTROY is looked up through the class's
472 * inheritance, as perl will look it up. */
473 #ifndef CvISXSUB
474 # define CvISXSUB(cv) (CvXSUB(cv) != NULL)
475 #endif
476  
477 /* An address that has been printed still has a public POK on perls before
478 * 5.36, where caching the digits set it, so POK alone cannot say "string".
479 * The PV that spells the integer back is the cached spelling and says nothing
480 * the IV did not; any other PV is a string, and a string is data. The cost is
481 * one ambiguous shape on those perls: a blessed "7" that has also been used
482 * as a number carries the same flags and the same bytes as a printed 7, and
483 * is refused with it. */
484 2 static int sc_pv_spells_the_iv(pTHX_ SV *sv) {
485 char buf[64];
486 int blen;
487 PERL_UNUSED_CONTEXT;
488 0 blen = SvIsUV(sv) ? my_snprintf(buf, sizeof(buf), "%" UVuf, SvUVX(sv))
489 2 ▶ : my_snprintf(buf, sizeof(buf), "%" IVdf, SvIVX(sv));
Branch: 1 branch
BranchTrueFalseTotal
: my_snprintf(buf, sizeof(buf), "%" IVdf, SvIVX(sv)); 0 2 2
490 2 ▶ return blen > 0 && (STRLEN)blen == SvCUR(sv)
Branch: 1 branch
BranchTrueFalseTotal
return blen > 0 && (STRLEN)blen == SvCUR(sv) 1 1 2
491 4 ▶ && memEQ(SvPVX_const(sv), buf, (STRLEN)blen);
Branch: 2 branches
BranchTrueFalseTotal
&& memEQ(SvPVX_const(sv), buf, (STRLEN)blen); 2 0 2
&& memEQ(SvPVX_const(sv), buf, (STRLEN)blen); 1 0 1
492 }
493  
494 1767 static int sc_is_pointer_obj(pTHX_ SV *sv, HV *stash) {
495 GV *gv;
496 1767 ▶ if (SvTYPE(sv) >= SVt_PVAV || SvTYPE(sv) == SVt_PVGV) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(sv) >= SVt_PVAV || SvTYPE(sv) == SVt_PVGV) return 0; 472 1295 1767
if (SvTYPE(sv) >= SVt_PVAV || SvTYPE(sv) == SVt_PVGV) return 0; 0 472 472
497 472 ▶ if (!SvIOK(sv) || SvNOK(sv) || SvROK(sv)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvIOK(sv) || SvNOK(sv) || SvROK(sv)) return 0; 85 387 472
if (!SvIOK(sv) || SvNOK(sv) || SvROK(sv)) return 0; 85 0 85
if (!SvIOK(sv) || SvNOK(sv) || SvROK(sv)) return 0; 0 85 85
498 85 ▶ if (SvPOK(sv) && !sc_pv_spells_the_iv(aTHX_ sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(sv) && !sc_pv_spells_the_iv(aTHX_ sv)) return 0; 2 83 85
if (SvPOK(sv) && !sc_pv_spells_the_iv(aTHX_ sv)) return 0; 1 1 2
499 84 gv = gv_fetchmeth(stash, "DESTROY", 7, 0);
500 84 ▶ return gv && GvCV(gv) && CvISXSUB(GvCV(gv)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
return gv && GvCV(gv) && CvISXSUB(GvCV(gv)) ? 1 : 0; 30 54 84
return gv && GvCV(gv) && CvISXSUB(GvCV(gv)) ? 1 : 0; 30 0 30
return gv && GvCV(gv) && CvISXSUB(GvCV(gv)) ? 1 : 0; 24 6 30
501 }
502  
503 static void sc_enc_value(pTHX_ sc_enc *e, SV *sv);
504 static void sc_enc_body(pTHX_ sc_enc *e, SV *sv);
505  
506 /* ---- the kinds that are not data -------------------------------------------------
507 *
508 * Each is written from what perl already knows about it, and none of them is
509 * read through: a tied hash is never iterated (that would call FETCH), a
510 * regexp is its pattern and flags rather than a match, a sub is its name or
511 * its source. */
512  
513 /* The tie object, as a value. Perl allows a tie with no object only through
514 * XS, and there is nothing to write for one. */
515 17 static void sc_enc_tie(pTHX_ sc_enc *e, MAGIC *mg, unsigned char tag,
516 const char *what)
517 {
518 17 ▶ if (!mg->mg_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mg->mg_obj) { 0 17 17
519 0 croak("Struct::Codec: cannot encode a tied %s with no object", what);
520 }
521 17 ▶ SC_PUT(e, tag);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, tag); 17 0 17
522 17 sc_enc_value(aTHX_ e, mg->mg_obj);
523 17 }
524  
525 #if SC_HAVE_REGEXP
526 /* The flag letters as re::regexp_pattern spells them: the modifiers in the
527 * order "msixxnp", then the character set. Read back by sc_dec_regexp, which
528 * accepts exactly this alphabet and nothing else. */
529 234 static STRLEN sc_rx_flags(REGEXP *rx, char *out) {
530 234 U32 fl = RX_EXTFLAGS(rx);
531 234 STRLEN n = 0;
532 234 ▶ if (fl & RXf_PMf_MULTILINE) out[n++] = 'm';
Branch: 1 branch
BranchTrueFalseTotal
if (fl & RXf_PMf_MULTILINE) out[n++] = 'm'; 5 229 234
533 234 ▶ if (fl & RXf_PMf_SINGLELINE) out[n++] = 's';
Branch: 1 branch
BranchTrueFalseTotal
if (fl & RXf_PMf_SINGLELINE) out[n++] = 's'; 5 229 234
534 234 ▶ if (fl & RXf_PMf_FOLD) out[n++] = 'i';
Branch: 1 branch
BranchTrueFalseTotal
if (fl & RXf_PMf_FOLD) out[n++] = 'i'; 10 224 234
535 234 ▶ if (fl & RXf_PMf_EXTENDED) out[n++] = 'x';
Branch: 1 branch
BranchTrueFalseTotal
if (fl & RXf_PMf_EXTENDED) out[n++] = 'x'; 5 229 234
536 # ifdef RXf_PMf_EXTENDED_MORE
537 234 ▶ if (fl & RXf_PMf_EXTENDED_MORE) out[n++] = 'x';
Branch: 1 branch
BranchTrueFalseTotal
if (fl & RXf_PMf_EXTENDED_MORE) out[n++] = 'x'; 1 233 234
538 # endif
539 # ifdef RXf_PMf_NOCAPTURE
540 234 ▶ if (fl & RXf_PMf_NOCAPTURE) out[n++] = 'n';
Branch: 1 branch
BranchTrueFalseTotal
if (fl & RXf_PMf_NOCAPTURE) out[n++] = 'n'; 2 232 234
541 # endif
542 234 ▶ if (fl & RXf_PMf_KEEPCOPY) out[n++] = 'p';
Branch: 1 branch
BranchTrueFalseTotal
if (fl & RXf_PMf_KEEPCOPY) out[n++] = 'p'; 0 234 234
543 # if SC_HAVE_CHARSET
544 234 switch (get_regex_charset(fl)) {
545 1 case REGEX_LOCALE_CHARSET: out[n++] = 'l'; break;
546 4 case REGEX_UNICODE_CHARSET: out[n++] = 'u'; break;
547 0 case REGEX_ASCII_RESTRICTED_CHARSET: out[n++] = 'a'; break;
548 2 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
549 2 out[n++] = 'a'; out[n++] = 'a'; break;
550 227 default: break;
551 }
552 # endif
553 234 return n;
554 }
555 #endif
556  
557 234 static void sc_enc_regexp(pTHX_ sc_enc *e, SV *rv) {
558 #if SC_HAVE_REGEXP
559 234 REGEXP *rx = SvRX(rv);
560 char flags[16];
561 STRLEN nf;
562 234 ▶ if (!rx) sc_enc_croak_type(aTHX_ "Regexp with no pattern");
Branch: 1 branch
BranchTrueFalseTotal
if (!rx) sc_enc_croak_type(aTHX_ "Regexp with no pattern"); 0 234 234
563 234 nf = sc_rx_flags(rx, flags);
564 234 ▶ SC_PUT(e, SC_T_REGEXP);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_REGEXP); 234 0 234
565 234 sc_enc_key(aTHX_ e, RX_PRECOMP(rx), (STRLEN)RX_PRELEN(rx), RX_UTF8(rx) ? 1 : 0);
566 234 sc_enc_key(aTHX_ e, flags, nf, 0);
567 #else
568 (void)e; (void)rv;
569 croak("Struct::Codec: a regexp needs perl 5.12 or later");
570 #endif
571 234 }
572  
573 /* "Package::name" for a glob, into a mortal; the flag says whether either
574 * half is UTF-8. NULL for a glob with no stash, which is one perl made for
575 * its own use and nobody else can find. */
576 12 static SV *sc_gv_name(pTHX_ GV *gv, int *utf8) {
577 12 HV *stash = GvSTASH(gv);
578 SV *name;
579 12 ▶ if (!stash || !HvNAME(stash) || !GvNAME(gv)) return NULL;
Branch: 9 branches
BranchTrueFalseTotal
if (!stash || !HvNAME(stash) || !GvNAME(gv)) return NULL; 12 0 12
if (!stash || !HvNAME(stash) || !GvNAME(gv)) return NULL; 12 0 12
if (!stash || !HvNAME(stash) || !GvNAME(gv)) return NULL; 12 0 12
if (!stash || !HvNAME(stash) || !GvNAME(gv)) return NULL; 0 12 12
if (!stash || !HvNAME(stash) || !GvNAME(gv)) return NULL; 0 0 0
if (!stash || !HvNAME(stash) || !GvNAME(gv)) return NULL; 12 0 12
if (!stash || !HvNAME(stash) || !GvNAME(gv)) return NULL; 0 12 12
if (!stash || !HvNAME(stash) || !GvNAME(gv)) return NULL; 12 0 12
if (!stash || !HvNAME(stash) || !GvNAME(gv)) return NULL; 0 12 12
580 12 ▶ name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash)));
Branch: 12 branches
BranchTrueFalseTotal
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 12 0 12
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 12 0 12
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 0 12 12
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 0 0 0
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 12 0 12
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 0 12 12
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 12 0 12
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 12 0 12
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 0 12 12
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 0 0 0
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 12 0 12
name = sv_2mortal(newSVpvn(HvNAME(stash), (STRLEN)HvNAMELEN_get(stash))); 0 12 12
581 12 ▶ if (HvNAMEUTF8(stash)) SvUTF8_on(name);
Branch: 7 branches
BranchTrueFalseTotal
if (HvNAMEUTF8(stash)) SvUTF8_on(name); 12 0 12
if (HvNAMEUTF8(stash)) SvUTF8_on(name); 12 0 12
if (HvNAMEUTF8(stash)) SvUTF8_on(name); 0 12 12
if (HvNAMEUTF8(stash)) SvUTF8_on(name); 12 0 12
if (HvNAMEUTF8(stash)) SvUTF8_on(name); 0 12 12
if (HvNAMEUTF8(stash)) SvUTF8_on(name); 0 12 12
if (HvNAMEUTF8(stash)) SvUTF8_on(name); 0 12 12
582 12 sv_catpvs(name, "::");
583 {
584 12 SV *tail = sv_2mortal(newSVpvn(GvNAME(gv), (STRLEN)GvNAMELEN(gv)));
585 #ifdef GvNAMEUTF8
586 12 ▶ if (GvNAMEUTF8(gv)) SvUTF8_on(tail);
Branch: 1 branch
BranchTrueFalseTotal
if (GvNAMEUTF8(gv)) SvUTF8_on(tail); 0 12 12
587 #endif
588 12 sv_catsv(name, tail);
589 }
590 12 *utf8 = SvUTF8(name) ? 1 : 0;
591 12 return name;
592 }
593  
594 /* A named sub is its name. An anonymous one is its source, from B::Deparse,
595 * which is slow and is the only way there is; one that captured a lexical is
596 * refused, because its source without the lexical is a different sub. The
597 * deparser's temporaries live inside their own ENTER/SAVETMPS, and the source
598 * is copied out before that scope closes, because the encoder's own mortals
599 * (the buffer, the seen table) were made before it and must not be swept. */
600 13 static void sc_enc_code(pTHX_ sc_enc *e, CV *cv) {
601 13 GV *gv = CvGV(cv);
602 SV *src;
603  
604 13 ▶ if (!CvANON(cv) && gv) {
Branch: 2 branches
BranchTrueFalseTotal
if (!CvANON(cv) && gv) { 8 5 13
if (!CvANON(cv) && gv) { 8 0 8
605 8 int utf8 = 0;
606 8 SV *name = sc_gv_name(aTHX_ gv, &utf8);
607 8 ▶ if (name) {
Branch: 1 branch
BranchTrueFalseTotal
if (name) { 8 0 8
608 8 ▶ SC_PUT(e, SC_T_CODE_NAME);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_CODE_NAME); 8 0 8
609 8 sc_enc_key(aTHX_ e, SvPVX(name), SvCUR(name), utf8);
610 8 return;
611 }
612 }
613 5 ▶ if (CvISXSUB(cv)) sc_enc_croak_type(aTHX_ "anonymous XSUB");
Branch: 1 branch
BranchTrueFalseTotal
if (CvISXSUB(cv)) sc_enc_croak_type(aTHX_ "anonymous XSUB"); 0 5 5
614 5 ▶ if (CvCLONED(cv)) croak("Struct::Codec: cannot encode a closure: "
Branch: 1 branch
BranchTrueFalseTotal
if (CvCLONED(cv)) croak("Struct::Codec: cannot encode a closure: " 5 0 5
615 "it captured a lexical that would not come with it");
616  
617 {
618 0 dSP;
619 int count;
620 0 ENTER;
621 0 SAVETMPS;
622 0 load_module(PERL_LOADMOD_NOIMPORT, newSVpvs("B::Deparse"), NULL);
623 /* load_module runs perl (B::Deparse's own compilation), which can
624 * GROW and so reallocate the argument stack. dSP was taken before it,
625 * so SP may now dangle into the freed old stack; writing args through
626 * it corrupts the heap. It only bites when the load happens to trigger
627 * a realloc, which depends on how deep the stack already is - hence a
628 * crash from inside a test and not from a bare one-line encode. */
629 0 SPAGAIN;
630 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
631 0 ▶ XPUSHs(sv_2mortal(newSVpvs("B::Deparse")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("B::Deparse"))); 0 0 0
632 0 PUTBACK;
633 0 count = call_method("new", G_SCALAR);
634 0 SPAGAIN;
635 0 ▶ if (count != 1) croak("Struct::Codec: B::Deparse->new returned nothing");
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) croak("Struct::Codec: B::Deparse->new returned nothing"); 0 0 0
636 {
637 0 SV *deparser = POPs;
638 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
639 0 ▶ XPUSHs(deparser);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(deparser); 0 0 0
640 0 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)cv)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)cv))); 0 0 0
641 0 PUTBACK;
642 0 count = call_method("coderef2text", G_SCALAR);
643 0 SPAGAIN;
644 0 ▶ if (count != 1) croak("Struct::Codec: B::Deparse returned nothing");
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) croak("Struct::Codec: B::Deparse returned nothing"); 0 0 0
645 0 src = newSVsv(POPs); /* owned: it outlives the scope */
646 0 PUTBACK;
647 }
648 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
649 0 LEAVE;
650 0 sv_2mortal(src);
651 }
652 0 ▶ SC_PUT(e, SC_T_CODE_SRC);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_CODE_SRC); 0 0 0
653 0 sc_enc_key(aTHX_ e, SvPVX(src), SvCUR(src), SvUTF8(src) ? 1 : 0);
654 }
655  
656 /* A glob that a name finds again is its name. One that no name finds - a
657 * lexical filehandle, a gensym - is its descriptor, if it has one open. The
658 * test is on the GP and not the SV: `$x = *STDOUT` makes a second SV that
659 * shares the first's GP, and by name is what it means. */
660 4 static void sc_enc_glob(pTHX_ sc_enc *e, GV *gv) {
661 4 int utf8 = 0;
662 4 SV *name = sc_gv_name(aTHX_ gv, &utf8);
663 IO *io;
664 4 ▶ if (name) {
Branch: 1 branch
BranchTrueFalseTotal
if (name) { 4 0 4
665 4 ▶ GV *found = gv_fetchpvn_flags(SvPVX(name), SvCUR(name),
Branch: 1 branch
BranchTrueFalseTotal
GV *found = gv_fetchpvn_flags(SvPVX(name), SvCUR(name), 0 4 4
666 utf8 ? SVf_UTF8 : 0, SVt_PVGV);
667 4 ▶ if (found && GvGP(found) == GvGP(gv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (found && GvGP(found) == GvGP(gv)) { 3 1 4
if (found && GvGP(found) == GvGP(gv)) { 3 0 3
668 3 ▶ SC_PUT(e, SC_T_GLOB);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_GLOB); 3 0 3
669 3 sc_enc_key(aTHX_ e, SvPVX(name), SvCUR(name), utf8);
670 4 return;
671 }
672 }
673 1 ▶ io = GvIO(gv);
Branch: 4 branches
BranchTrueFalseTotal
io = GvIO(gv); 1 0 1
io = GvIO(gv); 0 1 1
io = GvIO(gv); 0 0 0
io = GvIO(gv); 1 0 1
674 1 ▶ if (io && IoIFP(io) && PerlIO_fileno(IoIFP(io)) >= 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (io && IoIFP(io) && PerlIO_fileno(IoIFP(io)) >= 0) { 1 0 1
if (io && IoIFP(io) && PerlIO_fileno(IoIFP(io)) >= 0) { 1 0 1
if (io && IoIFP(io) && PerlIO_fileno(IoIFP(io)) >= 0) { 1 0 1
675 1 ▶ SC_PUT(e, SC_T_FD_GLOB);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_FD_GLOB); 1 0 1
676 1 ▶ SC_PUT(e, IoTYPE(io));
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, IoTYPE(io)); 1 0 1
677 1 sc_enc_varint(aTHX_ e, (UV)PerlIO_fileno(IoIFP(io)));
678 1 return;
679 }
680 0 sc_enc_croak_type(aTHX_ "GLOB that has no name and no open filehandle");
681 }
682  
683 0 static void sc_enc_io(pTHX_ sc_enc *e, IO *io) {
684 0 ▶ if (!IoIFP(io) || PerlIO_fileno(IoIFP(io)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!IoIFP(io) || PerlIO_fileno(IoIFP(io)) < 0) 0 0 0
if (!IoIFP(io) || PerlIO_fileno(IoIFP(io)) < 0) 0 0 0
685 0 sc_enc_croak_type(aTHX_ "IO that is not open");
686 0 ▶ SC_PUT(e, SC_T_FD_IO);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_FD_IO); 0 0 0
687 0 ▶ SC_PUT(e, IoTYPE(io));
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, IoTYPE(io)); 0 0 0
688 0 sc_enc_varint(aTHX_ e, (UV)PerlIO_fileno(IoIFP(io)));
689 0 }
690  
691 /* A format lives in a glob's FORMAT slot and has no other identity. */
692 0 static void sc_enc_format(pTHX_ sc_enc *e, CV *fm) {
693 0 GV *gv = CvGV(fm);
694 0 int utf8 = 0;
695 0 ▶ SV *name = gv ? sc_gv_name(aTHX_ gv, &utf8) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *name = gv ? sc_gv_name(aTHX_ gv, &utf8) : NULL; 0 0 0
696 0 ▶ if (!name) sc_enc_croak_type(aTHX_ "FORMAT with no name");
Branch: 1 branch
BranchTrueFalseTotal
if (!name) sc_enc_croak_type(aTHX_ "FORMAT with no name"); 0 0 0
697 0 ▶ SC_PUT(e, SC_T_FORMAT);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_FORMAT); 0 0 0
698 0 sc_enc_key(aTHX_ e, SvPVX(name), SvCUR(name), utf8);
699 0 }
700  
701 /* The thing behind a reference: an array, a hash, a scalar, or one of the
702 * kinds that are not data. The referent has already been entered in the seen
703 * table by the caller, so a scalar referent goes straight to its body and is
704 * not looked up again. */
705 618889 static void sc_enc_referent(pTHX_ sc_enc *e, SV *rv) {
706 MAGIC *tie;
707 618889 switch (SvTYPE(rv)) {
708 614420 case SVt_PVAV: {
709 614420 AV *av = (AV *)rv;
710 SSize_t n, i;
711 614420 ▶ if ((tie = sc_tie_magic(aTHX_ rv))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((tie = sc_tie_magic(aTHX_ rv))) { 3 614417 614420
712 3 sc_enc_tie(aTHX_ e, tie, SC_T_TIED_ARRAY, "ARRAY");
713 3 return;
714 }
715 614417 n = av_len(av) + 1;
716 614417 ▶ SC_PUT(e, SC_T_ARRAY);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_ARRAY); 614403 14 614417
717 614417 sc_enc_varint(aTHX_ e, (UV)n);
718 3726339 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3116020 610319 3726339
719 3116020 SV **slot = av_fetch(av, i, 0);
720 3116020 ▶ sc_enc_value(aTHX_ e, slot ? *slot : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sc_enc_value(aTHX_ e, slot ? *slot : &PL_sv_undef); 3116017 3 3116020
721 }
722 610319 return;
723 }
724 3730 case SVt_PVHV: {
725 3730 HV *hv = (HV *)rv;
726 HE *he;
727 3730 ▶ if ((tie = sc_tie_magic(aTHX_ rv))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((tie = sc_tie_magic(aTHX_ rv))) { 7 3723 3730
728 7 sc_enc_tie(aTHX_ e, tie, SC_T_TIED_HASH, "HASH");
729 7 return;
730 }
731 /* HvUSEDKEYS is the count of REAL keys. A restricted hash keeps a
732 * placeholder for each locked-but-absent key, and hv_iterinit counts
733 * those while hv_iternext skips them, so a count taken from it is
734 * larger than what follows and the decoder finds the stream short. */
735 3723 ▶ SC_PUT(e, SC_T_HASH);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_HASH); 3717 6 3723
736 3723 hv_iterinit(hv);
737 3723 ▶ sc_enc_varint(aTHX_ e, (UV)HvUSEDKEYS(hv));
Branch: 1 branch
BranchTrueFalseTotal
sc_enc_varint(aTHX_ e, (UV)HvUSEDKEYS(hv)); 2 3721 3723
738 609364 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 605647 3717 609364
739 STRLEN kl;
740 605647 ▶ const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, kl); 0 605647 605647
741 605647 ▶ sc_enc_key(aTHX_ e, k, kl, HeUTF8(he) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
sc_enc_key(aTHX_ e, k, kl, HeUTF8(he) ? 1 : 0); 0 605647 605647
742 605647 sc_enc_value(aTHX_ e, HeVAL(he));
743 }
744 3717 return;
745 }
746 13 case SVt_PVCV: sc_enc_code(aTHX_ e, (CV *)rv); return;
747 3 case SVt_PVGV: sc_enc_glob(aTHX_ e, (GV *)rv); return;
748 0 case SVt_PVIO: sc_enc_io(aTHX_ e, (IO *)rv); return;
749 0 case SVt_PVFM: sc_enc_format(aTHX_ e, (CV *)rv); return;
750 723 default:
751 723 ▶ if (sc_is_regexp(aTHX_ rv)) { sc_enc_regexp(aTHX_ e, rv); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (sc_is_regexp(aTHX_ rv)) { sc_enc_regexp(aTHX_ e, rv); return; } 234 489 723
752 489 sc_enc_body(aTHX_ e, rv);
753 489 return;
754 }
755 }
756  
757 /* The value itself, after the slot has been checked for sharing. */
758 3727196 static void sc_enc_body(pTHX_ sc_enc *e, SV *sv) {
759 MAGIC *tie;
760  
761 /* Before any magic is invoked: reading a tied scalar would call FETCH,
762 * and what is written is the tie, not what FETCH would have said. */
763 3727196 ▶ if (SvTYPE(sv) != SVt_PVGV && (tie = sc_tie_magic(aTHX_ sv))) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(sv) != SVt_PVGV && (tie = sc_tie_magic(aTHX_ sv))) { 3727195 1 3727196
if (SvTYPE(sv) != SVt_PVGV && (tie = sc_tie_magic(aTHX_ sv))) { 7 3727188 3727195
764 7 sc_enc_tie(aTHX_ e, tie, SC_T_TIED_SCALAR, "scalar");
765 7 return;
766 }
767 /* A glob copied into a slot, as `$x = *STDOUT` does. Not a reference, and
768 * not a string either, though it stringifies as one. */
769 3727189 ▶ if (SvTYPE(sv) == SVt_PVGV && isGV_with_GP(sv)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvTYPE(sv) == SVt_PVGV && isGV_with_GP(sv)) { 1 3727188 3727189
if (SvTYPE(sv) == SVt_PVGV && isGV_with_GP(sv)) { 1 0 1
if (SvTYPE(sv) == SVt_PVGV && isGV_with_GP(sv)) { 0 1 1
if (SvTYPE(sv) == SVt_PVGV && isGV_with_GP(sv)) { 0 0 0
770 1 sc_enc_glob(aTHX_ e, (GV *)sv);
771 1 return;
772 }
773  
774 3727188 SvGETMAGIC(sv);
775  
776 3727188 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 717697 3009491 3727188
777 717697 SV *rv = SvRV(sv);
778 717697 STRLEN tag_off = e->len;
779 717697 sc_seen *prev = sc_seen_find(e, rv);
780 717697 ▶ if (prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev) { 98785 618912 717697
781 98785 sc_enc_track(e, prev->off);
782 98785 ▶ SC_PUT(e, SC_T_REFP);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_REFP); 98716 69 98785
783 98785 sc_enc_varint(aTHX_ e, (UV)prev->off);
784 98785 return;
785 }
786 /* qr// is blessed into Regexp, and that class is implied by the tag;
787 * a regexp reblessed elsewhere keeps its class through OBJECT. */
788 618912 ▶ if (SvOBJECT(rv)
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(rv) 830 618082 618912
789 830 ▶ && !(sc_is_regexp(aTHX_ rv) && HvNAME(SvSTASH(rv))
Branch: 8 branches
BranchTrueFalseTotal
&& !(sc_is_regexp(aTHX_ rv) && HvNAME(SvSTASH(rv)) 234 596 830
&& !(sc_is_regexp(aTHX_ rv) && HvNAME(SvSTASH(rv)) 234 0 234
&& !(sc_is_regexp(aTHX_ rv) && HvNAME(SvSTASH(rv)) 234 0 234
&& !(sc_is_regexp(aTHX_ rv) && HvNAME(SvSTASH(rv)) 0 234 234
&& !(sc_is_regexp(aTHX_ rv) && HvNAME(SvSTASH(rv)) 0 0 0
&& !(sc_is_regexp(aTHX_ rv) && HvNAME(SvSTASH(rv)) 234 0 234
&& !(sc_is_regexp(aTHX_ rv) && HvNAME(SvSTASH(rv)) 0 234 234
&& !(sc_is_regexp(aTHX_ rv) && HvNAME(SvSTASH(rv)) 234 0 234
790 810 ▶ && strEQ(HvNAME(SvSTASH(rv)), "Regexp"))) {
Branch: 7 branches
BranchTrueFalseTotal
&& strEQ(HvNAME(SvSTASH(rv)), "Regexp"))) { 234 0 234
&& strEQ(HvNAME(SvSTASH(rv)), "Regexp"))) { 234 0 234
&& strEQ(HvNAME(SvSTASH(rv)), "Regexp"))) { 0 234 234
&& strEQ(HvNAME(SvSTASH(rv)), "Regexp"))) { 0 0 0
&& strEQ(HvNAME(SvSTASH(rv)), "Regexp"))) { 234 0 234
&& strEQ(HvNAME(SvSTASH(rv)), "Regexp"))) { 0 234 234
&& strEQ(HvNAME(SvSTASH(rv)), "Regexp"))) { 3 231 234
791 599 HV *stash = SvSTASH(rv);
792 599 ▶ const char *nm = HvNAME(stash);
Branch: 6 branches
BranchTrueFalseTotal
const char *nm = HvNAME(stash); 599 0 599
const char *nm = HvNAME(stash); 599 0 599
const char *nm = HvNAME(stash); 0 599 599
const char *nm = HvNAME(stash); 0 0 0
const char *nm = HvNAME(stash); 599 0 599
const char *nm = HvNAME(stash); 0 599 599
793 599 ▶ if (!nm) sc_enc_croak_type(aTHX_ "reference blessed into an anonymous stash");
Branch: 1 branch
BranchTrueFalseTotal
if (!nm) sc_enc_croak_type(aTHX_ "reference blessed into an anonymous stash"); 0 599 599
794 599 ▶ if (sc_is_pointer_obj(aTHX_ rv, stash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sc_is_pointer_obj(aTHX_ rv, stash)) { 23 576 599
795 /* Not entered in the seen table, so a second reference to it
796 * arrives here again and is dropped again. */
797 23 ▶ if (e->flags & SC_STRIP_POINTERS) { SC_PUT(e, SC_T_UNDEF); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (e->flags & SC_STRIP_POINTERS) { SC_PUT(e, SC_T_UNDEF); return; } 15 8 23
if (e->flags & SC_STRIP_POINTERS) { SC_PUT(e, SC_T_UNDEF); return; } 15 0 15
798 8 ▶ croak("Struct::Codec: cannot encode a %.*s object that holds a pointer"
Branch: 6 branches
BranchTrueFalseTotal
croak("Struct::Codec: cannot encode a %.*s object that holds a pointer" 8 0 8
croak("Struct::Codec: cannot encode a %.*s object that holds a pointer" 8 0 8
croak("Struct::Codec: cannot encode a %.*s object that holds a pointer" 0 8 8
croak("Struct::Codec: cannot encode a %.*s object that holds a pointer" 0 0 0
croak("Struct::Codec: cannot encode a %.*s object that holds a pointer" 8 0 8
croak("Struct::Codec: cannot encode a %.*s object that holds a pointer" 0 8 8
799 " (strip_pointers => 1 drops it)",
800 (int)HvNAMELEN_get(stash), nm);
801 }
802 576 ▶ SC_PUT(e, SC_T_OBJECT);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_OBJECT); 575 1 576
803 576 ▶ sc_enc_key(aTHX_ e, nm, (STRLEN)HvNAMELEN_get(stash),
Branch: 6 branches
BranchTrueFalseTotal
sc_enc_key(aTHX_ e, nm, (STRLEN)HvNAMELEN_get(stash), 576 0 576
sc_enc_key(aTHX_ e, nm, (STRLEN)HvNAMELEN_get(stash), 576 0 576
sc_enc_key(aTHX_ e, nm, (STRLEN)HvNAMELEN_get(stash), 0 576 576
sc_enc_key(aTHX_ e, nm, (STRLEN)HvNAMELEN_get(stash), 0 0 0
sc_enc_key(aTHX_ e, nm, (STRLEN)HvNAMELEN_get(stash), 576 0 576
sc_enc_key(aTHX_ e, nm, (STRLEN)HvNAMELEN_get(stash), 0 576 576
804 576 ▶ HvNAMEUTF8(stash) ? 1 : 0);
Branch: 8 branches
BranchTrueFalseTotal
HvNAMEUTF8(stash) ? 1 : 0); 576 0 576
HvNAMEUTF8(stash) ? 1 : 0); 576 0 576
HvNAMEUTF8(stash) ? 1 : 0); 0 576 576
HvNAMEUTF8(stash) ? 1 : 0); 0 0 0
HvNAMEUTF8(stash) ? 1 : 0); 576 0 576
HvNAMEUTF8(stash) ? 1 : 0); 0 576 576
HvNAMEUTF8(stash) ? 1 : 0); 0 576 576
HvNAMEUTF8(stash) ? 1 : 0); 174 402 576
805 }
806 else {
807 618313 ▶ SC_PUT(e, SC_T_REF);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_REF); 618292 21 618313
808 }
809 /* The referent is what a second reference shares, so it is what is
810 * tracked, under the REF tag's offset. One with refcount 1 cannot be
811 * shared and is not entered. */
812 618889 ▶ if (SvREFCNT(rv) > 1 && !SvIMMORTAL(rv)) sc_seen_put(aTHX_ e, rv, tag_off, 1);
Branch: 4 branches
BranchTrueFalseTotal
if (SvREFCNT(rv) > 1 && !SvIMMORTAL(rv)) sc_seen_put(aTHX_ e, rv, tag_off, 1); 3398 615491 618889
if (SvREFCNT(rv) > 1 && !SvIMMORTAL(rv)) sc_seen_put(aTHX_ e, rv, tag_off, 1); 14 3384 3398
if (SvREFCNT(rv) > 1 && !SvIMMORTAL(rv)) sc_seen_put(aTHX_ e, rv, tag_off, 1); 14 0 14
if (SvREFCNT(rv) > 1 && !SvIMMORTAL(rv)) sc_seen_put(aTHX_ e, rv, tag_off, 1); 14 0 14
813 618889 sc_enc_referent(aTHX_ e, rv);
814 614780 return;
815 }
816  
817 /* The PUBLIC flags decide the kind, in the order string, integer, float.
818 * A private flag alone is a cached conversion that may be lossy: Storable
819 * leaves pIOK on the NV 1.5 after freezing it, and an encoder that read
820 * IOKp there would write the integer 1. A public POK on a number that was
821 * stringified is not set, so 5 stays 5 after being printed, and "007"
822 * stays "007" because its POK is public. Only when no public flag is set
823 * do the private ones get a say, in the same order. A boolean is checked
824 * first because PL_sv_yes is also POK and IOK. */
825 #if SC_HAVE_BOOL
826 3009491 ▶ if (SvIsBOOL(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsBOOL(sv)) { 2 3009489 3009491
827 2 ▶ SC_PUT(e, SvTRUE(sv) ? SC_T_TRUE : SC_T_FALSE);
Branch: 2 branches
BranchTrueFalseTotal
SC_PUT(e, SvTRUE(sv) ? SC_T_TRUE : SC_T_FALSE); 2 0 2
SC_PUT(e, SvTRUE(sv) ? SC_T_TRUE : SC_T_FALSE); 1 1 2
828 }
829 else
830 #endif
831 3013727 ▶ if (SvPOK(sv) || (!SvIOK(sv) && !SvNOK(sv) && SvPOKp(sv))) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvPOK(sv) || (!SvIOK(sv) && !SvNOK(sv) && SvPOKp(sv))) { 3005251 4238 3009489
if (SvPOK(sv) || (!SvIOK(sv) && !SvNOK(sv) && SvPOKp(sv))) { 1479 3003772 3005251
if (SvPOK(sv) || (!SvIOK(sv) && !SvNOK(sv) && SvPOKp(sv))) { 395 1084 1479
if (SvPOK(sv) || (!SvIOK(sv) && !SvNOK(sv) && SvPOKp(sv))) { 0 395 395
832 STRLEN n;
833 4238 const char *p = SvPV_nomg(sv, n);
834 4238 sc_enc_str(aTHX_ e, p, n, SvUTF8(sv) ? 1 : 0);
835 }
836 3005251 ▶ else if (SvIOK(sv) || (!SvNOK(sv) && SvIOKp(sv))) {
Branch: 3 branches
BranchTrueFalseTotal
else if (SvIOK(sv) || (!SvNOK(sv) && SvIOKp(sv))) { 1479 3003772 3005251
else if (SvIOK(sv) || (!SvNOK(sv) && SvIOKp(sv))) { 395 1084 1479
else if (SvIOK(sv) || (!SvNOK(sv) && SvIOKp(sv))) { 0 395 395
837 3003772 ▶ if (SvIsUV(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) { 7 3003765 3003772
838 7 UV u = SvUVX(sv);
839 7 ▶ if (u <= (UV)SC_SMALL_MAX) SC_PUT(e, SC_T_SMALL_POS | (unsigned char)u);
Branch: 2 branches
BranchTrueFalseTotal
if (u <= (UV)SC_SMALL_MAX) SC_PUT(e, SC_T_SMALL_POS | (unsigned char)u); 0 7 7
if (u <= (UV)SC_SMALL_MAX) SC_PUT(e, SC_T_SMALL_POS | (unsigned char)u); 0 0 0
840 7 ▶ else { SC_PUT(e, SC_T_UV); sc_enc_varint(aTHX_ e, u); }
Branch: 1 branch
BranchTrueFalseTotal
else { SC_PUT(e, SC_T_UV); sc_enc_varint(aTHX_ e, u); } 7 0 7
841 }
842 else {
843 3003765 IV i = SvIVX(sv);
844 3003765 ▶ if (i >= 0 && i <= SC_SMALL_MAX) SC_PUT(e, SC_T_SMALL_POS | (unsigned char)i);
Branch: 3 branches
BranchTrueFalseTotal
if (i >= 0 && i <= SC_SMALL_MAX) SC_PUT(e, SC_T_SMALL_POS | (unsigned char)i); 3003282 483 3003765
if (i >= 0 && i <= SC_SMALL_MAX) SC_PUT(e, SC_T_SMALL_POS | (unsigned char)i); 152235 2851047 3003282
if (i >= 0 && i <= SC_SMALL_MAX) SC_PUT(e, SC_T_SMALL_POS | (unsigned char)i); 152214 21 152235
845 2851530 ▶ else if (i < 0 && i >= SC_SMALL_MIN) SC_PUT(e, SC_T_SMALL_NEG | (unsigned char)(i + 16));
Branch: 3 branches
BranchTrueFalseTotal
else if (i < 0 && i >= SC_SMALL_MIN) SC_PUT(e, SC_T_SMALL_NEG | (unsigned char)(i + 16)); 483 2851047 2851530
else if (i < 0 && i >= SC_SMALL_MIN) SC_PUT(e, SC_T_SMALL_NEG | (unsigned char)(i + 16)); 11 472 483
else if (i < 0 && i >= SC_SMALL_MIN) SC_PUT(e, SC_T_SMALL_NEG | (unsigned char)(i + 16)); 11 0 11
846 2851519 ▶ else if (i >= 0) { SC_PUT(e, SC_T_UV); sc_enc_varint(aTHX_ e, (UV)i); }
Branch: 2 branches
BranchTrueFalseTotal
else if (i >= 0) { SC_PUT(e, SC_T_UV); sc_enc_varint(aTHX_ e, (UV)i); } 2851047 472 2851519
else if (i >= 0) { SC_PUT(e, SC_T_UV); sc_enc_varint(aTHX_ e, (UV)i); } 2851047 0 2851047
847 472 ▶ else { SC_PUT(e, SC_T_NEG); sc_enc_varint(aTHX_ e, (UV)(-(i + 1))); }
Branch: 1 branch
BranchTrueFalseTotal
else { SC_PUT(e, SC_T_NEG); sc_enc_varint(aTHX_ e, (UV)(-(i + 1))); } 470 2 472
848 }
849 }
850 2563 ▶ else if (SvNOK(sv) || SvNOKp(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (SvNOK(sv) || SvNOKp(sv)) { 395 1084 1479
else if (SvNOK(sv) || SvNOKp(sv)) { 0 395 395
851 1084 NV nv = SvNVX(sv);
852 #if SC_NV_WIDE
853 if (sc_nv_needs_str(nv)) {
854 /* buf is an array and not a pointer on purpose: it is sized for the
855 * widest NV plus a sign, a point, an exponent and the NUL. */
856 char buf[SC_NV_STR_MAX + 1];
857 char fmt[16];
858 int blen;
859 sc_nv_fmt(fmt, sizeof(fmt));
860 blen = my_snprintf(buf, sizeof(buf), fmt, nv);
861 if (blen > 0 && blen <= SC_NV_STR_MAX) {
862 blen = sc_nv_radix(buf, blen);
863 SC_PUT(e, SC_T_NV_STR);
864 sc_enc_varint(aTHX_ e, (UV)blen);
865 sc_enc_bytes(aTHX_ e, buf, (STRLEN)blen);
866 return;
867 }
868 /* Unreachable at any NV width perl builds on: 36 digits is 44 bytes
869 * with the sign, the point and a five-byte exponent. A buffer that
870 * really did not fit never arrives here anyway - my_snprintf croaks
871 * "my_snprintf buffer overflow" rather than returning the length it
872 * wanted - so this covers a formatter that failed and returned
873 * negative, and the 8-byte form below is still a valid stream. */
874 }
875 #endif
876 {
877 unsigned char raw[SC_NV_BYTES];
878 1084 sc_nv_to_wire(nv, raw);
879 1084 ▶ SC_PUT(e, SC_T_NV);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_NV); 1083 1 1084
880 1084 sc_enc_bytes(aTHX_ e, (const char *)raw, SC_NV_BYTES);
881 }
882 }
883 else {
884 395 ▶ SC_PUT(e, SC_T_UNDEF);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_UNDEF); 395 0 395
885 }
886 }
887  
888 /* A slot value: something in an array element, a hash value, or the root. */
889 3726713 static void sc_enc_value(pTHX_ sc_enc *e, SV *sv) {
890 3726713 ▶ if (++e->depth > SC_DEPTH_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (++e->depth > SC_DEPTH_MAX) 1 3726712 3726713
891 1 croak("Struct::Codec: structure deeper than %d", SC_DEPTH_MAX);
892  
893 /* A scalar that can be met twice. A reference SV is not entered as a
894 * scalar - see the header - and an immortal never is. */
895 3726712 ▶ if (SvREFCNT(sv) > 1 && !SvIMMORTAL(sv) && !SvROK(sv)) {
Branch: 5 branches
BranchTrueFalseTotal
if (SvREFCNT(sv) > 1 && !SvIMMORTAL(sv) && !SvROK(sv)) { 109 3726603 3726712
if (SvREFCNT(sv) > 1 && !SvIMMORTAL(sv) && !SvROK(sv)) { 23 86 109
if (SvREFCNT(sv) > 1 && !SvIMMORTAL(sv) && !SvROK(sv)) { 16 7 23
if (SvREFCNT(sv) > 1 && !SvIMMORTAL(sv) && !SvROK(sv)) { 16 0 16
if (SvREFCNT(sv) > 1 && !SvIMMORTAL(sv) && !SvROK(sv)) { 69 33 102
896 69 sc_seen *prev = sc_seen_find(e, sv);
897 69 ▶ if (prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev) { 5 64 69
898 5 sc_enc_track(e, prev->off);
899 5 ▶ SC_PUT(e, SC_T_ALIAS);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_T_ALIAS); 5 0 5
900 5 sc_enc_varint(aTHX_ e, (UV)prev->off);
901 5 e->depth--;
902 5 return;
903 }
904 64 sc_seen_put(aTHX_ e, sv, e->len, 0);
905 }
906  
907 3726707 sc_enc_body(aTHX_ e, sv);
908 3722590 e->depth--;
909 }
910  
911 5029 static void sc_enc_run(pTHX_ sc_enc *e, SV *value) {
912 5029 ▶ SC_PUT(e, SC_MAGIC);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_MAGIC); 5015 14 5029
913 5029 ▶ SC_PUT(e, SC_VERSION);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_VERSION); 5015 14 5029
914 5029 ▶ SC_PUT(e, SC_FLOAT);
Branch: 1 branch
BranchTrueFalseTotal
SC_PUT(e, SC_FLOAT); 5013 16 5029
915 5029 sc_enc_value(aTHX_ e, value);
916 5015 }
917  
918 /* An owned byte SV with a refcount of one.
919 *
920 * A small value is encoded on the stack and copied once into an SV of exactly
921 * its size, with no allocation before that and nothing to free on a croak. A
922 * large one moves into a mortal SV when it outgrows the stack; that SV is
923 * then handed back with the caller's reference added, and the pending mortal
924 * decrement belongs to the caller's frame as it does for any XSUB's return. */
925 4951 static SV *sc_encode_flags(pTHX_ SV *value, U32 flags) {
926 sc_enc e;
927 char stack[SC_ENC_STACK];
928 4951 memset(&e, 0, sizeof e);
929 4951 e.buf = stack;
930 4951 e.cap = sizeof stack;
931 4951 e.flags = flags;
932 4951 sc_enc_run(aTHX_ &e, value);
933 4938 ▶ if (!e.bufsv) return newSVpvn(e.buf, e.len);
Branch: 1 branch
BranchTrueFalseTotal
if (!e.bufsv) return newSVpvn(e.buf, e.len); 2773 2165 4938
934 2165 SvCUR_set(e.bufsv, e.len);
935 2165 e.buf[e.len] = '\0';
936 2165 return SvREFCNT_inc_simple_NN(e.bufsv);
937 }
938  
939 /* The ABI's encode: no options, so a pointer object is a croak. */
940 1 static SV *sc_encode(pTHX_ SV *value) {
941 1 return sc_encode_flags(aTHX_ value, 0);
942 }
943  
944 /* Into caller memory. See sc_abi.h for the contract. */
945 78 static STRLEN sc_encode_to(pTHX_ SV *value, char *buf, STRLEN cap, STRLEN *need) {
946 sc_enc e;
947 78 memset(&e, 0, sizeof e);
948 78 e.buf = buf; e.cap = cap; e.fixed = 1;
949 78 sc_enc_run(aTHX_ &e, value);
950 77 ▶ if (need) *need = e.len;
Branch: 1 branch
BranchTrueFalseTotal
if (need) *need = e.len; 76 1 77
951 77 ▶ return e.len <= cap ? e.len : 0;
Branch: 1 branch
BranchTrueFalseTotal
return e.len <= cap ? e.len : 0; 30 47 77
952 }
953  
954 /* ============================================================================
955 * the decoder
956 * ==========================================================================*/
957  
958 typedef struct {
959 STRLEN off;
960 SV *sv; /* borrowed: it is already attached to the structure */
961 int isref; /* registered from a REF/OBJECT tag: sv is the RV */
962 } sc_slot;
963  
964 typedef struct {
965 const unsigned char *base, *p, *end;
966 sc_slot *tab; /* TRACKed tags in offset order, which is decode order;
967 * the body of a mortal SV, so a croak frees it */
968 STRLEN ntab, tabcap;
969 int depth;
970 } sc_dec;
971  
972 #define SC_OFF(d) ((UV)((d)->p - (d)->base))
973  
974 1799 static void sc_dec_croak(pTHX_ sc_dec *d, const char *why) {
975 1799 croak("Struct::Codec: %s at byte %" UVuf, why, SC_OFF(d));
976 }
977  
978 /* rule 1: every read is bounds-checked before it happens */
979 #define SC_NEED(d, n) \
980 do { \
981 if ((UV)((d)->end - (d)->p) < (UV)(n)) \
982 sc_dec_croak(aTHX_ (d), "truncated input"); \
983 } while (0)
984  
985 /* rule 2: an integer wider than a UV is corrupt, not big */
986 141303 static UV sc_dec_varint(pTHX_ sc_dec *d) {
987 141303 UV v = 0;
988 141303 int shift = 0, i;
989 149593 ▶ for (i = 0; i < SC_VARINT_MAX; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SC_VARINT_MAX; i++) { 149592 1 149593
990 unsigned char c;
991 149592 ▶ SC_NEED(d, 1);
Branch: 1 branch
BranchTrueFalseTotal
SC_NEED(d, 1); 209 149383 149592
992 149383 c = *d->p++;
993 149383 ▶ if (shift >= SC_UV_BITS
Branch: 1 branch
BranchTrueFalseTotal
if (shift >= SC_UV_BITS 149383 0 149383
994 149383 ▶ || (shift > SC_UV_BITS - 7 && ((UV)(c & 0x7F) >> (SC_UV_BITS - shift))))
Branch: 2 branches
BranchTrueFalseTotal
|| (shift > SC_UV_BITS - 7 && ((UV)(c & 0x7F) >> (SC_UV_BITS - shift)))) 7 149376 149383
|| (shift > SC_UV_BITS - 7 && ((UV)(c & 0x7F) >> (SC_UV_BITS - shift)))) 1 6 7
995 1 sc_dec_croak(aTHX_ d, "integer too wide");
996 149382 v |= (UV)(c & 0x7F) << shift;
997 149382 ▶ if (!(c & 0x80)) return v;
Branch: 1 branch
BranchTrueFalseTotal
if (!(c & 0x80)) return v; 141092 8290 149382
998 8290 shift += 7;
999 }
1000 1 sc_dec_croak(aTHX_ d, "integer too long");
1001 0 return 0;
1002 }
1003  
1004 /* rule 7: only an offset that was registered, which means already decoded */
1005 98905 static sc_slot *sc_dec_lookup(pTHX_ sc_dec *d, UV off) {
1006 98905 STRLEN lo = 0, hi = d->ntab;
1007 198501 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 99596 98905 198501
1008 99596 STRLEN mid = lo + (hi - lo) / 2;
1009 99596 ▶ if (d->tab[mid].off < off) lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (d->tab[mid].off < off) lo = mid + 1; 427 99169 99596
1010 99169 else hi = mid;
1011 }
1012 98905 ▶ if (lo < d->ntab && d->tab[lo].off == off) return &d->tab[lo];
Branch: 2 branches
BranchTrueFalseTotal
if (lo < d->ntab && d->tab[lo].off == off) return &d->tab[lo]; 98900 5 98905
if (lo < d->ntab && d->tab[lo].off == off) return &d->tab[lo]; 98900 0 98900
1013 5 sc_dec_croak(aTHX_ d, "reference to a value that was not tracked");
1014 0 return NULL;
1015 }
1016  
1017 2972 static void sc_dec_register(pTHX_ sc_dec *d, STRLEN off, SV *sv, int isref) {
1018 2972 ▶ if (d->ntab == d->tabcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (d->ntab == d->tabcap) { 2508 464 2972
1019 2508 sc_slot *old = d->tab;
1020 SV *holder;
1021 2508 ▶ d->tabcap = d->tabcap ? d->tabcap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
d->tabcap = d->tabcap ? d->tabcap * 2 : 16; 0 2508 2508
1022 2508 holder = sv_2mortal(newSV(d->tabcap * sizeof(sc_slot)));
1023 2508 d->tab = (sc_slot *)SvPVX(holder);
1024 2508 ▶ if (old) memcpy(d->tab, old, d->ntab * sizeof(sc_slot));
Branch: 1 branch
BranchTrueFalseTotal
if (old) memcpy(d->tab, old, d->ntab * sizeof(sc_slot)); 0 2508 2508
1025 }
1026 2972 d->tab[d->ntab].off = off;
1027 2972 d->tab[d->ntab].sv = sv;
1028 2972 d->tab[d->ntab].isref = isref;
1029 2972 d->ntab++;
1030 2972 }
1031  
1032 /* A key or class name: *p points INSIDE the input and the caller copies.
1033 *
1034 * rule 4: utf8 is validated, and only at a non-zero length. At zero the flag is
1035 * DROPPED and not merely left unchecked, because a zero length and a utf8 flag
1036 * together are a pair perl's string routines read as "no length given, find the
1037 * end yourself". is_utf8_string takes strlen there; hv_common is worse. It
1038 * sizes the buffer for the utf8-to-bytes downgrade from the length IT WAS GIVEN
1039 * - one byte for a zero-length key - and then copies the run utf8_to_bytes
1040 * measures for itself, which starts at the key and ends at the first byte that
1041 * cannot continue the string. Those bytes are the rest of the stream: a key of
1042 * length zero with the flag set wrote nine bytes into a one-byte allocation.
1043 * Nothing in a plain build says a word. The quadmath smoker aborted inside
1044 * free(), and under ASan it is a heap-buffer-overflow in memcpy under
1045 * Perl_hv_common. The empty string is the same key with the flag or without it,
1046 * so dropping it loses nothing. */
1047 10929 static void sc_dec_key(pTHX_ sc_dec *d, const char **p, STRLEN *n, int *utf8) {
1048 10929 UV v = sc_dec_varint(aTHX_ d);
1049 10909 *utf8 = (int)(v & 1);
1050 10909 v >>= 1;
1051 10909 ▶ SC_NEED(d, v);
Branch: 1 branch
BranchTrueFalseTotal
SC_NEED(d, v); 393 10516 10909
1052 10516 *p = (const char *)d->p;
1053 10516 *n = (STRLEN)v;
1054 10516 ▶ if (!v) *utf8 = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) *utf8 = 0; 438 10078 10516
1055 10078 ▶ else if (*utf8 && !is_utf8_string(d->p, (STRLEN)v))
Branch: 2 branches
BranchTrueFalseTotal
else if (*utf8 && !is_utf8_string(d->p, (STRLEN)v)) 2744 7334 10078
else if (*utf8 && !is_utf8_string(d->p, (STRLEN)v)) 1 2743 2744
1056 1 sc_dec_croak(aTHX_ d, "malformed UTF-8");
1057 10515 d->p += v;
1058 10515 }
1059  
1060 6300 static void sc_dec_str_into(pTHX_ sc_dec *d, SV *into, STRLEN n, int utf8) {
1061 6300 ▶ SC_NEED(d, n);
Branch: 1 branch
BranchTrueFalseTotal
SC_NEED(d, n); 423 5877 6300
1062 5877 ▶ if (utf8 && n && !is_utf8_string(d->p, n))
Branch: 3 branches
BranchTrueFalseTotal
if (utf8 && n && !is_utf8_string(d->p, n)) 1234 4643 5877
if (utf8 && n && !is_utf8_string(d->p, n)) 1234 0 1234
if (utf8 && n && !is_utf8_string(d->p, n)) 2 1232 1234
1063 2 sc_dec_croak(aTHX_ d, "malformed UTF-8");
1064 5875 sv_setpvn(into, (const char *)d->p, n);
1065 5875 ▶ if (utf8) SvUTF8_on(into);
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) SvUTF8_on(into); 1232 4643 5875
1066 5875 d->p += n;
1067 5875 }
1068  
1069 static SV *sc_dec_value(pTHX_ sc_dec *d, SV *into);
1070  
1071 /* A key as a mortal SV, flag and all: the spelling a name lookup wants. */
1072 298 static SV *sc_dec_key_sv(pTHX_ sc_dec *d) {
1073 const char *p; STRLEN n; int utf8;
1074 SV *sv;
1075 298 sc_dec_key(aTHX_ d, &p, &n, &utf8);
1076 249 sv = sv_2mortal(newSVpvn(p, n));
1077 249 ▶ if (utf8) SvUTF8_on(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) SvUTF8_on(sv); 2 247 249
1078 249 return sv;
1079 }
1080  
1081 /* ---- the kinds that are not data ----------------------------------------------------
1082 *
1083 * Each builder reads its payload and returns an OWNED SV, complete, or croaks
1084 * having built nothing. The caller attaches it in one step, so a croak inside
1085 * a builder leaves the parent holding the undef it was given, and the rule that
1086 * nothing half-built is ever reachable holds without any of them being filled
1087 * in place. */
1088  
1089 242 static SV *sc_dec_regexp(pTHX_ sc_dec *d) {
1090 #if SC_HAVE_REGEXP
1091 242 SV *pat = sc_dec_key_sv(aTHX_ d);
1092 const char *f; STRLEN nf, i; int fu;
1093 237 U32 fl = 0;
1094 237 int xs = 0, as = 0, charset = 0;
1095 REGEXP *rx;
1096  
1097 /* rule 12: the flag letters are checked against the alphabet the encoder
1098 * writes BEFORE anything is compiled; a letter outside it is corrupt
1099 * input, not a request. */
1100 237 sc_dec_key(aTHX_ d, &f, &nf, &fu);
1101 260 ▶ for (i = 0; i < nf; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nf; i++) { 28 232 260
1102 28 switch (f[i]) {
1103 3 case 'm': fl |= RXf_PMf_MULTILINE; break;
1104 3 case 's': fl |= RXf_PMf_SINGLELINE; break;
1105 6 case 'i': fl |= RXf_PMf_FOLD; break;
1106 0 case 'p': fl |= RXf_PMf_KEEPCOPY; break;
1107 4 case 'x':
1108 4 ▶ if (!xs++) { fl |= RXf_PMf_EXTENDED; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!xs++) { fl |= RXf_PMf_EXTENDED; break; } 3 1 4
1109 # ifdef RXf_PMf_EXTENDED_MORE
1110 1 fl |= RXf_PMf_EXTENDED_MORE; break;
1111 # else
1112 sc_dec_croak(aTHX_ d, "regexp flag /xx needs a newer perl");
1113 # endif
1114 2 case 'n':
1115 # ifdef RXf_PMf_NOCAPTURE
1116 2 fl |= RXf_PMf_NOCAPTURE; break;
1117 # else
1118 sc_dec_croak(aTHX_ d, "regexp flag /n needs a newer perl");
1119 # endif
1120 # if SC_HAVE_CHARSET
1121 1 case 'l': charset = REGEX_LOCALE_CHARSET; break;
1122 3 case 'u': charset = REGEX_UNICODE_CHARSET; break;
1123 8 case 'a': charset = as++ ? REGEX_ASCII_MORE_RESTRICTED_CHARSET
1124 4 ▶ : REGEX_ASCII_RESTRICTED_CHARSET; break;
Branch: 1 branch
BranchTrueFalseTotal
: REGEX_ASCII_RESTRICTED_CHARSET; break; 2 2 4
1125 # endif
1126 2 default:
1127 2 sc_dec_croak(aTHX_ d, "unknown regexp flag");
1128 }
1129 }
1130 # if SC_HAVE_CHARSET
1131 232 ▶ if (charset) set_regex_charset(&fl, (regex_charset)charset);
Branch: 1 branch
BranchTrueFalseTotal
if (charset) set_regex_charset(&fl, (regex_charset)charset); 6 226 232
1132 # endif
1133 /* Compiled at runtime from a string, so a (?{ }) block is refused by the
1134 * regexp engine itself unless the DECODER's caller has `use re 'eval'` in
1135 * force - the same rule as a pattern interpolated from a variable.
1136 *
1137 * The compile runs inside Struct::Codec::_regcomp under G_EVAL, because
1138 * that is the one way to catch the engine's croak cleanly from C and
1139 * report it as this decoder reports everything else: with the byte. A
1140 * bare JMPENV outside an eval frame catches the jump but not before perl
1141 * has printed the message and begun exiting. */
1142 {
1143 232 dSP;
1144 int count;
1145 232 SV *msg = NULL;
1146 232 ENTER;
1147 232 SAVETMPS;
1148 232 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 232 232
1149 232 ▶ XPUSHs(pat);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(pat); 0 232 232
1150 232 ▶ XPUSHs(sv_2mortal(newSVuv((UV)fl)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv((UV)fl))); 0 232 232
1151 232 PUTBACK;
1152 232 count = call_pv("Struct::Codec::_regcomp", G_SCALAR | G_EVAL);
1153 232 SPAGAIN;
1154 232 rx = NULL;
1155 232 ▶ if (count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) { 232 0 232
1156 232 SV *r = POPs;
1157 232 ▶ if (SvROK(r)) rx = (REGEXP *)SvREFCNT_inc_simple_NN(SvRV(r));
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(r)) rx = (REGEXP *)SvREFCNT_inc_simple_NN(SvRV(r)); 230 2 232
1158 }
1159 232 PUTBACK;
1160 232 ▶ if (!rx && SvTRUE(ERRSV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!rx && SvTRUE(ERRSV)) { 2 230 232
if (!rx && SvTRUE(ERRSV)) { 2 0 2
if (!rx && SvTRUE(ERRSV)) { 2 0 2
1161 2 ▶ msg = newSVsv(ERRSV); /* owned: it outlives the scope */
Branch: 1 branch
BranchTrueFalseTotal
msg = newSVsv(ERRSV); /* owned: it outlives the scope */ 2 0 2
1162 2 ▶ if (SvCUR(msg) && SvPVX(msg)[SvCUR(msg) - 1] == '\n')
Branch: 2 branches
BranchTrueFalseTotal
if (SvCUR(msg) && SvPVX(msg)[SvCUR(msg) - 1] == '\n') 2 0 2
if (SvCUR(msg) && SvPVX(msg)[SvCUR(msg) - 1] == '\n') 2 0 2
1163 2 SvCUR_set(msg, SvCUR(msg) - 1);
1164 }
1165 232 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 232 0 232
1166 232 LEAVE;
1167 232 ▶ if (msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg) { 2 230 232
1168 2 sv_2mortal(msg);
1169 2 croak("Struct::Codec: %" SVf " at byte %" UVuf, SVfARG(msg), SC_OFF(d));
1170 }
1171 }
1172 230 ▶ if (!rx) sc_dec_croak(aTHX_ d, "regexp did not compile");
Branch: 1 branch
BranchTrueFalseTotal
if (!rx) sc_dec_croak(aTHX_ d, "regexp did not compile"); 0 230 230
1173 230 return (SV *)rx;
1174 #else
1175 (void)d;
1176 croak("Struct::Codec: a regexp needs perl 5.12 or later");
1177 return NULL;
1178 #endif
1179 }
1180  
1181 /* rule 13: a name in the stream FINDS a sub or a format and never makes one.
1182 * A glob is the exception, because a glob that does not exist is what
1183 * naming it creates in perl too. */
1184 25 static SV *sc_dec_code_name(pTHX_ sc_dec *d) {
1185 25 SV *name = sc_dec_key_sv(aTHX_ d);
1186 GV *gv;
1187 CV *cv;
1188 7 ▶ if (!SvCUR(name)) sc_dec_croak(aTHX_ d, "empty sub name");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvCUR(name)) sc_dec_croak(aTHX_ d, "empty sub name"); 1 6 7
1189 6 gv = gv_fetchsv(name, 0, SVt_PVCV);
1190 6 ▶ cv = gv ? GvCV(gv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cv = gv ? GvCV(gv) : NULL; 5 1 6
1191 6 ▶ if (!cv)
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) 2 4 6
1192 2 croak("Struct::Codec: sub %" SVf " is not defined at byte %" UVuf,
1193 SVfARG(name), SC_OFF(d));
1194 4 return SvREFCNT_inc_simple_NN((SV *)cv);
1195 }
1196  
1197 /* The one place the decoder runs code from the stream, and only when
1198 * $Struct::Codec::Eval allows it, which it does by default: true means eval
1199 * the source as `sub {...}`, false refuses,
1200 * a code reference means hand it the source and take the sub it returns. Its
1201 * scope is its own so the decoder's mortals, made before it, are not swept. */
1202 2 static SV *sc_dec_code_src(pTHX_ sc_dec *d) {
1203 2 SV *src = sc_dec_key_sv(aTHX_ d);
1204 2 SV *ev = get_sv("Struct::Codec::Eval", 0);
1205 2 SV *cv = NULL, *msg = NULL;
1206  
1207 2 ▶ if (!ev || !SvTRUE(ev))
Branch: 2 branches
BranchTrueFalseTotal
if (!ev || !SvTRUE(ev)) 2 0 2
if (!ev || !SvTRUE(ev)) 1 1 2
1208 1 sc_dec_croak(aTHX_ d, "a sub by source needs $Struct::Codec::Eval");
1209 {
1210 1 dSP;
1211 int count;
1212 1 ENTER;
1213 1 SAVETMPS;
1214 1 ▶ if (SvROK(ev) && SvTYPE(SvRV(ev)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(ev) && SvTYPE(SvRV(ev)) == SVt_PVCV) { 0 1 1
if (SvROK(ev) && SvTYPE(SvRV(ev)) == SVt_PVCV) { 0 0 0
1215 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1216 0 ▶ XPUSHs(src);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(src); 0 0 0
1217 0 PUTBACK;
1218 0 count = call_sv(ev, G_SCALAR);
1219 }
1220 else {
1221 1 SV *text = sv_2mortal(newSVpvs("sub "));
1222 1 sv_catsv(text, src);
1223 1 count = eval_sv(text, G_SCALAR);
1224 }
1225 1 SPAGAIN;
1226 1 ▶ if (count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) { 1 0 1
1227 1 SV *r = POPs;
1228 1 ▶ if (SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVCV) 0 1 1
if (SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVCV) 0 0 0
1229 0 cv = SvREFCNT_inc_simple_NN(SvRV(r));
1230 }
1231 1 PUTBACK;
1232 1 ▶ if (!cv && SvTRUE(ERRSV)) msg = newSVsv(ERRSV); /* owned: outlives the scope */
Branch: 4 branches
BranchTrueFalseTotal
if (!cv && SvTRUE(ERRSV)) msg = newSVsv(ERRSV); /* owned: outlives the scope */ 1 0 1
if (!cv && SvTRUE(ERRSV)) msg = newSVsv(ERRSV); /* owned: outlives the scope */ 1 0 1
if (!cv && SvTRUE(ERRSV)) msg = newSVsv(ERRSV); /* owned: outlives the scope */ 1 0 1
if (!cv && SvTRUE(ERRSV)) msg = newSVsv(ERRSV); /* owned: outlives the scope */ 1 0 1
1233 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 0 1
1234 1 LEAVE;
1235 }
1236 1 ▶ if (msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg) { 1 0 1
1237 1 sv_2mortal(msg);
1238 1 croak("Struct::Codec: %" SVf, SVfARG(msg));
1239 }
1240 0 ▶ if (!cv) sc_dec_croak(aTHX_ d, "the source did not produce a sub");
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) sc_dec_croak(aTHX_ d, "the source did not produce a sub"); 0 0 0
1241 0 return cv;
1242 }
1243  
1244 /* Borrowed: a glob in a stash is the stash's. */
1245 28 static GV *sc_dec_glob_named(pTHX_ sc_dec *d) {
1246 28 SV *name = sc_dec_key_sv(aTHX_ d);
1247 GV *gv;
1248 2 ▶ if (!SvCUR(name)) sc_dec_croak(aTHX_ d, "empty glob name");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvCUR(name)) sc_dec_croak(aTHX_ d, "empty glob name"); 1 1 2
1249 1 gv = gv_fetchsv(name, GV_ADD, SVt_PVGV);
1250 1 ▶ if (!gv) sc_dec_croak(aTHX_ d, "glob name is unusable");
Branch: 1 branch
BranchTrueFalseTotal
if (!gv) sc_dec_croak(aTHX_ d, "glob name is unusable"); 0 1 1
1251 1 return gv;
1252 }
1253  
1254 /* A fresh glob, in no stash, holding a DUP of the descriptor: closing this
1255 * one does not close the one it came from, and in a process where the number
1256 * means nothing the open fails and says so. Owned. */
1257 4 static GV *sc_dec_fd_glob(pTHX_ sc_dec *d) {
1258 unsigned char type;
1259 UV fd;
1260 const char *how;
1261 char spec[32];
1262 GV *gv;
1263  
1264 4 ▶ SC_NEED(d, 1);
Branch: 1 branch
BranchTrueFalseTotal
SC_NEED(d, 1); 0 4 4
1265 4 type = *d->p++;
1266 4 fd = sc_dec_varint(aTHX_ d);
1267 3 ▶ if (fd > (UV)INT_MAX) sc_dec_croak(aTHX_ d, "descriptor too large");
Branch: 1 branch
BranchTrueFalseTotal
if (fd > (UV)INT_MAX) sc_dec_croak(aTHX_ d, "descriptor too large"); 1 2 3
1268 2 switch (type) {
1269 1 case '<': case '-': how = "<&"; break; /* read; a pipe read from */
1270 0 case '>': case '|': how = ">&"; break; /* write; a pipe written to */
1271 0 case 'a': how = ">>&"; break;
1272 0 case '+': case 's': case '#': how = "+<&"; break;
1273 1 default: sc_dec_croak(aTHX_ d, "unknown filehandle mode"); how = "";
1274 }
1275 1 my_snprintf(spec, sizeof spec, "%s%d", how, (int)fd);
1276  
1277 1 gv = (GV *)newSV(0);
1278 #if PERL_REVISION > 5 || (PERL_REVISION == 5 && PERL_VERSION >= 16)
1279 1 gv_init_pvn(gv, gv_stashpvs("Struct::Codec", GV_ADD), "FH", 2, 0);
1280 #else
1281 gv_init(gv, gv_stashpvs("Struct::Codec", GV_ADD), "FH", 2, 0);
1282 #endif
1283 1 ▶ if (!do_open(gv, spec, (I32)strlen(spec), FALSE, O_RDONLY, 0, NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!do_open(gv, spec, (I32)strlen(spec), FALSE, O_RDONLY, 0, NULL)) { 1 0 1
1284 1 int err = errno;
1285 1 SvREFCNT_dec((SV *)gv);
1286 1 croak("Struct::Codec: cannot reopen descriptor %d: %s at byte %" UVuf,
1287 (int)fd, Strerror(err), SC_OFF(d));
1288 }
1289 0 return gv;
1290 }
1291  
1292 /* The IO alone, which outlives the glob that opened it. Owned. */
1293 0 static SV *sc_dec_fd_io(pTHX_ sc_dec *d) {
1294 0 GV *gv = sc_dec_fd_glob(aTHX_ d);
1295 0 SV *io = (SV *)GvIOp(gv);
1296 0 ▶ if (!io) { SvREFCNT_dec((SV *)gv); sc_dec_croak(aTHX_ d, "descriptor opened no IO"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!io) { SvREFCNT_dec((SV *)gv); sc_dec_croak(aTHX_ d, "descriptor opened no IO"); } 0 0 0
1297 0 SvREFCNT_inc_simple_void_NN(io);
1298 0 SvREFCNT_dec((SV *)gv);
1299 0 return io;
1300 }
1301  
1302 1 static SV *sc_dec_format(pTHX_ sc_dec *d) {
1303 1 SV *name = sc_dec_key_sv(aTHX_ d);
1304 GV *gv;
1305 CV *fm;
1306 1 ▶ if (!SvCUR(name)) sc_dec_croak(aTHX_ d, "empty format name");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvCUR(name)) sc_dec_croak(aTHX_ d, "empty format name"); 0 1 1
1307 1 gv = gv_fetchsv(name, 0, SVt_PVFM);
1308 1 ▶ fm = gv ? GvFORM(gv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
fm = gv ? GvFORM(gv) : NULL; 0 1 1
1309 1 ▶ if (!fm)
Branch: 1 branch
BranchTrueFalseTotal
if (!fm) 1 0 1
1310 1 croak("Struct::Codec: format %" SVf " is not defined at byte %" UVuf,
1311 SVfARG(name), SC_OFF(d));
1312 0 return SvREFCNT_inc_simple_NN((SV *)fm);
1313 }
1314  
1315 /* The tie object, decoded through the ordinary path so that a stream which
1316 * shares it registers it as it would anything else; the mortal it lands in is
1317 * what the magic then holds its own reference to. */
1318 33 static SV *sc_dec_tie_obj(pTHX_ sc_dec *d) {
1319 33 SV *obj = sv_newmortal();
1320 33 SV *shared = sc_dec_value(aTHX_ d, obj);
1321 13 ▶ if (shared) obj = shared;
Branch: 1 branch
BranchTrueFalseTotal
if (shared) obj = shared; 0 13 13
1322 13 ▶ if (!SvROK(obj)) sc_dec_croak(aTHX_ d, "tie object is not a reference");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj)) sc_dec_croak(aTHX_ d, "tie object is not a reference"); 2 11 13
1323 11 return obj;
1324 }
1325  
1326 /* rule 8, widened: an ALIAS may put only a SCALAR in a slot. A container, a
1327 * glob, a sub, a format, an IO or a regexp placed where an SV belongs corrupts
1328 * the interpreter silently. */
1329 5 static int sc_slot_safe(pTHX_ SV *sv) {
1330 5 ▶ if (SvTYPE(sv) >= SVt_PVAV || SvTYPE(sv) == SVt_PVGV) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(sv) >= SVt_PVAV || SvTYPE(sv) == SVt_PVGV) return 0; 3 2 5
if (SvTYPE(sv) >= SVt_PVAV || SvTYPE(sv) == SVt_PVGV) return 0; 1 2 3
1331 2 return !sc_is_regexp(aTHX_ sv);
1332 }
1333  
1334 /* The parent has attached `slot` and decoded into it; an ALIAS answered with
1335 * the SV that must be there instead. Each parent kind swaps it in its own way,
1336 * and the placeholder goes with the swap. */
1337 5 static void sc_dec_swap_av(pTHX_ AV *av, SSize_t i, SV *shared) {
1338 5 SvREFCNT_inc_simple_void_NN(shared);
1339 5 (void)av_store(av, i, shared);
1340 5 }
1341  
1342 /* The thing behind a REF/OBJECT tag. `rv` is already attached to the parent;
1343 * the referent is made, the reference pointed at it, THEN it is filled (rule
1344 * 5), and blessed last (rule 10) so no DESTROY sees a half-built object. */
1345 28142 static void sc_dec_referent(pTHX_ sc_dec *d, SV *rv, STRLEN tag_off, int track,
1346 HV *stash)
1347 {
1348 unsigned char t, kind;
1349 SV *target;
1350 28142 int plain = 0, tied = 0;
1351  
1352 28142 ▶ SC_NEED(d, 1);
Branch: 1 branch
BranchTrueFalseTotal
SC_NEED(d, 1); 108 28034 28142
1353 28034 t = *d->p;
1354 28034 kind = SC_KIND(t);
1355 28034 switch (kind) {
1356 26740 case SC_T_ARRAY: case SC_T_HASH:
1357 /* the REF tag carried the TRACK bit; one here is corrupt */
1358 26740 ▶ if (t & SC_TRACK) sc_dec_croak(aTHX_ d, "tracked container tag");
Branch: 1 branch
BranchTrueFalseTotal
if (t & SC_TRACK) sc_dec_croak(aTHX_ d, "tracked container tag"); 1 26739 26740
1359 26739 d->p++;
1360 26739 ▶ target = kind == SC_T_ARRAY ? (SV *)newAV() : (SV *)newHV();
Branch: 1 branch
BranchTrueFalseTotal
target = kind == SC_T_ARRAY ? (SV *)newAV() : (SV *)newHV(); 23152 3587 26739
1361 26739 plain = 1;
1362 26739 break;
1363 28 case SC_T_TIED_ARRAY: case SC_T_TIED_HASH:
1364 28 ▶ if (t & SC_TRACK) sc_dec_croak(aTHX_ d, "tracked container tag");
Branch: 1 branch
BranchTrueFalseTotal
if (t & SC_TRACK) sc_dec_croak(aTHX_ d, "tracked container tag"); 1 27 28
1365 27 d->p++;
1366 27 ▶ target = kind == SC_T_TIED_ARRAY ? (SV *)newAV() : (SV *)newHV();
Branch: 1 branch
BranchTrueFalseTotal
target = kind == SC_T_TIED_ARRAY ? (SV *)newAV() : (SV *)newHV(); 3 24 27
1367 27 tied = 1;
1368 27 break;
1369 290 case SC_T_REGEXP: case SC_T_CODE_NAME: case SC_T_CODE_SRC:
1370 case SC_T_GLOB: case SC_T_FD_GLOB: case SC_T_FD_IO:
1371 case SC_T_FORMAT:
1372 290 ▶ if (t & SC_TRACK) sc_dec_croak(aTHX_ d, "tracked referent tag");
Branch: 1 branch
BranchTrueFalseTotal
if (t & SC_TRACK) sc_dec_croak(aTHX_ d, "tracked referent tag"); 1 289 290
1373 289 d->p++;
1374 /* built complete, or a croak that built nothing */
1375 289 switch (kind) {
1376 242 case SC_T_REGEXP: target = sc_dec_regexp(aTHX_ d); break;
1377 25 case SC_T_CODE_NAME: target = sc_dec_code_name(aTHX_ d); break;
1378 2 case SC_T_CODE_SRC: target = sc_dec_code_src(aTHX_ d); break;
1379 15 case SC_T_GLOB:
1380 15 target = (SV *)sc_dec_glob_named(aTHX_ d);
1381 1 SvREFCNT_inc_simple_void_NN(target);
1382 1 break;
1383 4 case SC_T_FD_GLOB: target = (SV *)sc_dec_fd_glob(aTHX_ d); break;
1384 0 case SC_T_FD_IO: target = sc_dec_fd_io(aTHX_ d); break;
1385 1 default: target = sc_dec_format(aTHX_ d); break;
1386 }
1387 /* qr// is blessed into Regexp, and the tag implied it */
1388 235 ▶ if (kind == SC_T_REGEXP && !stash) stash = gv_stashpvs("Regexp", GV_ADD);
Branch: 2 branches
BranchTrueFalseTotal
if (kind == SC_T_REGEXP && !stash) stash = gv_stashpvs("Regexp", GV_ADD); 230 5 235
if (kind == SC_T_REGEXP && !stash) stash = gv_stashpvs("Regexp", GV_ADD); 228 2 230
1389 235 break;
1390 976 default:
1391 976 target = newSV(0);
1392 }
1393 /* attach: the RV owns the referent from here, so a croak below frees it */
1394 27977 ▶ SvUPGRADE(rv, SC_SVt_REF);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(rv, SC_SVt_REF); 27977 0 27977
1395 27977 SvRV_set(rv, target);
1396 27977 SvROK_on(rv);
1397 27977 ▶ if (track) sc_dec_register(aTHX_ d, tag_off, rv, 1);
Branch: 1 branch
BranchTrueFalseTotal
if (track) sc_dec_register(aTHX_ d, tag_off, rv, 1); 2966 25011 27977
1398  
1399 27977 ▶ if (tied) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied) { 27 27950 27977
1400 /* Registered above, so a tie object that refers back to its own
1401 * variable finds it. sv_magic takes its own reference to the object
1402 * and calls nothing: no TIEHASH, and nothing until the caller reads. */
1403 27 SV *obj = sc_dec_tie_obj(aTHX_ d);
1404 6 sv_magic(target, obj, PERL_MAGIC_tied, NULL, 0);
1405 }
1406 27950 ▶ else if (!plain && SvTYPE(target) != SVt_NULL) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!plain && SvTYPE(target) != SVt_NULL) { 1211 26739 27950
else if (!plain && SvTYPE(target) != SVt_NULL) { 235 976 1211
1407 /* one of the kinds built complete above: nothing to fill */
1408 }
1409 27715 ▶ else if (SvTYPE(target) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTYPE(target) == SVt_PVAV) { 23152 4563 27715
1410 23152 UV n = sc_dec_varint(aTHX_ d), i;
1411 23080 ▶ SC_NEED(d, n); /* a value is at least one byte */
Branch: 1 branch
BranchTrueFalseTotal
SC_NEED(d, n); /* a value is at least one byte */ 58 23022 23080
1412 23022 ▶ if (n) av_extend((AV *)target, (SSize_t)n - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (n) av_extend((AV *)target, (SSize_t)n - 1); 22465 557 23022
1413 140718 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 127125 13593 140718
1414 127125 SV *slot = newSV(0), *shared;
1415 127125 av_push((AV *)target, slot); /* attached before filled */
1416 127125 shared = sc_dec_value(aTHX_ d, slot);
1417 117696 ▶ if (shared) sc_dec_swap_av(aTHX_ (AV *)target, (SSize_t)i, shared);
Branch: 1 branch
BranchTrueFalseTotal
if (shared) sc_dec_swap_av(aTHX_ (AV *)target, (SSize_t)i, shared); 5 117691 117696
1418 }
1419 }
1420 4563 ▶ else if (SvTYPE(target) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTYPE(target) == SVt_PVHV) { 3587 976 4563
1421 3587 UV n = sc_dec_varint(aTHX_ d), i;
1422 3571 ▶ if (n > (UV)(d->end - d->p) / 2) /* a key varint and a value tag */
Branch: 1 branch
BranchTrueFalseTotal
if (n > (UV)(d->end - d->p) / 2) /* a key varint and a value tag */ 32 3539 3571
1423 32 sc_dec_croak(aTHX_ d, "truncated input");
1424 11678 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9087 2591 11678
1425 const char *k; STRLEN kl; int ku;
1426 SV **svp, *shared;
1427 9087 UV before = HvTOTALKEYS((HV *)target);
1428 9087 sc_dec_key(aTHX_ d, &k, &kl, &ku);
1429 /* ONE lookup: an lvalue fetch makes the entry with a fresh undef
1430 * SV already attached, which is exactly the placeholder wanted.
1431 * rule 6: a duplicate is refused BEFORE a store could free the
1432 * earlier value, which a later REFP may still name - and an
1433 * lvalue fetch of an existing key frees nothing, it hands back
1434 * the value that is there, so the key count not having moved is
1435 * the whole test. */
1436 8773 svp = (SV **)hv_common((HV *)target, NULL, k, (I32)kl,
1437 ku ? HVhek_UTF8 : 0,
1438 HV_FETCH_LVALUE | HV_FETCH_JUST_SV, NULL, 0);
1439 8773 ▶ if (!svp || HvTOTALKEYS((HV *)target) == before)
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || HvTOTALKEYS((HV *)target) == before) 8773 0 8773
if (!svp || HvTOTALKEYS((HV *)target) == before) 2 8771 8773
1440 2 sc_dec_croak(aTHX_ d, "duplicate hash key");
1441 8771 shared = sc_dec_value(aTHX_ d, *svp);
1442 8139 ▶ if (shared) {
Branch: 1 branch
BranchTrueFalseTotal
if (shared) { 0 8139 8139
1443 0 SvREFCNT_inc_simple_void_NN(shared);
1444 0 (void)hv_common((HV *)target, NULL, k, (I32)kl,
1445 ku ? HVhek_UTF8 : 0, HV_FETCH_ISSTORE, shared, 0);
1446 }
1447 }
1448 }
1449 else {
1450 976 SV *shared = sc_dec_value(aTHX_ d, target);
1451 878 ▶ if (shared) {
Branch: 1 branch
BranchTrueFalseTotal
if (shared) { 0 878 878
1452 0 SvREFCNT_inc_simple_void_NN(shared);
1453 0 SvRV_set(rv, shared);
1454 0 SvREFCNT_dec(target);
1455 }
1456 }
1457  
1458 /* rule 10 has a second reason now: a referent that would become a pointer
1459 * object is refused BEFORE the bless, so it is freed as the plain integer
1460 * it is and the class's DESTROY never runs on it. Streams written before
1461 * the encoder refused the shape are how one arrives. */
1462 17303 ▶ if (stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (stash) { 1168 16135 17303
1463 1168 ▶ if (sc_is_pointer_obj(aTHX_ SvRV(rv), stash))
Branch: 1 branch
BranchTrueFalseTotal
if (sc_is_pointer_obj(aTHX_ SvRV(rv), stash)) 1 1167 1168
1464 1 ▶ croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf,
Branch: 12 branches
BranchTrueFalseTotal
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 1 0 1
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 1 0 1
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 0 1 1
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 0 0 0
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 1 0 1
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 0 1 1
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 1 0 1
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 1 0 1
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 0 1 1
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 0 0 0
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 1 0 1
croak("Struct::Codec: a %.*s object that holds a pointer at byte %" UVuf, 0 1 1
1465 (int)HvNAMELEN_get(stash), HvNAME(stash), SC_OFF(d));
1466 1167 sv_bless(rv, stash);
1467 }
1468 17302 }
1469  
1470 /* Fill `into`, which the caller has already attached. Returns NULL, or the SV
1471 * the caller must put in `into`'s place because the stream said the slot IS
1472 * that SV. */
1473 141235 static SV *sc_dec_value(pTHX_ sc_dec *d, SV *into) {
1474 unsigned char t, kind;
1475 STRLEN tag_off;
1476 int track;
1477 141235 SV *shared = NULL;
1478  
1479 141235 ▶ if (++d->depth > SC_DEPTH_MAX) /* rule 3 */
Branch: 1 branch
BranchTrueFalseTotal
if (++d->depth > SC_DEPTH_MAX) /* rule 3 */ 2 141233 141235
1480 2 sc_dec_croak(aTHX_ d, "structure too deep");
1481  
1482 141233 ▶ SC_NEED(d, 1);
Branch: 1 branch
BranchTrueFalseTotal
SC_NEED(d, 1); 114 141119 141233
1483 141119 tag_off = (STRLEN)SC_OFF(d);
1484 141119 t = *d->p++;
1485 141119 kind = SC_KIND(t);
1486 141119 track = (t & SC_TRACK) ? 1 : 0;
1487  
1488 141119 ▶ if (kind >= SC_T_SHORT_BYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if (kind >= SC_T_SHORT_BYTES) { 4508 136611 141119
1489 4508 sc_dec_str_into(aTHX_ d, into, SC_SHORT_LEN(kind),
1490 kind >= SC_T_SHORT_UTF8 ? 1 : 0);
1491 }
1492 136611 ▶ else if (kind < 0x20) {
Branch: 1 branch
BranchTrueFalseTotal
else if (kind < 0x20) { 2126 134485 136611
1493 2126 ▶ sv_setiv(into, (kind & 0x10) ? (IV)(kind & 0x0F) - 16 : (IV)kind);
Branch: 1 branch
BranchTrueFalseTotal
sv_setiv(into, (kind & 0x10) ? (IV)(kind & 0x0F) - 16 : (IV)kind); 7 2119 2126
1494 }
1495 134485 else switch (kind) {
1496 1884 case SC_T_UV: {
1497 1884 UV u = sc_dec_varint(aTHX_ d);
1498 1826 ▶ if (u <= (UV)IV_MAX) sv_setiv(into, (IV)u); else sv_setuv(into, u);
Branch: 1 branch
BranchTrueFalseTotal
if (u <= (UV)IV_MAX) sv_setiv(into, (IV)u); else sv_setuv(into, u); 1822 4 1826
1499 1826 break;
1500 }
1501 1026 case SC_T_NEG: {
1502 1026 UV u = sc_dec_varint(aTHX_ d);
1503 994 ▶ if (u > (UV)IV_MAX) sc_dec_croak(aTHX_ d, "negative integer too wide");
Branch: 1 branch
BranchTrueFalseTotal
if (u > (UV)IV_MAX) sc_dec_croak(aTHX_ d, "negative integer too wide"); 1 993 994
1504 993 sv_setiv(into, -(IV)u - 1);
1505 993 break;
1506 }
1507 1846 case SC_T_NV:
1508 1846 ▶ SC_NEED(d, SC_NV_BYTES);
Branch: 1 branch
BranchTrueFalseTotal
SC_NEED(d, SC_NV_BYTES); 97 1749 1846
1509 1749 sv_setnv(into, sc_nv_from_wire(d->p));
1510 1749 d->p += SC_NV_BYTES;
1511 1749 break;
1512 13 case SC_T_NV_STR: {
1513 /* Decimal digits from a perl whose NV is wider than a double, read at
1514 * this perl's NV width: on a wide perl that returns the value the writer
1515 * had, and on a double perl the nearest double, which is the best it
1516 * could hold. The parser is named for the build rather than taken from
1517 * Atof, which below 5.30 is a ULP or two out at the top of a long double
1518 * - see the encoder's note.
1519 *
1520 * Copied out to get the NUL the parser needs - the stream has no room
1521 * for one and must not be written to. A length past the buffer is
1522 * corrupt input and is refused, not truncated, so the stack read stays
1523 * inside what the varint was checked against. */
1524 13 UV n = sc_dec_varint(aTHX_ d);
1525 char buf[SC_NV_STR_MAX + 1];
1526 12 ▶ if (n > (UV)SC_NV_STR_MAX) sc_dec_croak(aTHX_ d, "float too long");
Branch: 1 branch
BranchTrueFalseTotal
if (n > (UV)SC_NV_STR_MAX) sc_dec_croak(aTHX_ d, "float too long"); 2 10 12
1527 10 ▶ SC_NEED(d, n);
Branch: 1 branch
BranchTrueFalseTotal
SC_NEED(d, n); 1 9 10
1528 9 Copy(d->p, buf, (STRLEN)n, char);
1529 9 buf[n] = '\0';
1530 9 d->p += n;
1531 9 sv_setnv(into, SC_NV_FROM_STR(buf));
1532 9 break;
1533 }
1534 787 case SC_T_UNDEF: break; /* `into` is always a fresh SV, which is undef */
1535 1 case SC_T_TRUE: sv_setsv(into, &PL_sv_yes); break;
1536 1 case SC_T_FALSE: sv_setsv(into, &PL_sv_no); break;
1537 1798 case SC_T_BYTES:
1538 case SC_T_UTF8: {
1539 1798 UV n = sc_dec_varint(aTHX_ d);
1540 1792 sc_dec_str_into(aTHX_ d, into, (STRLEN)n, kind == SC_T_UTF8);
1541 1549 break;
1542 }
1543 26884 case SC_T_REF:
1544 26884 sc_dec_referent(aTHX_ d, into, tag_off, track, NULL);
1545 16363 track = 0; /* registered inside, as the RV */
1546 16363 break;
1547 1307 case SC_T_OBJECT: {
1548 const char *cn; STRLEN cl; int cu;
1549 HV *stash;
1550 1307 sc_dec_key(aTHX_ d, &cn, &cl, &cu);
1551 1259 ▶ if (!cl) sc_dec_croak(aTHX_ d, "empty class name");
Branch: 1 branch
BranchTrueFalseTotal
if (!cl) sc_dec_croak(aTHX_ d, "empty class name"); 1 1258 1259
1552 /* trusted contents: the stash is created if it does not exist */
1553 #ifdef SVf_UTF8
1554 1258 ▶ stash = gv_stashpvn(cn, cl, GV_ADD | (cu ? SVf_UTF8 : 0));
Branch: 1 branch
BranchTrueFalseTotal
stash = gv_stashpvn(cn, cl, GV_ADD | (cu ? SVf_UTF8 : 0)); 426 832 1258
1555 #else
1556 stash = gv_stashpvn(cn, cl, GV_ADD);
1557 #endif
1558 1258 sc_dec_referent(aTHX_ d, into, tag_off, track, stash);
1559 939 track = 0;
1560 939 break;
1561 }
1562 98900 case SC_T_REFP: {
1563 sc_slot *s;
1564 98900 ▶ if (track) sc_dec_croak(aTHX_ d, "tracked reference tag");
Branch: 1 branch
BranchTrueFalseTotal
if (track) sc_dec_croak(aTHX_ d, "tracked reference tag"); 1 98899 98900
1565 98899 s = sc_dec_lookup(aTHX_ d, sc_dec_varint(aTHX_ d));
1566 98891 ▶ if (s->isref) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->isref) { 98890 1 98891
1567 98890 sv_setsv(into, s->sv); /* a new RV to the same referent */
1568 }
1569 else {
1570 1 SV *r = newRV_inc(s->sv); /* a reference TO that scalar */
1571 1 sv_setsv(into, r);
1572 1 SvREFCNT_dec(r);
1573 }
1574 98891 break;
1575 }
1576 12 case SC_T_ALIAS: {
1577 sc_slot *s;
1578 12 ▶ if (track) sc_dec_croak(aTHX_ d, "tracked alias tag");
Branch: 1 branch
BranchTrueFalseTotal
if (track) sc_dec_croak(aTHX_ d, "tracked alias tag"); 1 11 12
1579 11 s = sc_dec_lookup(aTHX_ d, sc_dec_varint(aTHX_ d));
1580 9 ▶ if (s->isref) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->isref) { 5 4 9
1581 /* the slot IS the referent: legal for a scalar referent only.
1582 * rule 8: anything else placed where an SV belongs corrupts perl */
1583 5 shared = SvRV(s->sv);
1584 5 ▶ if (!sc_slot_safe(aTHX_ shared))
Branch: 1 branch
BranchTrueFalseTotal
if (!sc_slot_safe(aTHX_ shared)) 4 1 5
1585 4 sc_dec_croak(aTHX_ d, "alias of a container");
1586 }
1587 else {
1588 4 shared = s->sv;
1589 }
1590 5 break;
1591 }
1592 6 case SC_T_TIED_SCALAR: {
1593 /* `into` is attached and undef; the magic makes it the tie */
1594 6 SV *obj = sc_dec_tie_obj(aTHX_ d);
1595 5 sv_magic(into, obj, PERL_MAGIC_tiedscalar, NULL, 0);
1596 5 break;
1597 }
1598 13 case SC_T_GLOB:
1599 /* a glob in a slot: `$x = *STDOUT`, which sv_setsv reproduces */
1600 13 sv_setsv(into, (SV *)sc_dec_glob_named(aTHX_ d));
1601 0 break;
1602 0 case SC_T_FD_GLOB: {
1603 0 GV *gv = sc_dec_fd_glob(aTHX_ d);
1604 0 sv_setsv(into, (SV *)gv);
1605 0 SvREFCNT_dec((SV *)gv);
1606 0 break;
1607 }
1608 3 case SC_T_ARRAY:
1609 case SC_T_HASH:
1610 case SC_T_TIED_ARRAY:
1611 case SC_T_TIED_HASH:
1612 3 sc_dec_croak(aTHX_ d, "container without a reference");
1613 0 break;
1614 2 case SC_T_REGEXP:
1615 case SC_T_CODE_NAME:
1616 case SC_T_CODE_SRC:
1617 case SC_T_FD_IO:
1618 case SC_T_FORMAT:
1619 2 sc_dec_croak(aTHX_ d, "referent without a reference");
1620 0 break;
1621 2 default:
1622 2 sc_dec_croak(aTHX_ d, "unknown tag");
1623 }
1624  
1625 129570 ▶ if (track) sc_dec_register(aTHX_ d, tag_off, into, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (track) sc_dec_register(aTHX_ d, tag_off, into, 0); 6 129564 129570
1626 129570 d->depth--;
1627 129570 return shared;
1628 }
1629  
1630 /* An owned SV. */
1631 4649 static SV *sc_decode(pTHX_ const char *bytes, STRLEN len) {
1632 sc_dec d;
1633 SV *root, *shared;
1634  
1635 4649 memset(&d, 0, sizeof d);
1636 4649 d.base = d.p = (const unsigned char *)bytes;
1637 4649 d.end = d.base + len;
1638  
1639 4649 ▶ SC_NEED(&d, SC_HDR_LEN); /* rule 11 */
Branch: 1 branch
BranchTrueFalseTotal
SC_NEED(&d, SC_HDR_LEN); /* rule 11 */ 316 4333 4649
1640 4333 ▶ if (d.p[0] != SC_MAGIC) sc_dec_croak(aTHX_ &d, "not a Struct::Codec stream");
Branch: 1 branch
BranchTrueFalseTotal
if (d.p[0] != SC_MAGIC) sc_dec_croak(aTHX_ &d, "not a Struct::Codec stream"); 1 4332 4333
1641 4332 ▶ if (d.p[1] != SC_VERSION) sc_dec_croak(aTHX_ &d, "unknown format version");
Branch: 1 branch
BranchTrueFalseTotal
if (d.p[1] != SC_VERSION) sc_dec_croak(aTHX_ &d, "unknown format version"); 1 4331 4332
1642 4331 ▶ if (d.p[2] != SC_FLOAT) sc_dec_croak(aTHX_ &d, "unknown float encoding");
Branch: 1 branch
BranchTrueFalseTotal
if (d.p[2] != SC_FLOAT) sc_dec_croak(aTHX_ &d, "unknown float encoding"); 1 4330 4331
1643 4330 d.p += SC_HDR_LEN;
1644  
1645 4330 root = sv_newmortal(); /* the mortal root: a croak frees it all */
1646 4330 shared = sc_dec_value(aTHX_ &d, root);
1647 2844 ▶ if (shared) sc_dec_croak(aTHX_ &d, "alias at the root"); /* nothing precedes it */
Branch: 1 branch
BranchTrueFalseTotal
if (shared) sc_dec_croak(aTHX_ &d, "alias at the root"); /* nothing precedes it */ 0 2844 2844
1648 2844 ▶ if (d.p != d.end) sc_dec_croak(aTHX_ &d, "trailing bytes"); /* rule 9 */
Branch: 1 branch
BranchTrueFalseTotal
if (d.p != d.end) sc_dec_croak(aTHX_ &d, "trailing bytes"); /* rule 9 */ 2 2842 2844
1649  
1650 2842 return SvREFCNT_inc_simple_NN(root);
1651 }
1652  
1653 #endif /* SC_CODEC_H */
« xs/abi.xs ↑ Summary Codec.xs »