srl_encoder.c

filter: Statementstmt 25.7% 398 / 1544 Branchbran 9.9% 221 / 2212 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 16.4% 619 / 3756 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« srl_compress.h ↑ Summary srl_checksum_policy.h »
1 /* Must be defined before including Perl header files or we slow down by 2x! */
2 #define PERL_NO_GET_CONTEXT
3  
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 #include "EXTERN.h"
8 #include "perl.h"
9 #include "XSUB.h"
10 #include "ppport.h"
11 #ifdef __cplusplus
12 }
13 #endif
14  
15 #include <stdlib.h>
16 #include <string.h>
17  
18 #ifndef PERL_VERSION
19 # include <patchlevel.h>
20 # if !(defined(PERL_VERSION) || (PERL_SUBVERSION > 0 && defined(PATCHLEVEL)))
21 # include <could_not_find_Perl_patchlevel.h>
22 # endif
23 # define PERL_REVISION 5
24 # define PERL_VERSION PATCHLEVEL
25 # define PERL_SUBVERSION PERL_SUBVERSION
26 #endif
27 #if PERL_VERSION < 8
28 # define PERL_MAGIC_qr 'r' /* precompiled qr// regex */
29 # define BFD_Svs_SMG_OR_RMG SVs_RMG
30 #elif ((PERL_VERSION==8) && (PERL_SUBVERSION >= 1) || (PERL_VERSION>8))
31 # define BFD_Svs_SMG_OR_RMG SVs_SMG
32 # define MY_PLACEHOLDER PL_sv_placeholder
33 #else
34 # define BFD_Svs_SMG_OR_RMG SVs_RMG
35 # define MY_PLACEHOLDER PL_sv_undef
36 #endif
37 #if (((PERL_VERSION == 9) && (PERL_SUBVERSION >= 4)) || (PERL_VERSION > 9))
38 # define NEW_REGEX_ENGINE 1
39 #endif
40 #if (((PERL_VERSION == 8) && (PERL_SUBVERSION >= 1)) || (PERL_VERSION > 8))
41 #define MY_CAN_FIND_PLACEHOLDERS
42 #define HAS_SV2OBJ
43 #endif
44  
45 /* Marks hash keys that are not in the shared string table; the flag was
46 * renamed across perl versions. */
47 #if defined(HVhek_NOTSHARED)
48 #define SRL_HVhek_NOTSHARED HVhek_NOTSHARED
49 #elif defined(HVhek_UNSHARED)
50 #define SRL_HVhek_NOTSHARED HVhek_UNSHARED
51 #else
52 #define SRL_HVhek_NOTSHARED 0
53 #endif
54  
55 /* hv_backreferences_p is not marked as exported in embed.fnc in any perl */
56 #if (PERL_VERSION >= 10)
57 #define HAS_HV_BACKREFS
58 #endif
59  
60 #include "srl_protocol.h"
61 #include "srl_encoder.h"
62 #include "srl_common.h"
63 #include "srl_float.h"
64 #include "ptable.h"
65 #include "srl_buffer.h"
66 #include "srl_compress.h"
67 #include "qsort.h"
68 #include "srl_siphash.h"
69 #include "srl_checksum_policy.h"
70  
71 SRL_STATIC_INLINE void
72 0 srl_encode_u64_le(srl_u64 value, U8 out[8])
73 {
74 U8 i;
75 0 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 0 0 0
76 0 out[i] = (U8)(value >> (8 * i));
77 0 }
78  
79 SRL_STATIC_INLINE size_t
80 0 srl_encode_varint_uv(UV value, U8 *out)
81 {
82 0 size_t len = 0;
83 0 ▶ while (value >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
while (value >= 0x80) { 0 0 0
84 0 out[len++] = (U8)((value & 0x7f) | 0x80);
85 0 value >>= 7;
86 }
87 0 out[len++] = (U8)value;
88 0 return len;
89 }
90  
91 /* Add the version-6 integrity envelope around the existing raw/compressed
92 * payload. The MAC covers the packet header, checksum version, payload length,
93 * and payload; only the tag bytes themselves are excluded. */
94 SRL_STATIC_INLINE void
95 0 srl_protect_packet(pTHX_ srl_encoder_t *enc)
96 {
97 U8 checksum_len_bytes[SRL_MAX_VARINT_LENGTH];
98 U8 payload_len_bytes[SRL_MAX_VARINT_LENGTH];
99 U8 tag_bytes[SRL_CHECKSUM_TAG_LENGTH_128];
100 size_t tag_len;
101 size_t checksum_len_varint_size;
102 size_t payload_len_varint_size;
103 size_t prefix_len;
104 size_t payload_len;
105 size_t extra_len;
106 const U8 *p;
107 U8 *envelope;
108 0 UV suffix_len = 0;
109 0 unsigned int shift = 0;
110 U8 byte;
111 U8 *tag;
112 U8 *payload_len_field;
113 U8 *payload;
114 srl_u64 digest;
115 srl_siphash_ctx sip;
116  
117 0 p = (const U8 *)enc->buf.start + SRL_MAGIC_STRLEN + 1;
118 do {
119 0 ▶ if (p >= (const U8 *)enc->buf.pos)
Branch: 1 branch
BranchTrueFalseTotal
if (p >= (const U8 *)enc->buf.pos) 0 0 0
120 0 croak("Internal error: missing Sereal header suffix length");
121 0 byte = *p++;
122 0 ▶ if (shift >= sizeof(UV) * 8 ||
Branch: 2 branches
BranchTrueFalseTotal
if (shift >= sizeof(UV) * 8 || 0 0 0
if (shift >= sizeof(UV) * 8 || 0 0 0
123 0 ▶ (shift == sizeof(UV) * 8 - 1 && (byte & 0x7e)))
Branch: 1 branch
BranchTrueFalseTotal
(shift == sizeof(UV) * 8 - 1 && (byte & 0x7e))) 0 0 0
124 0 croak("Internal error: invalid Sereal header suffix length");
125 0 suffix_len |= ((UV)(byte & 0x7f)) << shift;
126 0 shift += 7;
127 0 ▶ } while (byte & 0x80);
Branch: 1 branch
BranchTrueFalseTotal
} while (byte & 0x80); 0 0 0
128  
129 0 ▶ if (suffix_len > (UV)((const U8 *)enc->buf.pos - p))
Branch: 1 branch
BranchTrueFalseTotal
if (suffix_len > (UV)((const U8 *)enc->buf.pos - p)) 0 0 0
130 0 croak("Internal error: Sereal header suffix exceeds packet");
131 0 p += (size_t)suffix_len;
132 0 prefix_len = (size_t)(p - (const U8 *)enc->buf.start);
133 0 payload_len = (size_t)((const U8 *)enc->buf.pos - p);
134 if ((size_t)(UV)payload_len != payload_len)
135 croak("Sereal packet payload is too large for this platform's UV type");
136  
137 0 tag_len = srl_checksum_tag_length(enc->siphash_version);
138  
139 0 checksum_len_varint_size = srl_encode_varint_uv((UV)tag_len, checksum_len_bytes);
140 0 payload_len_varint_size = srl_encode_varint_uv((UV)payload_len, payload_len_bytes);
141 0 extra_len = 1 + checksum_len_varint_size + tag_len + payload_len_varint_size;
142 0 ▶ BUF_SIZE_ASSERT(&enc->buf, extra_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(&enc->buf, extra_len); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, extra_len); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, extra_len); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, extra_len); 0 0 0
143  
144 /* BUF_SIZE_ASSERT may relocate the buffer. Recompute packet pointers. */
145 0 envelope = (U8 *)enc->buf.start + prefix_len;
146 0 p = envelope;
147 0 payload_len = (size_t)((const U8 *)enc->buf.pos - p);
148 0 memmove(envelope + extra_len, p, payload_len);
149 0 envelope[0] = enc->siphash_version;
150 0 Copy(checksum_len_bytes, envelope + 1, checksum_len_varint_size, U8);
151 0 tag = envelope + 1 + checksum_len_varint_size;
152 0 Zero(tag, tag_len, U8);
153 0 payload_len_field = tag + tag_len;
154 0 Copy(payload_len_bytes, payload_len_field, payload_len_varint_size, U8);
155 0 payload = payload_len_field + payload_len_varint_size;
156 0 enc->buf.pos += extra_len;
157  
158 /* The high bit of the document-type nibble marks the protected form. */
159 0 enc->buf.start[SRL_MAGIC_STRLEN] |= SRL_PROTOCOL_ENCODING_CHECKSUM;
160  
161 0 srl_siphash_init(&sip, enc->checksum_key, enc->siphash_version);
162 0 srl_siphash_update(&sip, (const U8 *)enc->buf.start, (size_t)(tag - (const U8 *)enc->buf.start));
163 0 srl_siphash_update(&sip, payload_len_field, payload_len_varint_size);
164 0 srl_siphash_update(&sip, payload, payload_len);
165 0 ▶ if (tag_len == SRL_CHECKSUM_TAG_LENGTH_128) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag_len == SRL_CHECKSUM_TAG_LENGTH_128) { 0 0 0
166 0 srl_siphash_final128(&sip, tag_bytes);
167 0 Copy(tag_bytes, tag, SRL_CHECKSUM_TAG_LENGTH_128, U8);
168 } else {
169 0 digest = srl_siphash_final64(&sip);
170 0 srl_encode_u64_le(digest, tag);
171 }
172 0 }
173  
174 /* The ENABLE_DANGEROUS_HACKS (passed through from ENV via Makefile.PL) enables
175 * optimizations that may make the code so cozy with a particular version of the
176 * Perl core that the code is no longer portable and/or compatible.
177 * It would be great to determine where these hacks are safe and enable them
178 * where possible. Gut feeling as for portability is that most things will be
179 * ok on Unixes, but fail on the stricter Win32. As for compatibility with old
180 * versions of perl, all bets are off.
181 */
182 #ifdef ENABLE_DANGEROUS_HACKS
183 /* It's unclear why DO_SHARED_HASH_ENTRY_REFCOUNT_CHECK doesn't
184 * help much. It basically means breaking perl's encapsulation to
185 * check whether a HE (hash entry) that is shared has a refcount > 1
186 * and only bothers inserting key into our ptr table if that's the
187 * case. Benchmarks don't show much of a difference and it's a high
188 * price to pay to break encapsulation for something that's not
189 * measureable.
190 */
191 /* DO_SHARED_HASH_ENTRY_REFCOUNT_CHECK only works on 5.10 and better */
192 # define DO_SHARED_HASH_ENTRY_REFCOUNT_CHECK 1
193 #else
194 # define DO_SHARED_HASH_ENTRY_REFCOUNT_CHECK 0
195 #endif
196  
197 #if defined(__CYGWIN__)
198 /* Cygwin's main thread stack is set by the Perl executable. */
199 #define DEFAULT_MAX_RECUR_DEPTH 4096
200 #else
201 #define DEFAULT_MAX_RECUR_DEPTH 8192
202 #endif
203 #define PARANOID_MAX_RECUR_DEPTH 2048
204  
205 #define DEBUGHACK 0
206  
207 /* some static function declarations */
208 SRL_STATIC_INLINE void srl_clear_seen_hashes(pTHX_ srl_encoder_t *enc);
209 static void srl_dump_sv(pTHX_ srl_encoder_t *enc, SV *src);
210 SRL_STATIC_INLINE void srl_dump_svpv(pTHX_ srl_encoder_t *enc, SV *src, int allow_alias);
211 SRL_STATIC_INLINE void srl_dump_svpv_pv(pTHX_ srl_encoder_t *enc, SV *src, const char *str, STRLEN len, int allow_alias);
212 static SRL_NOINLINE void srl_dump_svpv_dedupe(pTHX_ srl_encoder_t *enc, const char *str, STRLEN len, int is_utf8, int allow_alias);
213 static SRL_NOINLINE void srl_dump_vstring(pTHX_ srl_encoder_t *enc, SV *src, int allow_alias);
214 SRL_STATIC_INLINE void srl_dump_pv(pTHX_ srl_encoder_t *enc, const char* src, STRLEN src_len, int is_utf8);
215 SRL_STATIC_INLINE void srl_fixup_weakrefs(pTHX_ srl_encoder_t *enc);
216 static SRL_NOINLINE void srl_dump_av(pTHX_ srl_encoder_t *enc, AV *src, U32 refcnt);
217 static SRL_NOINLINE void srl_dump_hv(pTHX_ srl_encoder_t *enc, HV *src, U32 refcnt);
218 SRL_STATIC_INLINE void srl_dump_hk(pTHX_ srl_encoder_t *enc, HE *src, const int share_keys);
219 SRL_STATIC_INLINE void srl_dump_hk_sv(pTHX_ srl_encoder_t *enc, SV *src);
220 SRL_STATIC_INLINE void srl_dump_nv(pTHX_ srl_encoder_t *enc, SV *src);
221 SRL_STATIC_INLINE void srl_dump_ivuv(pTHX_ srl_encoder_t *enc, SV *src);
222 static SRL_NOINLINE int srl_dump_classname(pTHX_ srl_encoder_t *enc, SV *referent, SV *replacement);
223 static SRL_NOINLINE SV *srl_get_frozen_object(pTHX_ srl_encoder_t *enc, SV *src, SV *referent);
224 static SRL_NOINLINE void srl_handle_unsupported_svtype(pTHX_ srl_encoder_t *enc, SV *src, svtype svt, SV *refsv, SSize_t ref_rewrite_pos, UV weak_rewrite_ofs);
225 SRL_STATIC_INLINE int srl_class_allowed(pTHX_ srl_encoder_t *enc, HV *allow_hash,
226 SV *allow_cb, HV *stash, SV *src, SV *referent, int with_object);
227 SRL_STATIC_INLINE PTABLE_t *srl_init_string_hash(srl_encoder_t *enc);
228 SRL_STATIC_INLINE PTABLE_t *srl_init_ref_hash(srl_encoder_t *enc);
229 SRL_STATIC_INLINE PTABLE_t *srl_init_freezeobj_svhash(srl_encoder_t *enc);
230 SRL_STATIC_INLINE PTABLE_t *srl_init_weak_hash(srl_encoder_t *enc);
231  
232 /* Note: This returns an encoder struct pointer because it will
233 * clone the current encoder struct if it's dirty. That in
234 * turn means in order to access the output buffer, you need
235 * to inspect the returned encoder struct. If necessary, it
236 * will be cleaned up automatically by Perl, so don't bother
237 * freeing it. */
238 SRL_STATIC_INLINE srl_encoder_t *srl_dump_data_structure(pTHX_ srl_encoder_t *enc, SV *src, SV *user_header_src);
239  
240 /* Borrow ordinary input while traversing it. Only an actual call into Perl
241 * needs strong references and a check that cached iteration storage survived.
242 * These frames describe live C state; they do not copy container contents. */
243 typedef struct srl_encoder_frame {
244 struct srl_encoder_frame *parent;
245 SV *container;
246 HE **cursor;
247 HE_SV *sorted;
248 UV sorted_count;
249 /* Captured only when a callback is about to run. */
250 void *storage;
251 SSize_t limit;
252 UV count;
253 svtype kind;
254 int opaque;
255 MAGIC *tie;
256 SV *tie_object;
257 HE *borrowed_entry;
258 SV *borrowed_key;
259 SV *active_sorted_key;
260 int key_guard;
261 int sorted_keyed;
262 } srl_encoder_frame;
263  
264 SRL_STATIC_INLINE void
265 20 srl_frame_push(srl_encoder_t *enc, srl_encoder_frame *frame, SV *container)
266 {
267 20 frame->parent = enc->frames;
268 20 frame->container = container;
269 20 enc->frames = frame;
270 20 }
271  
272 /* Callback-heavy encodes add many entries to this table on every run.
273 * Recycle at most 4096 entries (about 96 KiB on 64-bit platforms) across
274 * runs of a reusable Encoder. The spare entries contain no live SV refs. */
275 #define SRL_CALLBACK_SPARE_LIMIT 4096
276  
277 SRL_STATIC_INLINE void
278 487 srl_callback_hold(pTHX_ srl_encoder_t *enc, SV *value)
279 {
280 PTABLE_t *table;
281 PTABLE_ENTRY_t *entry;
282 UV bucket;
283  
284 487 ▶ if (!value)
Branch: 1 branch
BranchTrueFalseTotal
if (!value) 186 301 487
285 186 return;
286 301 table = enc->callback_seen;
287 301 ▶ if (PTABLE_find(table, value))
Branch: 1 branch
BranchTrueFalseTotal
if (PTABLE_find(table, value)) 223 78 301
288 223 return;
289 78 ▶ if (enc->callback_spare) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc->callback_spare) { 0 78 78
290 0 entry = enc->callback_spare;
291 0 enc->callback_spare = entry->next;
292 0 enc->callback_spare_count--;
293 } else {
294 78 Newx(entry, 1, PTABLE_ENTRY_t);
295 }
296 78 bucket = PTABLE_HASH(value) & table->tbl_max;
297 78 entry->key = value;
298 78 entry->value = INT2PTR(void *, 1);
299 78 entry->next = table->tbl_ary[bucket];
300 78 table->tbl_ary[bucket] = entry;
301 78 table->tbl_items++;
302 78 ▶ if (entry->next && table->tbl_items > table->tbl_max)
Branch: 2 branches
BranchTrueFalseTotal
if (entry->next && table->tbl_items > table->tbl_max) 34 44 78
if (entry->next && table->tbl_items > table->tbl_max) 6 28 34
303 6 PTABLE_grow(table);
304 78 av_push(enc->callback_keepalive, SvREFCNT_inc(value));
305 }
306  
307 SRL_STATIC_INLINE void
308 7 srl_callback_clear_seen(srl_encoder_t *enc)
309 {
310 7 PTABLE_t *table = enc->callback_seen;
311 UV i;
312 7 ▶ if (!table || !table->tbl_items)
Branch: 2 branches
BranchTrueFalseTotal
if (!table || !table->tbl_items) 7 0 7
if (!table || !table->tbl_items) 0 7 7
313 0 return;
314 111 ▶ for (i = 0; i <= table->tbl_max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= table->tbl_max; i++) { 104 7 111
315 104 PTABLE_ENTRY_t *entry = table->tbl_ary[i];
316 182 ▶ while (entry) {
Branch: 1 branch
BranchTrueFalseTotal
while (entry) { 78 104 182
317 78 PTABLE_ENTRY_t *next = entry->next;
318 78 ▶ if (enc->callback_spare_count < SRL_CALLBACK_SPARE_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc->callback_spare_count < SRL_CALLBACK_SPARE_LIMIT) { 78 0 78
319 78 entry->next = enc->callback_spare;
320 78 enc->callback_spare = entry;
321 78 enc->callback_spare_count++;
322 } else {
323 0 Safefree(entry);
324 }
325 78 entry = next;
326 }
327 104 table->tbl_ary[i] = NULL;
328 }
329 7 table->tbl_items = 0;
330 }
331  
332 SRL_STATIC_INLINE void
333 7 srl_callback_free_spare(srl_encoder_t *enc)
334 {
335 85 ▶ while (enc->callback_spare) {
Branch: 1 branch
BranchTrueFalseTotal
while (enc->callback_spare) { 78 7 85
336 78 PTABLE_ENTRY_t *entry = enc->callback_spare;
337 78 enc->callback_spare = entry->next;
338 78 Safefree(entry);
339 }
340 7 enc->callback_spare_count = 0;
341 7 }
342  
343 /* Both SVs and HEKs are aligned. Tag SV identities so one offset table can
344 * distinguish class/object identities from shared hash-key identities. */
345 #define SRL_CLASS_ID(value) INT2PTR(void *, PTR2UV(value) | (UV)1)
346  
347 SRL_STATIC_INLINE void
348 0 srl_callback_hold_key(pTHX_ srl_encoder_t *enc, HEK *hek)
349 {
350 /* newSVhek upgrades WASUTF8 keys into private copies. Keep the actual
351 * shared allocation instead; this scalar is only used for its lifetime. */
352 0 SV *keeper = newSV_type(SVt_PV);
353 0 SvPV_set(keeper, (char *)HEK_KEY(share_hek_hek(hek)));
354 0 SvCUR_set(keeper, HEK_LEN(hek));
355 0 SvLEN_set(keeper, 0);
356 #ifdef SvIsCOW_on
357 0 SvIsCOW_on(keeper);
358 #else
359 SvREADONLY_on(keeper);
360 SvFAKE_on(keeper);
361 #endif
362 0 SvPOK_on(keeper);
363 0 ▶ if (HEK_UTF8(hek))
Branch: 1 branch
BranchTrueFalseTotal
if (HEK_UTF8(hek)) 0 0 0
364 0 SvUTF8_on(keeper);
365 0 av_push(enc->callback_keepalive, keeper);
366 0 }
367  
368 static void
369 115 srl_callback_begin(pTHX_ srl_encoder_t *enc, SV *value, SV *reference)
370 {
371 srl_encoder_frame *frame;
372 115 ▶ if (!SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS)) { 7 108 115
373 7 ▶ if (!enc->callback_keepalive)
Branch: 1 branch
BranchTrueFalseTotal
if (!enc->callback_keepalive) 7 0 7
374 7 enc->callback_keepalive = newAV();
375 7 ▶ if (!enc->callback_seen)
Branch: 1 branch
BranchTrueFalseTotal
if (!enc->callback_seen) 7 0 7
376 7 enc->callback_seen = PTABLE_new_size(3);
377 7 srl_callback_hold(aTHX_ enc, enc->root_src);
378 7 srl_callback_hold(aTHX_ enc, enc->header_src);
379 /* Keep previously tracked identities alive before user code can
380 * release them and recycle their addresses. */
381 7 ▶ if (enc->ref_seenhash) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc->ref_seenhash) { 7 0 7
382 7 PTABLE_ITER_t *it = PTABLE_iter_new(enc->ref_seenhash);
383 PTABLE_ENTRY_t *entry;
384 14 ▶ while ((entry = PTABLE_iter_next(it)))
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = PTABLE_iter_next(it))) 7 7 14
385 7 srl_callback_hold(aTHX_ enc, (SV *)entry->key);
386 7 PTABLE_iter_free(it);
387 }
388 7 ▶ if (enc->str_seenhash) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc->str_seenhash) { 0 7 7
389 0 PTABLE_ITER_t *it = PTABLE_iter_new(enc->str_seenhash);
390 PTABLE_ENTRY_t *entry;
391 0 ▶ while ((entry = PTABLE_iter_next(it))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = PTABLE_iter_next(it))) { 0 0 0
392 0 ▶ if (PTR2UV(entry->key) & (UV)1)
Branch: 1 branch
BranchTrueFalseTotal
if (PTR2UV(entry->key) & (UV)1) 0 0 0
393 0 srl_callback_hold(aTHX_ enc, INT2PTR(SV *, PTR2UV(entry->key) & ~(UV)1));
394 else
395 0 srl_callback_hold_key(aTHX_ enc, (HEK *)entry->key);
396 }
397 0 PTABLE_iter_free(it);
398 }
399 7 SRL_ENC_SET_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS);
400 }
401 115 srl_callback_hold(aTHX_ enc, value);
402 115 srl_callback_hold(aTHX_ enc, reference);
403 345 ▶ for (frame = enc->frames; frame; frame = frame->parent) {
Branch: 1 branch
BranchTrueFalseTotal
for (frame = enc->frames; frame; frame = frame->parent) { 230 115 345
404 230 SV *container = frame->container;
405 230 srl_callback_hold(aTHX_ enc, container);
406 230 frame->kind = SvTYPE(container);
407 230 ▶ frame->tie = SvRMAGICAL(container) ? mg_find(container, PERL_MAGIC_tied) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
frame->tie = SvRMAGICAL(container) ? mg_find(container, PERL_MAGIC_tied) : NULL; 115 115 230
408 230 frame->opaque = frame->tie != NULL;
409 230 ▶ if (frame->opaque) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->opaque) { 0 230 230
410 0 frame->tie_object = frame->tie->mg_obj;
411 0 srl_callback_hold(aTHX_ enc, frame->tie_object);
412 0 continue;
413 }
414 230 ▶ if (frame->kind == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->kind == SVt_PVAV) { 230 0 230
415 230 frame->storage = AvARRAY((AV *)container);
416 230 frame->limit = AvFILLp((AV *)container);
417 } else {
418 0 HV *hash = (HV *)container;
419 UV i;
420 0 frame->storage = HvARRAY(hash);
421 0 frame->limit = HvMAX(hash);
422 0 ▶ frame->count = HvUSEDKEYS(hash);
Branch: 1 branch
BranchTrueFalseTotal
frame->count = HvUSEDKEYS(hash); 0 0 0
423 0 frame->key_guard = 0;
424 0 ▶ if (!SvMAGICAL(hash) && frame->cursor && *frame->cursor
Branch: 3 branches
BranchTrueFalseTotal
if (!SvMAGICAL(hash) && frame->cursor && *frame->cursor 0 0 0
if (!SvMAGICAL(hash) && frame->cursor && *frame->cursor 0 0 0
if (!SvMAGICAL(hash) && frame->cursor && *frame->cursor 0 0 0
425 0 ▶ && HeKLEN(*frame->cursor) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& HeKLEN(*frame->cursor) >= 0) { 0 0 0
426 /* Keep just this borrowed key so an O(1) lookup can prove
427 * that the current HE is still live after the callback. */
428 0 frame->borrowed_entry = *frame->cursor;
429 0 frame->borrowed_key = frame->active_sorted_key
430 ? frame->active_sorted_key
431 0 ▶ : sv_2mortal(newSVhek(HeKEY_hek(frame->borrowed_entry)));
Branch: 1 branch
BranchTrueFalseTotal
: sv_2mortal(newSVhek(HeKEY_hek(frame->borrowed_entry))); 0 0 0
432 0 frame->key_guard = 1;
433 }
434 0 ▶ if (frame->sorted && !frame->sorted_keyed) {
Branch: 2 branches
BranchTrueFalseTotal
if (frame->sorted && !frame->sorted_keyed) { 0 0 0
if (frame->sorted && !frame->sorted_keyed) { 0 0 0
435 /* The sort already needs this key list. Before the first
436 * callback, give remaining borrowed HEs stable lookup keys.
437 * Validate each HE immediately before use instead of scanning
438 * the entire hash after every callback. Values are still
439 * fetched at use. */
440 0 ▶ for (i = 0; i < frame->sorted_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < frame->sorted_count; i++) { 0 0 0
441 0 HE_SV *item = &frame->sorted[i];
442 0 ▶ if (item->key.sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (item->key.sv) { 0 0 0
443 0 srl_callback_hold(aTHX_ enc, item->key.sv);
444 } else {
445 0 item->key.sv = newSVhek(HeKEY_hek(item->val.he));
446 0 av_push(enc->callback_keepalive, item->key.sv);
447 }
448 }
449 0 frame->sorted_keyed = 1;
450 }
451 }
452 }
453 115 }
454  
455 static void
456 115 srl_callback_end(pTHX_ srl_encoder_t *enc)
457 {
458 srl_encoder_frame *frame;
459 345 ▶ for (frame = enc->frames; frame; frame = frame->parent) {
Branch: 1 branch
BranchTrueFalseTotal
for (frame = enc->frames; frame; frame = frame->parent) { 230 115 345
460 230 SV *container = frame->container;
461 230 ▶ if (!container)
Branch: 1 branch
BranchTrueFalseTotal
if (!container) 0 230 230
462 0 continue;
463 230 ▶ if (frame->opaque) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->opaque) { 0 230 230
464 0 ▶ MAGIC *tie = SvRMAGICAL(container) ? mg_find(container, PERL_MAGIC_tied) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
MAGIC *tie = SvRMAGICAL(container) ? mg_find(container, PERL_MAGIC_tied) : NULL; 0 0 0
465 0 ▶ if (tie != frame->tie || tie->mg_obj != frame->tie_object)
Branch: 2 branches
BranchTrueFalseTotal
if (tie != frame->tie || tie->mg_obj != frame->tie_object) 0 0 0
if (tie != frame->tie || tie->mg_obj != frame->tie_object) 0 0 0
466 0 croak("Sereal: input tied container was modified during encoding");
467 0 continue;
468 }
469 230 ▶ if (SvTYPE(container) != frame->kind)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(container) != frame->kind) 0 230 230
470 0 croak("Sereal: input container was modified during encoding");
471 230 ▶ if (frame->kind == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->kind == SVt_PVAV) { 230 0 230
472 230 ▶ if (AvARRAY((AV *)container) != frame->storage || AvFILLp((AV *)container) != frame->limit)
Branch: 2 branches
BranchTrueFalseTotal
if (AvARRAY((AV *)container) != frame->storage || AvFILLp((AV *)container) != frame->limit) 230 0 230
if (AvARRAY((AV *)container) != frame->storage || AvFILLp((AV *)container) != frame->limit) 0 230 230
473 0 croak("Sereal: input array was modified during encoding");
474 } else {
475 0 HV *hash = (HV *)container;
476 0 HE **buckets = HvARRAY(hash);
477 UV i;
478 0 ▶ HE *current = frame->cursor ? *frame->cursor : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *current = frame->cursor ? *frame->cursor : NULL; 0 0 0
479 0 int found = current == NULL;
480 0 ▶ if (buckets != frame->storage || HvMAX(hash) != (UV)frame->limit
Branch: 2 branches
BranchTrueFalseTotal
if (buckets != frame->storage || HvMAX(hash) != (UV)frame->limit 0 0 0
if (buckets != frame->storage || HvMAX(hash) != (UV)frame->limit 0 0 0
481 0 ▶ || HvUSEDKEYS(hash) != frame->count)
Branch: 2 branches
BranchTrueFalseTotal
|| HvUSEDKEYS(hash) != frame->count) 0 0 0
|| HvUSEDKEYS(hash) != frame->count) 0 0 0
482 0 croak("Sereal: input hash was modified during encoding");
483 0 ▶ if (frame->key_guard) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->key_guard) { 0 0 0
484 0 ▶ if (SvMAGICAL(hash)
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL(hash) 0 0 0
485 0 ▶ || hv_fetch_ent(hash, frame->borrowed_key, 0, 0) != frame->borrowed_entry)
Branch: 1 branch
BranchTrueFalseTotal
|| hv_fetch_ent(hash, frame->borrowed_key, 0, 0) != frame->borrowed_entry) 0 0 0
486 0 croak("Sereal: input hash was modified during encoding");
487 0 continue;
488 }
489 0 ▶ if (buckets && current) {
Branch: 2 branches
BranchTrueFalseTotal
if (buckets && current) { 0 0 0
if (buckets && current) { 0 0 0
490 #ifdef XPERL_VERSION
491 const UV limit = HvENTRY_LIMIT(hash);
492 for (i = 0; i < limit; i++) {
493 HE *entry;
494 for (entry = HvENTRY_FIRST(hash, i); entry;
495 entry = HvENTRY_NEXT(hash, entry)) {
496 if (entry == current)
497 found = 1;
498 }
499 }
500 #else
501 0 ▶ for (i = 0; i <= HvMAX(hash); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= HvMAX(hash); i++) { 0 0 0
502 HE *entry;
503 0 ▶ for (entry = buckets[i]; entry; entry = HeNEXT(entry)) {
Branch: 1 branch
BranchTrueFalseTotal
for (entry = buckets[i]; entry; entry = HeNEXT(entry)) { 0 0 0
504 0 ▶ if (entry == current)
Branch: 1 branch
BranchTrueFalseTotal
if (entry == current) 0 0 0
505 0 found = 1;
506 }
507 }
508 #endif
509 }
510 0 ▶ if (!found)
Branch: 1 branch
BranchTrueFalseTotal
if (!found) 0 0 0
511 0 croak("Sereal: input hash was modified during encoding");
512 }
513 }
514 115 }
515  
516 SRL_STATIC_INLINE void
517 64 srl_getmagic(pTHX_ srl_encoder_t *enc, SV *value, SV *reference)
518 {
519 64 ▶ if (SvGMAGICAL(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(value)) { 51 13 64
520 51 srl_callback_begin(aTHX_ enc, value, reference);
521 51 SvGETMAGIC(value);
522 51 srl_callback_end(aTHX_ enc);
523 }
524 64 }
525  
526 #define SRL_GET_STR_PTR_SEENHASH(enc) ( (enc)->str_seenhash == NULL \
527 ? srl_init_string_hash(enc) \
528 : (enc)->str_seenhash )
529  
530 #define SRL_GET_REF_SEENHASH(enc) ( (enc)->ref_seenhash == NULL \
531 ? srl_init_ref_hash(enc) \
532 : (enc)->ref_seenhash )
533  
534 #define SRL_GET_WEAK_SEENHASH(enc) ( (enc)->weak_seenhash == NULL \
535 ? srl_init_weak_hash(enc) \
536 : (enc)->weak_seenhash )
537  
538 #define SRL_GET_WEAK_SEENHASH_OR_NULL(enc) ((enc)->weak_seenhash)
539  
540 #define SRL_GET_FREEZEOBJ_SVHASH(enc) ( (enc)->freezeobj_svhash == NULL \
541 ? srl_init_freezeobj_svhash(enc) \
542 : (enc)->freezeobj_svhash )
543  
544 #define SRL_ENC_UPDATE_BODY_POS(enc) SRL_UPDATE_BODY_POS(&(enc)->buf, (enc)->protocol_version)
545  
546 #ifndef MAX_CHARSET_NAME_LENGTH
547 # define MAX_CHARSET_NAME_LENGTH 2
548 #endif
549  
550 #if PERL_VERSION == 10
551 /*
552 Apparently regexes in 5.10 are "modern" but with 5.8 internals
553 */
554 #ifndef RXf_PMf_STD_PMMOD_SHIFT
555 # define RXf_PMf_STD_PMMOD_SHIFT 12
556 #endif
557 #ifndef RE_EXTFLAGS
558 # define RX_EXTFLAGS(re) ((re)->extflags)
559 #endif
560 #ifndef RX_PRECOMP
561 # define RX_PRECOMP(re) ((re)->precomp)
562 #endif
563 #ifndef RX_PRELEN
564 # define RX_PRELEN(re) ((re)->prelen)
565 #endif
566  
567 /* Maybe this is only on OS X, where SvUTF8(sv) exists but looks at flags that don't exist */
568 #ifndef RX_UTF8
569 # define RX_UTF8(re) (RX_EXTFLAGS(re) & RXf_UTF8)
570 #endif
571  
572 #elif defined(SvRX)
573 # define MODERN_REGEXP
574 # if ( PERL_VERSION > 27 || (PERL_VERSION == 27 && PERL_SUBVERSION >= 3) )
575 /* Commit df6b4bd56551f2d39f7c0019c23f27181d8c39c4
576 * changed the behavior mentioned below, so that the POK flag is on again. Sigh.
577 * So this branch is a deliberate NO-OP, it just makes the conditions easier to read.*/
578 # elif ( PERL_VERSION > 17 || (PERL_VERSION == 17 && PERL_SUBVERSION >= 6) )
579 /* With commit 8d919b0a35f2b57a6bed2f8355b25b19ac5ad0c5 (perl.git) and
580 * release 5.17.6, regular expression are no longer SvPOK (IOW are no longer
581 * considered to be containing a string).
582 * This breaks some of the REGEXP detection logic in srl_dump_sv, so
583 * we need yet another CPP define. */
584 # define REGEXP_NO_LONGER_POK
585 # endif
586 #else
587 # define INT_PAT_MODS "msix"
588 # define RXf_PMf_STD_PMMOD_SHIFT 12
589 # define RX_PRECOMP(re) ((re)->precomp)
590 # define RX_PRELEN(re) ((re)->prelen)
591 # define RX_UTF8(re) ((re)->reganch & ROPT_UTF8)
592 # define RX_EXTFLAGS(re) ((re)->reganch)
593 # define RXf_PMf_COMPILETIME PMf_COMPILETIME
594 #endif
595  
596 #if defined(MODERN_REGEXP) && !defined(REGEXP_NO_LONGER_POK)
597 #define DO_POK_REGEXP(enc, src, svt) \
598 /* Only need to enter here if we have rather modern regexps,*/ \
599 /* but they're still POK (pre 5.17.6). */ \
600 if (expect_false( svt == SVt_REGEXP ) ) { \
601 srl_dump_regexp(aTHX_ enc, src); \
602 } \
603 else
604 #else
605 #define DO_POK_REGEXP(enc, src, svt) /*no-op*/
606 #endif
607  
608  
609  
610 #ifdef SvIsBOOL
611 #define _SRL_CHECK_BOOL(enc, src, svt) \
612 if (enc->protocol_version >=5 && SvIsBOOL(src)) { \
613 if (PV == PL_No) { \
614 srl_buf_cat_char(&enc->buf, SRL_HDR_NO); \
615 } else { \
616 assert(PV == PL_Yes); \
617 srl_buf_cat_char(&enc->buf, SRL_HDR_YES); \
618 } \
619 } \
620 else
621 #else
622 #define _SRL_CHECK_BOOL(enc, src, svt)
623 #endif
624  
625 #define _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, allow_alias) \
626 if (SvPOK(src)) { \
627 if (expect_false( SvIOK(src) || SvNOK(src) )) { \
628 STRLEN L; \
629 char *PV= SvPV_nomg(src, L); \
630 if ( SvIOK(src) ) { \
631 _SRL_CHECK_BOOL(enc, src, svt) \
632 if ( SvIV_nomg(src) == 0 ) { \
633 if ( L == 1 && PV[0] == '0' ) { \
634 /* its a true 0 */ \
635 srl_buf_cat_char(&enc->buf, SRL_HDR_POS + 0); \
636 } \
637 else { \
638 /* must be a string */ \
639 srl_dump_svpv_pv(aTHX_ enc, src, PV, L, allow_alias); \
640 } \
641 } \
642 else \
643 if ( \
644 !L || \
645 !isDIGIT(PV[L-1]) || \
646 ( \
647 SvIV_nomg(src) > 0 \
648 ? ( PV[0] == '0' || !isDIGIT(PV[0]) ) \
649 : ( L < 2 || PV[0] != '-' || PV[1] == '0' || !isDIGIT(PV[1]) ) \
650 ) \
651 ) { \
652 srl_dump_svpv_pv(aTHX_ enc, src, PV, L, allow_alias); \
653 } \
654 else { \
655 if ( SvNOK(src) ) { \
656 /* fallback to checking if the canonical stringified*/ \
657 /* int is the same as the buffer */ \
658 sv_setiv(enc->scratch_sv,SvIV_nomg(src)); \
659 if ( sv_cmp_flags(enc->scratch_sv,src,0) ) { \
660 srl_dump_svpv_pv(aTHX_ enc, src, PV, L, allow_alias); \
661 } else { \
662 srl_dump_ivuv(aTHX_ enc, src); \
663 } \
664 } else { \
665 srl_dump_ivuv(aTHX_ enc, src); \
666 } \
667 } \
668 } \
669 else { \
670 if ( L <= 8 || \
671 !isDIGIT(PV[0]) || \
672 !isDIGIT(PV[L-1]) || \
673 PV[L-1] == '0' || \
674 ( \
675 SvNV_nomg(src) > 0.0 \
676 ? ( PV[0] == '.' || (PV[0] == '0' && PV[1] != '.') ) \
677 : ( PV[0] != '-' || PV[1] == '.' || (PV[1] == '0' && PV[2] != '.')) \
678 ) \
679 ) { \
680 srl_dump_svpv_pv(aTHX_ enc, src, PV, L, allow_alias); \
681 } \
682 else { \
683 srl_dump_nv(aTHX_ enc, src); \
684 } \
685 } \
686 } \
687 else { \
688 DO_POK_REGEXP(enc,src,svt) \
689 srl_dump_svpv_pv(aTHX_ enc, src, SvPVX_const(src), SvCUR(src), allow_alias); \
690 } \
691 } \
692 else \
693 if ( SvIOK(src) ) { \
694 srl_dump_ivuv(aTHX_ enc, src); \
695 } \
696 else \
697 /* if its a float then its a float */ \
698 if (SvNOK(src)) { \
699 srl_dump_nv(aTHX_ enc, src); \
700 } \
701 else \
702 /* The POKp, IOKp, NOKp checks below deal with PVLV */ \
703 /* if its POK or POKp, then we treat it as a string */ \
704 if (SvPOKp(src)) { \
705 DO_POK_REGEXP(enc,src,svt) \
706 srl_dump_svpv(aTHX_ enc, src, allow_alias); \
707 } \
708 else \
709 /* if its IOKp then we treat it as an int */ \
710 if (SvIOKp(src)) { \
711 srl_dump_ivuv(aTHX_ enc, src); \
712 } \
713 else \
714 /* if its NOKp then we treat it as an nv */ \
715 if (SvNOKp(src)) { \
716 srl_dump_nv(aTHX_ enc, src); \
717 } \
718  
719 #define CALL_SRL_DUMP_SV(enc, src) STMT_START { \
720 if (!(src)) { \
721 srl_buf_cat_char(&(enc)->buf, \
722 (enc)->protocol_version >= 3 ? SRL_HDR_CANONICAL_UNDEF : SRL_HDR_UNDEF);\
723 } \
724 else \
725 { \
726 svtype svt; \
727 svt= SvTYPE((src)); \
728 if (svt < SVt_PVMG && \
729 SvREFCNT((src)) == 1 && \
730 !SvROK((src)) \
731 ) { \
732 _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, 1) \
733 else if (!SvOK((src))) { \
734 /* SvREFCNT == 1 rules out immortal &PL_sv_undef (canonical undef). */\
735 srl_buf_cat_char(&(enc)->buf, SRL_HDR_UNDEF); \
736 } \
737 else { \
738 srl_dump_sv(aTHX_ (enc), (src)); \
739 } \
740 } else { \
741 srl_dump_sv(aTHX_ (enc), (src)); \
742 } \
743 } \
744 } STMT_END
745  
746 #define CALL_SRL_DUMP_SVP(enc, srcp) STMT_START { \
747 if (!(srcp)) { \
748 srl_buf_cat_char(&(enc)->buf, \
749 (enc)->protocol_version >= 3 ? SRL_HDR_CANONICAL_UNDEF : SRL_HDR_UNDEF);\
750 } else { \
751 SV *src= *srcp; \
752 CALL_SRL_DUMP_SV(enc,src); \
753 } \
754 } STMT_END
755  
756 /* This is fired when we exit the Perl pseudo-block.
757 * It frees our encoder and all. Put encoder-level cleanup
758 * logic here so that we can simply use croak/longjmp for
759 * exception handling. Makes life vastly easier!
760 */
761 void
762 7 srl_destructor_hook(pTHX_ void *p)
763 {
764 7 srl_encoder_t *enc = (srl_encoder_t *)p;
765 /* Do not auto-destroy encoder if set to be re-used */
766 7 ▶ if (!SRL_ENC_HAVE_OPTION(enc, SRL_F_REUSE_ENCODER)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SRL_ENC_HAVE_OPTION(enc, SRL_F_REUSE_ENCODER)) { 0 7 7
767 /* Exception cleanup. Under normal operation, we should have
768 * assigned NULL to buf_start after we're done. */
769 0 srl_destroy_encoder(aTHX_ enc);
770 }
771 else {
772 7 srl_clear_encoder(aTHX_ enc);
773 }
774 7 }
775  
776 SRL_STATIC_INLINE void
777 7 srl_clear_seen_hashes(pTHX_ srl_encoder_t *enc)
778 {
779 7 ▶ if (enc->ref_seenhash != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->ref_seenhash != NULL) 7 0 7
780 7 PTABLE_clear(enc->ref_seenhash);
781 7 ▶ if (enc->freezeobj_svhash != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->freezeobj_svhash != NULL) 0 7 7
782 0 PTABLE_clear_dec(aTHX_ enc->freezeobj_svhash);
783 7 ▶ if (enc->str_seenhash != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->str_seenhash != NULL) 0 7 7
784 0 PTABLE_clear(enc->str_seenhash);
785 7 ▶ if (enc->weak_seenhash != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->weak_seenhash != NULL) 0 7 7
786 0 PTABLE_clear(enc->weak_seenhash);
787 7 srl_str_dedupe_clear(aTHX_ &enc->str_dedupe_tbl);
788 7 }
789  
790 SRL_STATIC_INLINE void
791 14 srl_reset_or_trim_buffer(pTHX_ srl_buffer_t *buf)
792 {
793 14 ▶ if (buf->start == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (buf->start == NULL) 7 7 14
794 7 return;
795 7 ▶ if ((size_t)BUF_SIZE(buf) > (size_t)SRL_ENC_BUF_REUSE_MAX_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)BUF_SIZE(buf) > (size_t)SRL_ENC_BUF_REUSE_MAX_SIZE) { 0 7 7
796 0 srl_buf_free_buffer(aTHX_ buf);
797 0 buf->start = buf->pos = buf->end = buf->body_pos = NULL;
798 } else {
799 7 buf->pos = buf->start;
800 7 SRL_SET_BODY_POS(buf, buf->start);
801 }
802 }
803  
804 void
805 7 srl_clear_encoder(pTHX_ srl_encoder_t *enc)
806 {
807 /* TODO I think this could just be made an assert. */
808 7 ▶ if (!SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_DIRTY)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_DIRTY)) { 0 7 7
809 0 warn("Sereal Encoder being cleared but in virgin state. That is unexpected.");
810 }
811  
812 7 enc->recursion_depth = 0;
813 7 enc->frames = NULL;
814 7 enc->root_src = enc->header_src = NULL;
815 7 ▶ if (enc->callback_seen)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->callback_seen) 7 0 7
816 7 srl_callback_clear_seen(enc);
817 7 srl_clear_seen_hashes(aTHX_ enc);
818 7 ▶ if (enc->callback_keepalive)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->callback_keepalive) 7 0 7
819 7 av_clear(enc->callback_keepalive);
820  
821 7 srl_reset_or_trim_buffer(aTHX_ &enc->buf);
822 7 srl_reset_or_trim_buffer(aTHX_ &enc->tmp_buf);
823  
824 7 SRL_ENC_RESET_OPER_FLAG(enc, SRL_OF_ENCODER_DIRTY | SRL_OF_ENCODER_HAS_CALLBACKS);
825 7 }
826  
827 void
828 7 srl_destroy_encoder(pTHX_ srl_encoder_t *enc)
829 {
830 7 enc->frames = NULL;
831 7 SvREFCNT_dec(enc->callback_keepalive);
832 7 ▶ if (enc->callback_seen)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->callback_seen) 7 0 7
833 7 PTABLE_free(enc->callback_seen);
834 7 srl_callback_free_spare(enc);
835 7 ▶ if (enc->buf.start != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->buf.start != NULL) 7 0 7
836 7 srl_buf_free_buffer(aTHX_ &enc->buf);
837  
838 /* Free tmp buffer only if it was allocated at all. */
839 7 ▶ if (enc->tmp_buf.start != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->tmp_buf.start != NULL) 0 7 7
840 0 srl_buf_free_buffer(aTHX_ &enc->tmp_buf);
841  
842 7 srl_destroy_snappy_workmem(aTHX_ enc->snappy_workmem);
843 7 ▶ if (enc->zstd_cctx != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc->zstd_cctx != NULL) { 0 7 7
844 0 ZSTD_freeCCtx((ZSTD_CCtx *)enc->zstd_cctx);
845 0 enc->zstd_cctx = NULL;
846 }
847  
848 7 ▶ if (enc->ref_seenhash != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->ref_seenhash != NULL) 7 0 7
849 7 PTABLE_free(enc->ref_seenhash);
850 7 ▶ if (enc->freezeobj_svhash != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->freezeobj_svhash != NULL) 0 7 7
851 0 PTABLE_free(enc->freezeobj_svhash);
852 7 ▶ if (enc->str_seenhash != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->str_seenhash != NULL) 0 7 7
853 0 PTABLE_free(enc->str_seenhash);
854 7 ▶ if (enc->weak_seenhash != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->weak_seenhash != NULL) 0 7 7
855 0 PTABLE_free(enc->weak_seenhash);
856 7 srl_str_dedupe_free(aTHX_ &enc->str_dedupe_tbl);
857 7 ▶ if (enc->freeze_allow_hash != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->freeze_allow_hash != NULL) 0 7 7
858 0 SvREFCNT_dec((SV*)enc->freeze_allow_hash);
859 7 ▶ if (enc->freeze_allow_cb != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->freeze_allow_cb != NULL) 0 7 7
860 0 SvREFCNT_dec(enc->freeze_allow_cb);
861 7 ▶ if (enc->allow_hash != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->allow_hash != NULL) 0 7 7
862 0 SvREFCNT_dec((SV*)enc->allow_hash);
863 7 ▶ if (enc->allow_cb != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->allow_cb != NULL) 0 7 7
864 0 SvREFCNT_dec(enc->allow_cb);
865 7 ▶ if (enc->track_classes != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->track_classes != NULL) 0 7 7
866 0 SvREFCNT_dec((SV*)enc->track_classes);
867  
868 7 SvREFCNT_dec(enc->sereal_string_sv);
869 7 SvREFCNT_dec(enc->scratch_sv);
870  
871 7 Safefree(enc);
872 7 }
873  
874 /* allocate an empty encoder struct - flags still to be set up */
875 SRL_STATIC_INLINE srl_encoder_t *
876 7 srl_empty_encoder_struct(pTHX)
877 {
878 srl_encoder_t *enc;
879 7 Newxz(enc, 1, srl_encoder_t);
880 7 ▶ if (enc == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (enc == NULL) 0 7 7
881 0 croak("Out of memory");
882  
883 /* Init buffer struct */
884 7 ▶ if (expect_false( srl_buf_init_buffer(aTHX_ &(enc->buf), INITIALIZATION_SIZE) != 0 )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( srl_buf_init_buffer(aTHX_ &(enc->buf), INITIALIZATION_SIZE) != 0 )) { 0 7 7
885 0 Safefree(enc);
886 0 croak("Out of memory");
887 }
888  
889 7 enc->protocol_version = SRL_PROTOCOL_VERSION;
890 7 enc->max_recursion_depth = DEFAULT_MAX_RECUR_DEPTH;
891 7 enc->zstd_cctx = NULL;
892  
893 7 return enc;
894 }
895  
896 /* Construction has no encoded state to clear. Keep a completed encoder
897 * through scope exit, but release all partial state if an option croaks. */
898 static void
899 7 srl_encoder_constructor_cleanup(pTHX_ void *p)
900 {
901 7 srl_encoder_t *enc = (srl_encoder_t *)p;
902 7 ▶ if (!SRL_ENC_HAVE_OPTION(enc, SRL_F_REUSE_ENCODER))
Branch: 1 branch
BranchTrueFalseTotal
if (!SRL_ENC_HAVE_OPTION(enc, SRL_F_REUSE_ENCODER)) 0 7 7
903 0 srl_destroy_encoder(aTHX_ enc);
904 7 }
905  
906 static U32
907 6 srl_encoder_option_id(pTHX_ sv_with_hash *options, U32 option_count,
908 const U8 *known_option_heads, const U8 *known_option_next,
909 U32 key_hash, const char *key_bytes, STRLEN key_len)
910 {
911 6 U8 indexed = known_option_heads[key_hash & (SRL_OPTION_INDEX_BUCKETS - 1)];
912 10 ▶ while (indexed) {
Branch: 1 branch
BranchTrueFalseTotal
while (indexed) { 10 0 10
913 10 U32 option_id = (U32)indexed - 1;
914 STRLEN known_len;
915 const char *known_bytes;
916 10 ▶ if (options[option_id].hash == key_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (options[option_id].hash == key_hash) { 6 4 10
917 6 known_bytes = SvPV(options[option_id].sv, known_len);
918 6 ▶ if (key_len == known_len && memEQ(key_bytes, known_bytes, key_len))
Branch: 2 branches
BranchTrueFalseTotal
if (key_len == known_len && memEQ(key_bytes, known_bytes, key_len)) 6 0 6
if (key_len == known_len && memEQ(key_bytes, known_bytes, key_len)) 6 0 6
919 6 return option_id;
920 }
921 4 indexed = known_option_next[option_id];
922 }
923 0 return option_count;
924 }
925  
926 static void
927 0 srl_set_class_allow_list(pTHX_ SV *val, const char *name, HV **allow_hash, SV **allow_cb)
928 {
929 0 ▶ SV *rv = SvROK(val) ? SvRV(val) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *rv = SvROK(val) ? SvRV(val) : NULL; 0 0 0
930 0 ▶ if (rv && SvTYPE(rv) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv && SvTYPE(rv) == SVt_PVAV) { 0 0 0
if (rv && SvTYPE(rv) == SVt_PVAV) { 0 0 0
931 0 AV *av = (AV*)rv;
932 0 SSize_t i, len = av_len(av) + 1;
933 0 *allow_hash = newHV();
934 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
935 0 SV **elem = av_fetch(av, i, 0);
936 0 ▶ if (elem && SvOK(*elem)) {
Branch: 2 branches
BranchTrueFalseTotal
if (elem && SvOK(*elem)) { 0 0 0
if (elem && SvOK(*elem)) { 0 0 0
937 STRLEN klen;
938 0 char *kstr = SvPV(*elem, klen);
939 0 ▶ (void)hv_store(*allow_hash, kstr, SvUTF8(*elem) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(*allow_hash, kstr, SvUTF8(*elem) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0); 0 0 0
940 }
941 }
942 }
943 0 ▶ else if (rv && SvTYPE(rv) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
else if (rv && SvTYPE(rv) == SVt_PVHV) { 0 0 0
else if (rv && SvTYPE(rv) == SVt_PVHV) { 0 0 0
944 0 HV *src = (HV*)rv;
945 HE *ent;
946 0 *allow_hash = newHV();
947 0 hv_iterinit(src);
948 0 ▶ while ((ent = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ent = hv_iternext(src))) { 0 0 0
949 0 ▶ if (SvTRUE(HeVAL(ent))) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(HeVAL(ent))) { 0 0 0
950 STRLEN klen;
951 0 ▶ char *kstr = HePV(ent, klen);
Branch: 1 branch
BranchTrueFalseTotal
char *kstr = HePV(ent, klen); 0 0 0
952 0 ▶ (void)hv_store(*allow_hash, kstr, HeUTF8(ent) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0);
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store(*allow_hash, kstr, HeUTF8(ent) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0); 0 0 0
(void)hv_store(*allow_hash, kstr, HeUTF8(ent) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0); 0 0 0
953 }
954 }
955 }
956 0 ▶ else if (rv && SvTYPE(rv) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
else if (rv && SvTYPE(rv) == SVt_PVCV) { 0 0 0
else if (rv && SvTYPE(rv) == SVt_PVCV) { 0 0 0
957 0 *allow_cb = newRV_inc(rv);
958 }
959 else {
960 0 croak("The '%s' option must be an array ref, hash ref or code ref", name);
961 }
962 0 }
963  
964 /* Builds the C-level configuration and state struct. */
965 srl_encoder_t *
966 7 srl_build_encoder_struct(pTHX_ HV *opt, sv_with_hash *options,
967 const U8 *known_option_heads, const U8 *known_option_next)
968 {
969 srl_encoder_t *enc;
970 HE *entry;
971 SV *val;
972 7 SV *freeze_allow_classes_value = NULL;
973 7 SV *allow_classes_value = NULL;
974 7 SV *track_classes_value = NULL;
975 7 SV *checksum_key = NULL;
976 7 SV *checksum_policy_value = NULL;
977 7 SV *unknown_option_policy_value = NULL;
978 7 AV *unknown_option_keys = NULL;
979 7 SV *canonical_value = NULL;
980 7 SV *sort_keys_value = NULL;
981 7 SV *canonical_refs_value = NULL;
982 7 SV *compress_level_value = NULL;
983 7 SV *compress_threshold_value = NULL;
984 7 SV *snappy_threshold_value = NULL;
985 7 int checksum_mode = SRL_CHECKSUM_MODE_UNSET;
986 7 int protocol_version_set = 0;
987 7 int use_protocol_v1 = 0;
988 7 int compression_set = 0;
989 7 IV compression_format = 0;
990 7 int snappy = 0;
991 7 int snappy_incr = 0;
992 7 int want_freeze = 0;
993 7 int paranoid = 0;
994 7 int refuse_objects_set = 0;
995 7 int max_recursion_depth_set = 0;
996 7 int use_standard_double_set = 0;
997 7 int native_float_requested = 0;
998  
999 7 ENTER;
1000 7 enc = srl_empty_encoder_struct(aTHX);
1001 7 SAVEDESTRUCTOR_X(&srl_encoder_constructor_cleanup, (void *)enc);
1002 7 enc->scratch_sv = newSViv(0);
1003 7 SRL_ENC_SET_OPTION(enc, SRL_F_SHARED_HASHKEYS);
1004  
1005 /* Visit supplied options once. Independent settings are applied here;
1006 * aliases and settings that depend on one another are resolved below. */
1007 7 ▶ if (opt != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt != NULL) { 6 1 7
1008 6 hv_iterinit(opt);
1009 12 ▶ while ((entry = hv_iternext(opt))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(opt))) { 6 6 12
1010 6 SV *key = hv_iterkeysv(entry);
1011 STRLEN key_len;
1012 6 const char *key_bytes = SvPV(key, key_len);
1013 6 U32 option_id = srl_encoder_option_id(aTHX_ options, SRL_ENC_OPT_COUNT,
1014 known_option_heads, known_option_next,
1015 6 HeHASH(entry), key_bytes, key_len);
1016 6 ▶ if (option_id == SRL_ENC_OPT_COUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if (option_id == SRL_ENC_OPT_COUNT) { 0 6 6
1017 0 ▶ if (!unknown_option_keys)
Branch: 1 branch
BranchTrueFalseTotal
if (!unknown_option_keys) 0 0 0
1018 0 unknown_option_keys = (AV *)sv_2mortal((SV *)newAV());
1019 0 av_push(unknown_option_keys, newSVsv(key));
1020 0 continue;
1021 }
1022 6 val = HeVAL(entry);
1023 6 switch (option_id) {
1024 0 case SRL_ENC_OPT_IDX_FLOAT_TYPE:
1025 {
1026 STRLEN len;
1027 0 const char *type = SvPV(val, len);
1028 0 ▶ if (memEQs(type, len, "native")) {
Branch: 2 branches
BranchTrueFalseTotal
if (memEQs(type, len, "native")) { 0 0 0
if (memEQs(type, len, "native")) { 0 0 0
1029 0 native_float_requested = 1;
1030 #if defined(USE_LONG_DOUBLE) && !defined(HAS_QUADMATH)
1031 SRL_ENC_SET_OPTION(enc, SRL_F_USE_NATIVE_LONG_DOUBLE);
1032 #endif
1033 0 ▶ } else if (!memEQs(type, len, "std")) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (!memEQs(type, len, "std")) { 0 0 0
} else if (!memEQs(type, len, "std")) { 0 0 0
1034 0 croak("Invalid float_type; expected std or native");
1035 }
1036 }
1037 0 break;
1038 0 case SRL_ENC_OPT_IDX_NO_SHARED_HASHKEYS:
1039 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1040 0 SRL_ENC_RESET_OPTION(enc, SRL_F_SHARED_HASHKEYS);
1041 0 break;
1042 6 case SRL_ENC_OPT_IDX_PROTOCOL_VERSION:
1043 6 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 6 0 6
1044 6 enc->protocol_version = SvUV(val);
1045 6 ▶ if (enc->protocol_version < 1 || enc->protocol_version > SRL_PROTOCOL_VERSION)
Branch: 2 branches
BranchTrueFalseTotal
if (enc->protocol_version < 1 || enc->protocol_version > SRL_PROTOCOL_VERSION) 6 0 6
if (enc->protocol_version < 1 || enc->protocol_version > SRL_PROTOCOL_VERSION) 0 6 6
1046 0 croak("Specified Sereal protocol version (%"UVuf") is invalid",
1047 (UV)enc->protocol_version);
1048 6 protocol_version_set = 1;
1049 }
1050 6 break;
1051 0 case SRL_ENC_OPT_IDX_USE_PROTOCOL_V1:
1052 0 use_protocol_v1 = SvTRUE(val) ? 1 : 0;
1053 0 break;
1054 0 case SRL_ENC_OPT_IDX_CROAK_ON_BLESS:
1055 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1056 0 SRL_ENC_SET_OPTION(enc, SRL_F_CROAK_ON_BLESS);
1057 0 break;
1058 0 case SRL_ENC_OPT_IDX_REFUSE_OBJECTS:
1059 0 refuse_objects_set = 1;
1060 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1061 0 SRL_ENC_SET_OPTION(enc, SRL_F_REFUSE_OBJECTS);
1062 0 break;
1063 0 case SRL_ENC_OPT_IDX_PARANOID:
1064 0 paranoid = SvTRUE(val) ? 1 : 0;
1065 0 break;
1066 0 case SRL_ENC_OPT_IDX_NO_BLESS_OBJECTS:
1067 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1068 0 SRL_ENC_SET_OPTION(enc, SRL_F_NO_BLESS_OBJECTS);
1069 0 break;
1070 0 case SRL_ENC_OPT_IDX_FREEZE_CALLBACKS:
1071 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1072 0 want_freeze = 1;
1073 0 break;
1074 0 case SRL_ENC_OPT_IDX_FREEZE_ALLOW_CLASSES:
1075 0 freeze_allow_classes_value = val;
1076 0 break;
1077 0 case SRL_ENC_OPT_IDX_ALLOW_CLASSES:
1078 0 allow_classes_value = val;
1079 0 break;
1080 0 case SRL_ENC_OPT_IDX_TRACK_CLASSES:
1081 0 track_classes_value = val;
1082 0 break;
1083 0 case SRL_ENC_OPT_IDX_FREEZE_DENY_ACTION:
1084 0 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 0 0 0
1085 STRLEN len;
1086 0 const char *action = SvPV(val, len);
1087 0 ▶ if (memEQs(action, len, "serialize"))
Branch: 2 branches
BranchTrueFalseTotal
if (memEQs(action, len, "serialize")) 0 0 0
if (memEQs(action, len, "serialize")) 0 0 0
1088 0 SRL_ENC_SET_OPTION(enc, SRL_F_FREEZE_DENY_SERIALIZE);
1089 0 ▶ else if (!memEQs(action, len, "croak"))
Branch: 2 branches
BranchTrueFalseTotal
else if (!memEQs(action, len, "croak")) 0 0 0
else if (!memEQs(action, len, "croak")) 0 0 0
1090 0 croak("The 'freeze_deny_action' option must be 'croak' or 'serialize'");
1091 }
1092 0 break;
1093 0 case SRL_ENC_OPT_IDX_COMPRESS:
1094 0 compression_set = 1;
1095 0 compression_format = SvIV(val);
1096 0 break;
1097 0 case SRL_ENC_OPT_IDX_SNAPPY:
1098 0 snappy = SvTRUE(val) ? 1 : 0;
1099 0 break;
1100 0 case SRL_ENC_OPT_IDX_SNAPPY_INCR:
1101 0 snappy_incr = SvTRUE(val) ? 1 : 0;
1102 0 break;
1103 0 case SRL_ENC_OPT_IDX_COMPRESS_LEVEL:
1104 0 compress_level_value = val;
1105 0 break;
1106 0 case SRL_ENC_OPT_IDX_COMPRESS_THRESHOLD:
1107 0 compress_threshold_value = val;
1108 0 break;
1109 0 case SRL_ENC_OPT_IDX_SNAPPY_THRESHOLD:
1110 0 snappy_threshold_value = val;
1111 0 break;
1112 0 case SRL_ENC_OPT_IDX_UNDEF_UNKNOWN:
1113 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1114 0 SRL_ENC_SET_OPTION(enc, SRL_F_UNDEF_UNKNOWN);
1115 0 break;
1116 0 case SRL_ENC_OPT_IDX_STRINGIFY_UNKNOWN:
1117 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1118 0 SRL_ENC_SET_OPTION(enc, SRL_F_STRINGIFY_UNKNOWN);
1119 0 break;
1120 0 case SRL_ENC_OPT_IDX_WARN_UNKNOWN:
1121 0 ▶ if (SvTRUE(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) { 0 0 0
1122 0 SRL_ENC_SET_OPTION(enc, SRL_F_WARN_UNKNOWN);
1123 0 ▶ if (SvIV(val) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (SvIV(val) < 0) 0 0 0
1124 0 SRL_ENC_SET_OPTION(enc, SRL_F_NOWARN_UNKNOWN_OVERLOAD);
1125 }
1126 0 break;
1127 0 case SRL_ENC_OPT_IDX_SORT_KEYS:
1128 0 sort_keys_value = val;
1129 0 break;
1130 0 case SRL_ENC_OPT_IDX_CANONICAL_REFS:
1131 0 canonical_refs_value = val;
1132 0 break;
1133 0 case SRL_ENC_OPT_IDX_CANONICAL:
1134 0 canonical_value = val;
1135 0 break;
1136 0 case SRL_ENC_OPT_IDX_ALIASED_DEDUPE_STRINGS:
1137 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1138 0 SRL_ENC_SET_OPTION(enc, SRL_F_ALIASED_DEDUPE_STRINGS | SRL_F_DEDUPE_STRINGS);
1139 0 break;
1140 0 case SRL_ENC_OPT_IDX_DEDUPE_STRINGS:
1141 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1142 0 SRL_ENC_SET_OPTION(enc, SRL_F_DEDUPE_STRINGS);
1143 0 break;
1144 0 case SRL_ENC_OPT_IDX_MAX_RECURSION_DEPTH:
1145 0 max_recursion_depth_set = 1;
1146 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1147 0 enc->max_recursion_depth = SvUV(val);
1148 0 break;
1149 0 case SRL_ENC_OPT_IDX_USE_STANDARD_DOUBLE:
1150 0 use_standard_double_set = 1;
1151 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
1152 0 SRL_ENC_SET_OPTION(enc, SRL_F_USE_STANDARD_DOUBLE);
1153 0 break;
1154 0 case SRL_ENC_OPT_IDX_UNKNOWN_OPTION_POLICY:
1155 0 unknown_option_policy_value = val;
1156 0 break;
1157 0 case SRL_ENC_OPT_IDX_CHECKSUM_POLICY:
1158 0 checksum_policy_value = val;
1159 0 break;
1160 0 case SRL_ENC_OPT_IDX_CHECKSUM_KEY:
1161 0 srl_set_checksum_key(aTHX_ &checksum_key, val);
1162 0 break;
1163 0 case SRL_ENC_OPT_IDX_CHECKSUM_MODE:
1164 0 srl_set_checksum_mode(aTHX_ &checksum_mode,
1165 srl_checksum_mode_from_sv(aTHX_ val, "checksum_mode"));
1166 0 break;
1167 0 case SRL_ENC_OPT_IDX_CHECKSUM_KEY_V1:
1168 0 srl_set_checksum_key(aTHX_ &checksum_key, val);
1169 0 srl_set_checksum_mode(aTHX_ &checksum_mode, SRL_CHECKSUM_MODE_V1);
1170 0 break;
1171 0 case SRL_ENC_OPT_IDX_CHECKSUM_KEY_V2:
1172 0 srl_set_checksum_key(aTHX_ &checksum_key, val);
1173 0 srl_set_checksum_mode(aTHX_ &checksum_mode, SRL_CHECKSUM_MODE_V2);
1174 0 break;
1175 0 case SRL_ENC_OPT_IDX_CHECKSUM_KEY_V3:
1176 0 srl_set_checksum_key(aTHX_ &checksum_key, val);
1177 0 srl_set_checksum_mode(aTHX_ &checksum_mode, SRL_CHECKSUM_MODE_V3);
1178 0 break;
1179 0 default:
1180 0 croak("Internal error: unhandled Encoder option ID %u", (unsigned int)option_id);
1181 }
1182 }
1183 }
1184  
1185 /* Traverse nested allow-lists after the options loop: the allow-list
1186 * may be the options hash itself, whose iterator must not be reset. */
1187 7 val = freeze_allow_classes_value;
1188 7 ▶ if ( val && SvOK(val) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( val && SvOK(val) ) { 0 7 7
if ( val && SvOK(val) ) { 0 0 0
1189 0 srl_set_class_allow_list(aTHX_ val, "freeze_allow_classes",
1190 &enc->freeze_allow_hash, &enc->freeze_allow_cb);
1191 0 want_freeze = 1;
1192 }
1193 7 val = allow_classes_value;
1194 7 ▶ if (val && SvOK(val))
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvOK(val)) 0 7 7
if (val && SvOK(val)) 0 0 0
1195 0 srl_set_class_allow_list(aTHX_ val, "allow_classes",
1196 &enc->allow_hash, &enc->allow_cb);
1197 7 val = track_classes_value;
1198 7 ▶ if (val && SvOK(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvOK(val)) { 0 7 7
if (val && SvOK(val)) { 0 0 0
1199 0 ▶ SV *rv = SvROK(val) ? SvRV(val) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *rv = SvROK(val) ? SvRV(val) : NULL; 0 0 0
1200 0 ▶ if (!rv || SvTYPE(rv) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!rv || SvTYPE(rv) != SVt_PVHV) 0 0 0
if (!rv || SvTYPE(rv) != SVt_PVHV) 0 0 0
1201 0 croak("The 'track_classes' option must be a hash ref");
1202 0 enc->track_classes = (HV *)SvREFCNT_inc(rv);
1203 }
1204  
1205 7 ▶ if (paranoid) {
Branch: 1 branch
BranchTrueFalseTotal
if (paranoid) { 0 7 7
1206 0 ▶ if (!max_recursion_depth_set)
Branch: 1 branch
BranchTrueFalseTotal
if (!max_recursion_depth_set) 0 0 0
1207 0 enc->max_recursion_depth = PARANOID_MAX_RECUR_DEPTH;
1208 0 ▶ if (!use_standard_double_set && !native_float_requested)
Branch: 2 branches
BranchTrueFalseTotal
if (!use_standard_double_set && !native_float_requested) 0 0 0
if (!use_standard_double_set && !native_float_requested) 0 0 0
1209 0 SRL_ENC_SET_OPTION(enc, SRL_F_USE_STANDARD_DOUBLE);
1210 0 ▶ if (!refuse_objects_set && !enc->allow_hash && !enc->allow_cb)
Branch: 3 branches
BranchTrueFalseTotal
if (!refuse_objects_set && !enc->allow_hash && !enc->allow_cb) 0 0 0
if (!refuse_objects_set && !enc->allow_hash && !enc->allow_cb) 0 0 0
if (!refuse_objects_set && !enc->allow_hash && !enc->allow_cb) 0 0 0
1211 0 SRL_ENC_SET_OPTION(enc, SRL_F_REFUSE_OBJECTS);
1212 0 ▶ if (!unknown_option_policy_value)
Branch: 1 branch
BranchTrueFalseTotal
if (!unknown_option_policy_value) 0 0 0
1213 0 unknown_option_policy_value = sv_2mortal(newSVpvs("die"));
1214 }
1215  
1216 7 srl_handle_unknown_option_keys(aTHX_ unknown_option_keys, unknown_option_policy_value,
1217 "Sereal::Encoder", "Sereal::Encoder::UnknownOptionPolicy");
1218  
1219 7 ▶ if (!protocol_version_set && use_protocol_v1)
Branch: 2 branches
BranchTrueFalseTotal
if (!protocol_version_set && use_protocol_v1) 1 6 7
if (!protocol_version_set && use_protocol_v1) 0 1 1
1220 0 enc->protocol_version = 1;
1221 7 ▶ if (want_freeze) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_freeze) { 0 7 7
1222 0 ▶ if (enc->protocol_version < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->protocol_version < 2) 0 0 0
1223 0 croak("FREEZE/THAW callbacks require Sereal protocol version 2 or later");
1224 0 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_NO_BLESS_OBJECTS))
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_NO_BLESS_OBJECTS)) 0 0 0
1225 0 croak("The no_bless_objects option is mutually exclusive "
1226 "with freeze support (freeze_callbacks / freeze_allow_classes)");
1227 0 SRL_ENC_SET_OPTION(enc, SRL_F_ENABLE_FREEZE_SUPPORT);
1228 0 enc->sereal_string_sv = newSVpvs("Sereal");
1229 }
1230 7 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_UNDEF_UNKNOWN)
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_UNDEF_UNKNOWN) 0 7 7
1231 0 ▶ && SRL_ENC_HAVE_OPTION(enc, SRL_F_STRINGIFY_UNKNOWN))
Branch: 1 branch
BranchTrueFalseTotal
&& SRL_ENC_HAVE_OPTION(enc, SRL_F_STRINGIFY_UNKNOWN)) 0 0 0
1232 0 croak("'undef_unknown' and 'stringify_unknown' options are mutually exclusive");
1233  
1234 7 ▶ val = sort_keys_value ? sort_keys_value : canonical_value;
Branch: 1 branch
BranchTrueFalseTotal
val = sort_keys_value ? sort_keys_value : canonical_value; 0 7 7
1235 7 ▶ if (val && SvTRUE(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvTRUE(val)) { 0 7 7
if (val && SvTRUE(val)) { 0 0 0
1236 0 SRL_ENC_SET_OPTION(enc, SRL_F_SORT_KEYS);
1237 0 ▶ if (SvIV(val) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIV(val) > 1) { 0 0 0
1238 0 SRL_ENC_SET_OPTION(enc, SRL_F_SORT_KEYS_PERL);
1239 0 ▶ if (SvIV(val) > 2)
Branch: 1 branch
BranchTrueFalseTotal
if (SvIV(val) > 2) 0 0 0
1240 0 SRL_ENC_SET_OPTION(enc, SRL_F_SORT_KEYS_PERL_REV);
1241 }
1242 }
1243 7 ▶ val = canonical_refs_value ? canonical_refs_value : canonical_value;
Branch: 1 branch
BranchTrueFalseTotal
val = canonical_refs_value ? canonical_refs_value : canonical_value; 0 7 7
1244 7 ▶ if (val && SvTRUE(val))
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvTRUE(val)) 0 7 7
if (val && SvTRUE(val)) 0 0 0
1245 0 SRL_ENC_SET_OPTION(enc, SRL_F_CANONICAL_REFS);
1246  
1247 7 ▶ if (!compression_set && (snappy_incr || snappy))
Branch: 3 branches
BranchTrueFalseTotal
if (!compression_set && (snappy_incr || snappy)) 7 0 7
if (!compression_set && (snappy_incr || snappy)) 7 0 7
if (!compression_set && (snappy_incr || snappy)) 0 7 7
1248 0 compression_format = 1;
1249 7 switch (compression_format) {
1250 7 case 0:
1251 7 break;
1252 0 case 1:
1253 0 ▶ if (compression_set || snappy_incr || enc->protocol_version > 1)
Branch: 3 branches
BranchTrueFalseTotal
if (compression_set || snappy_incr || enc->protocol_version > 1) 0 0 0
if (compression_set || snappy_incr || enc->protocol_version > 1) 0 0 0
if (compression_set || snappy_incr || enc->protocol_version > 1) 0 0 0
1254 0 SRL_ENC_SET_OPTION(enc, SRL_F_COMPRESS_SNAPPY_INCREMENTAL);
1255 else
1256 0 SRL_ENC_SET_OPTION(enc, SRL_F_COMPRESS_SNAPPY);
1257 0 break;
1258 0 case 2:
1259 0 ▶ if (enc->protocol_version < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->protocol_version < 3) 0 0 0
1260 0 croak("Zlib compression was introduced in protocol version 3 and you are asking for only version %i", (int)enc->protocol_version);
1261 0 SRL_ENC_SET_OPTION(enc, SRL_F_COMPRESS_ZLIB);
1262 0 enc->compress_level = MZ_DEFAULT_COMPRESSION;
1263 0 ▶ if (compress_level_value && SvTRUE(compress_level_value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (compress_level_value && SvTRUE(compress_level_value)) { 0 0 0
if (compress_level_value && SvTRUE(compress_level_value)) { 0 0 0
1264 0 IV level = SvIV(compress_level_value);
1265 0 ▶ if (expect_false(level < 1 || level > 10))
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(level < 1 || level > 10)) 0 0 0
if (expect_false(level < 1 || level > 10)) 0 0 0
if (expect_false(level < 1 || level > 10)) 0 0 0
1266 0 croak("'compress_level' needs to be between 1 and 9");
1267 0 enc->compress_level = level;
1268 }
1269 0 break;
1270 0 case 3:
1271 0 ▶ if (enc->protocol_version < 4)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->protocol_version < 4) 0 0 0
1272 0 croak("zstd compression was introduced in protocol version 4 and you are asking for only version %i", (int)enc->protocol_version);
1273 0 SRL_ENC_SET_OPTION(enc, SRL_F_COMPRESS_ZSTD);
1274 0 enc->compress_level = 3;
1275 0 ▶ if (compress_level_value && SvTRUE(compress_level_value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (compress_level_value && SvTRUE(compress_level_value)) { 0 0 0
if (compress_level_value && SvTRUE(compress_level_value)) { 0 0 0
1276 0 IV level = SvIV(compress_level_value);
1277 0 ▶ if (expect_false(level < 1 || level > 22))
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(level < 1 || level > 22)) 0 0 0
if (expect_false(level < 1 || level > 22)) 0 0 0
if (expect_false(level < 1 || level > 22)) 0 0 0
1278 0 croak("'compress_level' needs to be between 1 and 22");
1279 0 enc->compress_level = level;
1280 }
1281 0 break;
1282 0 default:
1283 0 croak("Invalid Sereal compression format");
1284 }
1285 7 ▶ if (compression_format) {
Branch: 1 branch
BranchTrueFalseTotal
if (compression_format) { 0 7 7
1286 0 enc->compress_threshold = 1024;
1287 0 ▶ if (compress_threshold_value && SvOK(compress_threshold_value))
Branch: 2 branches
BranchTrueFalseTotal
if (compress_threshold_value && SvOK(compress_threshold_value)) 0 0 0
if (compress_threshold_value && SvOK(compress_threshold_value)) 0 0 0
1288 0 enc->compress_threshold = SvIV(compress_threshold_value);
1289 0 ▶ else if (compression_format == 1 && snappy_threshold_value && SvOK(snappy_threshold_value))
Branch: 3 branches
BranchTrueFalseTotal
else if (compression_format == 1 && snappy_threshold_value && SvOK(snappy_threshold_value)) 0 0 0
else if (compression_format == 1 && snappy_threshold_value && SvOK(snappy_threshold_value)) 0 0 0
else if (compression_format == 1 && snappy_threshold_value && SvOK(snappy_threshold_value)) 0 0 0
1290 0 enc->compress_threshold = SvIV(snappy_threshold_value);
1291 }
1292  
1293 7 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_USE_NATIVE_LONG_DOUBLE)
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_USE_NATIVE_LONG_DOUBLE) 0 7 7
1294 0 ▶ && SRL_ENC_HAVE_OPTION(enc, SRL_F_USE_STANDARD_DOUBLE))
Branch: 1 branch
BranchTrueFalseTotal
&& SRL_ENC_HAVE_OPTION(enc, SRL_F_USE_STANDARD_DOUBLE)) 0 0 0
1295 0 croak("float_type 'native' and use_standard_double are mutually exclusive");
1296  
1297 /* Paranoid requires authentication unless the caller explicitly
1298 * selects another policy. Package defaults cannot weaken it. */
1299 7 ▶ if (paranoid && (!checksum_policy_value || !SvOK(checksum_policy_value)))
Branch: 3 branches
BranchTrueFalseTotal
if (paranoid && (!checksum_policy_value || !SvOK(checksum_policy_value))) 0 7 7
if (paranoid && (!checksum_policy_value || !SvOK(checksum_policy_value))) 0 0 0
if (paranoid && (!checksum_policy_value || !SvOK(checksum_policy_value))) 0 0 0
1300 0 checksum_policy_value = sv_2mortal(newSVpvs("require_key"));
1301 7 enc->checksum_policy = srl_get_checksum_policy(aTHX_ checksum_policy_value, "Sereal::Encoder::ChecksumPolicy");
1302 7 ▶ if (checksum_mode == SRL_CHECKSUM_MODE_UNSET)
Branch: 1 branch
BranchTrueFalseTotal
if (checksum_mode == SRL_CHECKSUM_MODE_UNSET) 7 0 7
1303 7 checksum_mode = srl_get_default_checksum_mode(aTHX_ "Sereal::Encoder::ChecksumMode",
1304 SRL_CHECKSUM_MODE_V3);
1305 7 ▶ if (!checksum_key)
Branch: 1 branch
BranchTrueFalseTotal
if (!checksum_key) 7 0 7
1306 7 checksum_key = srl_get_default_checksum_key(aTHX_ "Sereal::Encoder::DefaultKey");
1307 7 ▶ if (checksum_mode == SRL_CHECKSUM_MODE_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if (checksum_mode == SRL_CHECKSUM_MODE_NONE) 0 7 7
1308 0 checksum_key = NULL;
1309 7 srl_validate_checksum_key(aTHX_ checksum_key, "checksum_key", enc->checksum_policy);
1310 7 srl_validate_checksum_required_key(aTHX_ checksum_key, enc->checksum_policy);
1311 7 srl_validate_checksum_required_mode(aTHX_ checksum_mode, enc->checksum_policy);
1312 7 ▶ if (checksum_key && SvOK(checksum_key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (checksum_key && SvOK(checksum_key)) { 7 0 7
if (checksum_key && SvOK(checksum_key)) { 0 7 7
1313 STRLEN key_len;
1314 0 char *key = SvPVbyte(checksum_key, key_len);
1315 0 ▶ if (key_len != sizeof(enc->checksum_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (key_len != sizeof(enc->checksum_key)) { 0 0 0
1316 0 ▶ if (key_len < sizeof(enc->checksum_key))
Branch: 1 branch
BranchTrueFalseTotal
if (key_len < sizeof(enc->checksum_key)) 0 0 0
1317 0 warn("The 'checksum_key' key is shorter than 16 bytes; padding with nulls. Use Sereal::Encoder::prepare_checksum_key() to normalize it");
1318 else
1319 0 warn("The 'checksum_key' key is longer than 16 bytes; extra bytes are ignored and may contain unused entropy. Use Sereal::Encoder::prepare_checksum_key() to normalize it");
1320 }
1321 0 ▶ if (enc->protocol_version < 6)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->protocol_version < 6) 0 0 0
1322 0 croak("The 'checksum_key' option requires Sereal protocol version 6 or later");
1323 0 Zero(enc->checksum_key, sizeof(enc->checksum_key), U8);
1324 0 Copy(key, enc->checksum_key,
1325 key_len < sizeof(enc->checksum_key) ? key_len : sizeof(enc->checksum_key), U8);
1326 0 enc->checksum_key_set = 1;
1327 0 enc->siphash_version = checksum_mode - 1;
1328 }
1329  
1330 DEBUG_ASSERT_BUF_SANE(&enc->buf);
1331 7 SRL_ENC_SET_OPTION(enc, SRL_F_REUSE_ENCODER);
1332 7 LEAVE;
1333 7 SRL_ENC_RESET_OPTION(enc, SRL_F_REUSE_ENCODER);
1334 7 return enc;
1335 }
1336  
1337 /* clone an encoder without current state */
1338 srl_encoder_t *
1339 0 srl_build_encoder_struct_alike(pTHX_ srl_encoder_t *proto)
1340 {
1341 srl_encoder_t *enc;
1342 0 enc = srl_empty_encoder_struct(aTHX);
1343  
1344 /* Copy the configuration-type, non-ephemeral attributes */
1345 0 enc->flags = proto->flags;
1346 0 enc->max_recursion_depth = proto->max_recursion_depth;
1347 0 enc->compress_threshold = proto->compress_threshold;
1348 0 enc->compress_level = proto->compress_level;
1349 0 enc->zstd_cctx = NULL;
1350 0 ▶ if (expect_false(SRL_ENC_HAVE_OPTION(enc, SRL_F_ENABLE_FREEZE_SUPPORT))) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(SRL_ENC_HAVE_OPTION(enc, SRL_F_ENABLE_FREEZE_SUPPORT))) { 0 0 0
1351 0 enc->sereal_string_sv = newSVpvs("Sereal");
1352 }
1353 /* freeze allow-list is immutable config; share it with the clone. */
1354 0 ▶ if (proto->freeze_allow_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->freeze_allow_hash) { 0 0 0
1355 0 enc->freeze_allow_hash = proto->freeze_allow_hash;
1356 0 SvREFCNT_inc((SV*)enc->freeze_allow_hash);
1357 }
1358 0 ▶ if (proto->freeze_allow_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->freeze_allow_cb) { 0 0 0
1359 0 enc->freeze_allow_cb = proto->freeze_allow_cb;
1360 0 SvREFCNT_inc(enc->freeze_allow_cb);
1361 }
1362 0 ▶ if (proto->allow_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->allow_hash) { 0 0 0
1363 0 enc->allow_hash = proto->allow_hash;
1364 0 SvREFCNT_inc((SV*)enc->allow_hash);
1365 }
1366 0 ▶ if (proto->allow_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->allow_cb) { 0 0 0
1367 0 enc->allow_cb = proto->allow_cb;
1368 0 SvREFCNT_inc(enc->allow_cb);
1369 }
1370 0 ▶ if (proto->track_classes) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->track_classes) { 0 0 0
1371 0 enc->track_classes = proto->track_classes;
1372 0 SvREFCNT_inc((SV*)enc->track_classes);
1373 }
1374 0 enc->protocol_version = proto->protocol_version;
1375 0 enc->checksum_key_set = proto->checksum_key_set;
1376 0 enc->checksum_policy = proto->checksum_policy;
1377 0 enc->siphash_version = proto->siphash_version;
1378 0 Copy(proto->checksum_key, enc->checksum_key, sizeof(enc->checksum_key), U8);
1379 0 enc->scratch_sv= newSViv(0);
1380 DEBUG_ASSERT_BUF_SANE(&enc->buf);
1381 0 return enc;
1382 }
1383  
1384 SRL_STATIC_INLINE PTABLE_t *
1385 0 srl_init_string_hash(srl_encoder_t *enc)
1386 {
1387 0 enc->str_seenhash = PTABLE_new_size(4);
1388 0 return enc->str_seenhash;
1389 }
1390  
1391 SRL_STATIC_INLINE PTABLE_t *
1392 7 srl_init_ref_hash(srl_encoder_t *enc)
1393 {
1394 7 enc->ref_seenhash = PTABLE_new_size(4);
1395 7 return enc->ref_seenhash;
1396 }
1397  
1398 SRL_STATIC_INLINE PTABLE_t *
1399 0 srl_init_weak_hash(srl_encoder_t *enc)
1400 {
1401 0 enc->weak_seenhash = PTABLE_new_size(3);
1402 0 return enc->weak_seenhash;
1403 }
1404  
1405 SRL_STATIC_INLINE PTABLE_t *
1406 0 srl_init_freezeobj_svhash(srl_encoder_t *enc)
1407 {
1408 0 enc->freezeobj_svhash = PTABLE_new_size(3);
1409 0 return enc->freezeobj_svhash;
1410 }
1411  
1412  
1413 void
1414 7 srl_write_header(pTHX_ srl_encoder_t *enc, SV *user_header_src, const U32 compress_flags)
1415 {
1416 /* 4th to 8th bit are flags. Using 4th for snappy flag. FIXME needs to go in spec. */
1417  
1418 7 U8 flags= srl_get_compression_header_flag(compress_flags);
1419 7 const U8 version_and_flags = (U8)enc->protocol_version | flags;
1420  
1421 /* 4 byte magic string + proto version
1422 * + potentially uncompressed size varint
1423 * + 1 byte varint that indicates zero-length header */
1424 7 ▶ BUF_SIZE_ASSERT(&enc->buf, sizeof(SRL_MAGIC_STRING) + 1 + 1);
Branch: 4 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(&enc->buf, sizeof(SRL_MAGIC_STRING) + 1 + 1); 7 0 7
BUF_SIZE_ASSERT(&enc->buf, sizeof(SRL_MAGIC_STRING) + 1 + 1); 0 7 7
BUF_SIZE_ASSERT(&enc->buf, sizeof(SRL_MAGIC_STRING) + 1 + 1); 0 7 7
BUF_SIZE_ASSERT(&enc->buf, sizeof(SRL_MAGIC_STRING) + 1 + 1); 0 0 0
1425 7 ▶ if (expect_true( enc->protocol_version > 2 ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true( enc->protocol_version > 2 )) 5 2 7
1426 5 srl_buf_cat_str_s_nocheck(&enc->buf, SRL_MAGIC_STRING_HIGHBIT);
1427 else
1428 2 srl_buf_cat_str_s_nocheck(&enc->buf, SRL_MAGIC_STRING);
1429 7 srl_buf_cat_char_nocheck(&enc->buf, version_and_flags);
1430 7 ▶ if (user_header_src == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (user_header_src == NULL) { 7 0 7
1431 7 srl_buf_cat_char_nocheck(&enc->buf, '\0'); /* variable header length (0 right now) */
1432 }
1433 else {
1434 STRLEN user_data_len;
1435  
1436 0 ▶ if (expect_false( enc->protocol_version < 2 ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( enc->protocol_version < 2 )) 0 0 0
1437 0 croak("Cannot serialize user header data in Sereal protocol V1 mode!");
1438  
1439 /* Allocate tmp buffer for swapping if necessary,
1440 * will be cleaned up automatically */
1441 0 ▶ if (enc->tmp_buf.start == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc->tmp_buf.start == NULL) { 0 0 0
1442 0 ▶ if (expect_false(srl_buf_init_buffer(aTHX_ &enc->tmp_buf, INITIALIZATION_SIZE) != 0))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(srl_buf_init_buffer(aTHX_ &enc->tmp_buf, INITIALIZATION_SIZE) != 0)) 0 0 0
1443 0 croak("Out of memory");
1444 }
1445  
1446 /* Write document body (for header) into separate buffer */
1447 0 srl_buf_swap_buffer(aTHX_ &enc->tmp_buf, &enc->buf);
1448 0 ▶ SRL_ENC_UPDATE_BODY_POS(enc);
Branch: 1 branch
BranchTrueFalseTotal
SRL_ENC_UPDATE_BODY_POS(enc); 0 0 0
1449 0 enc->encoding_header = 1;
1450 0 srl_dump_sv(aTHX_ enc, user_header_src);
1451 0 enc->encoding_header = 0;
1452 0 srl_fixup_weakrefs(aTHX_ enc); /* more bodies to follow */
1453 0 srl_clear_seen_hashes(aTHX_ enc); /* more bodies to follow */
1454  
1455 /* Swap main buffer back in, encode header length&bitfield, copy user header data */
1456 0 user_data_len = BUF_POS_OFS(&enc->buf);
1457 0 srl_buf_swap_buffer(aTHX_ &enc->buf, &enc->tmp_buf);
1458  
1459 0 ▶ if (expect_false((size_t)user_data_len > SRL_BUF_MAX_SIZE - (size_t)(1 + SRL_MAX_VARINT_LENGTH)))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false((size_t)user_data_len > SRL_BUF_MAX_SIZE - (size_t)(1 + SRL_MAX_VARINT_LENGTH))) 0 0 0
1460 0 croak("Sereal output buffer is too large");
1461 0 ▶ BUF_SIZE_ASSERT(&enc->buf, user_data_len + 1 + SRL_MAX_VARINT_LENGTH); /* +1 for bit field, +X for header len */
Branch: 4 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(&enc->buf, user_data_len + 1 + SRL_MAX_VARINT_LENGTH); /* +1 for bit field, +X for header len */ 0 0 0
BUF_SIZE_ASSERT(&enc->buf, user_data_len + 1 + SRL_MAX_VARINT_LENGTH); /* +1 for bit field, +X for header len */ 0 0 0
BUF_SIZE_ASSERT(&enc->buf, user_data_len + 1 + SRL_MAX_VARINT_LENGTH); /* +1 for bit field, +X for header len */ 0 0 0
BUF_SIZE_ASSERT(&enc->buf, user_data_len + 1 + SRL_MAX_VARINT_LENGTH); /* +1 for bit field, +X for header len */ 0 0 0
1462  
1463 /* Encode header length */
1464 0 srl_buf_cat_varint_nocheck(aTHX_ &enc->buf, 0, (UV)(user_data_len + 1)); /* +1 for bit field */
1465 /* Encode bitfield */
1466 0 srl_buf_cat_char_nocheck(&enc->buf, '\1');
1467 /* Copy user header data */
1468 0 Copy(enc->tmp_buf.start, enc->buf.pos, user_data_len, char);
1469 0 enc->buf.pos += user_data_len;
1470  
1471 0 enc->tmp_buf.pos = enc->tmp_buf.start; /* reset tmp buffer just to be clean */
1472 }
1473 7 }
1474  
1475 /* The following is to handle the fact that under normal build options
1476 * VC6 will compare all floating point at 80 bits of precision, regardless
1477 * regardless of the type.
1478 * By setting the vars to "volatile" we avoid this behavior.
1479 * Hopefully this fixes various remaining Win32 test failures we see.
1480 *
1481 * Note this patch could not have been written without Bulk88's help.
1482 * Thanks a lot man!
1483 *
1484 * Comment from Bulk88:
1485 * -O1 and -O2 tested and both of those 2 "failed"
1486 * -Op - Improve Float Consistency does not have the bug
1487 * Problem not seen in VC 2003
1488 * I (Bulk88) don't have a VC 2002 to test v13 officially
1489 *
1490 */
1491 #if defined(_MSC_VER)
1492 # if _MSC_VER < 1300
1493 # define MS_VC6_WORKAROUND_VOLATILE volatile
1494 # else
1495 # define MS_VC6_WORKAROUND_VOLATILE
1496 # endif
1497 #else
1498 # define MS_VC6_WORKAROUND_VOLATILE
1499 #endif
1500  
1501  
1502 /* Native values are copied only after the caller selects that representation
1503 * or a legacy protocol requires it. Known x87 padding can be cleared; opaque
1504 * layouts deliberately retain raw storage because their padding is unknown. */
1505 SRL_STATIC_INLINE void
1506 0 srl_dump_native_nv(pTHX_ srl_encoder_t *enc, NV nv, U8 tag)
1507 {
1508 0 ▶ BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(nv));
Branch: 4 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(nv)); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(nv)); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(nv)); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(nv)); 0 0 0
1509 0 srl_buf_cat_char_nocheck(&enc->buf, tag);
1510 0 Copy((char *)&nv, enc->buf.pos, sizeof(nv), char);
1511 #if defined(USE_LONG_DOUBLE) && !defined(HAS_QUADMATH)
1512 if ((srl_native_long_double_format() & 127) == SRL_LD_X87)
1513 memset(enc->buf.pos + 10, 0, sizeof(nv) - 10);
1514 #endif
1515 0 enc->buf.pos += sizeof(nv);
1516 0 }
1517  
1518 /* Code for serializing floats */
1519 SRL_STATIC_INLINE void
1520 0 srl_dump_nv(pTHX_ srl_encoder_t *enc, SV *src)
1521 {
1522 0 NV nv= SvNV_nomg(src);
1523 MS_VC6_WORKAROUND_VOLATILE float f;
1524 MS_VC6_WORKAROUND_VOLATILE double d;
1525 /* An unidentified native representation must not depend on our ability
1526 * to produce compact IEEE values. Raw output avoids those conversions
1527 * and preserves same-ABI round trips even when the layout is unnamed. */
1528 0 ▶ if (enc->protocol_version >= 7 && !enc->encoding_header
Branch: 2 branches
BranchTrueFalseTotal
if (enc->protocol_version >= 7 && !enc->encoding_header 0 0 0
if (enc->protocol_version >= 7 && !enc->encoding_header 0 0 0
1529 0 ▶ && SRL_ENC_HAVE_OPTION(enc, SRL_F_USE_NATIVE_LONG_DOUBLE)
Branch: 1 branch
BranchTrueFalseTotal
&& SRL_ENC_HAVE_OPTION(enc, SRL_F_USE_NATIVE_LONG_DOUBLE) 0 0 0
1530 0 ▶ && !srl_native_long_double_format()) {
Branch: 1 branch
BranchTrueFalseTotal
&& !srl_native_long_double_format()) { 0 0 0
1531 0 srl_dump_native_nv(aTHX_ enc, nv, SRL_HDR_LONG_DOUBLE);
1532 0 return;
1533 }
1534 0 f = (float)nv;
1535 0 d = (double)nv;
1536 #ifdef HAS_QUADMATH
1537 #define LONG_FLOAT_MIN_VER 5
1538 #else
1539 #define LONG_FLOAT_MIN_VER 4
1540 #endif
1541 /* Compact IEEE representations retain exact values without the cost of
1542 * extended storage. Wider values use portable binary128 unless native
1543 * output was selected or the requested legacy protocol predates it. */
1544 0 ▶ if ( f == nv || nv != nv ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( f == nv || nv != nv ) { 0 0 0
if ( f == nv || nv != nv ) { 0 0 0
1545 0 ▶ BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(f)); /* tag + payload */
Branch: 4 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(f)); /* tag + payload */ 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(f)); /* tag + payload */ 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(f)); /* tag + payload */ 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(f)); /* tag + payload */ 0 0 0
1546 0 srl_buf_cat_char_nocheck(&enc->buf, SRL_HDR_FLOAT);
1547 0 ▶ if (enc->protocol_version >= 7)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->protocol_version >= 7) 0 0 0
1548 0 srl_copy_float_little_endian(aTHX_ enc->buf.pos, (const U8 *)&f);
1549 else
1550 0 Copy((char *)&f, enc->buf.pos, sizeof(f), char);
1551 0 enc->buf.pos += sizeof(f);
1552 } else if (
1553 !HAS_LONG_FLOAT ||
1554 d == nv ||
1555 SRL_ENC_HAVE_OPTION(enc,SRL_F_USE_STANDARD_DOUBLE) ||
1556 (enc->protocol_version < LONG_FLOAT_MIN_VER)
1557 ) {
1558 0 ▶ BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(d)); /* tag + payload */
Branch: 4 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(d)); /* tag + payload */ 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(d)); /* tag + payload */ 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(d)); /* tag + payload */ 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 1 + sizeof(d)); /* tag + payload */ 0 0 0
1559 0 srl_buf_cat_char_nocheck(&enc->buf, SRL_HDR_DOUBLE);
1560 0 ▶ if (enc->protocol_version >= 7)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->protocol_version >= 7) 0 0 0
1561 0 srl_copy_double_little_endian(aTHX_ enc->buf.pos, (const U8 *)&d);
1562 else
1563 0 Copy((char *)&d, enc->buf.pos, sizeof(d), char);
1564 0 enc->buf.pos += sizeof(d);
1565 } else if (enc->protocol_version >= 7
1566 && (!SRL_ENC_HAVE_OPTION(enc, SRL_F_USE_NATIVE_LONG_DOUBLE) || enc->encoding_header)) {
1567 BUF_SIZE_ASSERT(&enc->buf, 17);
1568 srl_buf_cat_char_nocheck(&enc->buf, SRL_HDR_FLOAT_128);
1569 srl_nv_to_binary128(nv, enc->buf.pos);
1570 enc->buf.pos += 16;
1571 } else {
1572 assert(HAS_LONG_FLOAT);
1573 #ifdef HAS_QUADMATH
1574 srl_dump_native_nv(aTHX_ enc, nv, SRL_HDR_FLOAT_128);
1575 #else
1576 srl_dump_native_nv(aTHX_ enc, nv, SRL_HDR_LONG_DOUBLE);
1577 #endif
1578 }
1579 }
1580  
1581  
1582 /* Code for serializing any SINGLE integer type */
1583 SRL_STATIC_INLINE void
1584 48 srl_dump_ivuv(pTHX_ srl_encoder_t *enc, SV *src)
1585 {
1586 char hdr;
1587 /* TODO for the time being, we just won't ever use NUMLIST types because that's
1588 * a fair amount of extra implementation work. The decoders won't care and
1589 * we're just wasting some space. */
1590 /* TODO optimize! */
1591  
1592 /* Entry contract: CALL_SRL_DUMP_SV's fast path passes nonmagical scalars;
1593 * srl_dump_sv's general path runs srl_getmagic first when needed. If src
1594 * is still magical, sign and payload must use that cached value: another
1595 * FETCH could return something different. Use _nomg conversions here.
1596 * They suppress FETCH, but may still do numeric conversion. A private
1597 * integer cache is also valid after FETCH: older Perls may leave only
1598 * that cache set, and converting its UV through IV loses the sign. */
1599 48 ▶ if (expect_true( (SvIOKp(src) && SvIsUV(src))
Branch: 6 branches
BranchTrueFalseTotal
if (expect_true( (SvIOKp(src) && SvIsUV(src)) 48 0 48
if (expect_true( (SvIOKp(src) && SvIsUV(src)) 48 0 48
if (expect_true( (SvIOKp(src) && SvIsUV(src)) 48 0 48
if (expect_true( (SvIOKp(src) && SvIsUV(src)) 48 0 48
if (expect_true( (SvIOKp(src) && SvIsUV(src)) 0 0 0
if (expect_true( (SvIOKp(src) && SvIsUV(src)) 48 0 48
1600 || (SvIOKp(src) ? SvIVX(src) : SvIV_nomg(src)) >= 0 )) {
1601 /* A cached IV is already known nonnegative here. Read its
1602 * slot directly instead of asking Perl to convert a signed IV to UV.
1603 * Keep conversion for SVs without a cached integer. */
1604 96 const UV num = SvIOKp(src)
1605 48 ▶ ? (SvIsUV(src) ? SvUVX(src) : (UV)SvIVX(src))
Branch: 1 branch
BranchTrueFalseTotal
? (SvIsUV(src) ? SvUVX(src) : (UV)SvIVX(src)) 0 48 48
1606 96 ▶ : SvUV_nomg(src);
Branch: 1 branch
BranchTrueFalseTotal
: SvUV_nomg(src); 48 0 48
1607 48 ▶ if (num <= 15) {
Branch: 1 branch
BranchTrueFalseTotal
if (num <= 15) { 48 0 48
1608 /* encodable as POS */
1609 48 hdr = SRL_HDR_POS_LOW | (unsigned char)num;
1610 48 srl_buf_cat_char(&enc->buf, hdr);
1611 }
1612 else {
1613 0 srl_buf_cat_varint(aTHX_ &enc->buf, SRL_HDR_VARINT, num);
1614 }
1615 }
1616 else {
1617 0 ▶ const IV num = SvIOKp(src) ? SvIVX(src) : SvIV_nomg(src);
Branch: 1 branch
BranchTrueFalseTotal
const IV num = SvIOKp(src) ? SvIVX(src) : SvIV_nomg(src); 0 0 0
1618 0 ▶ if (num >= -16) {
Branch: 1 branch
BranchTrueFalseTotal
if (num >= -16) { 0 0 0
1619 /* encodable as NEG */
1620 0 hdr = SRL_HDR_NEG_LOW | ((unsigned char)num + 32);
1621 0 srl_buf_cat_char(&enc->buf, hdr);
1622 }
1623 else {
1624 0 ▶ if (enc->protocol_version >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc->protocol_version >= 6) { 0 0 0
1625 /* NEG_VARINT stores the absolute value as an unsigned varint. */
1626 0 const UV magnitude = (UV)(-(num + 1)) + 1;
1627 0 srl_buf_cat_varint(aTHX_ &enc->buf, SRL_HDR_NEG_VARINT, magnitude);
1628 } else {
1629 /* Protocol versions through 5 use ZIGZAG for large negatives. */
1630 0 srl_buf_cat_zigzag(aTHX_ &enc->buf, SRL_HDR_ZIGZAG, num);
1631 }
1632 }
1633 }
1634 48 }
1635  
1636 /* Check either class allow-list. Only freeze_allow_classes passes the object
1637 * as a second predicate argument; allowed_classes matches Decoder's API. */
1638 SRL_STATIC_INLINE int
1639 0 srl_class_allowed(pTHX_ srl_encoder_t *enc, HV *allow_hash, SV *allow_cb,
1640 HV *stash, SV *src, SV *referent, int with_object)
1641 {
1642 const char *classname;
1643 STRLEN classname_len;
1644 int is_utf8;
1645  
1646 0 ▶ if (!allow_hash && !allow_cb)
Branch: 2 branches
BranchTrueFalseTotal
if (!allow_hash && !allow_cb) 0 0 0
if (!allow_hash && !allow_cb) 0 0 0
1647 0 return 1; /* no allow-list configured: allow everything */
1648  
1649 0 ▶ classname = HvNAME_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
classname = HvNAME_get(stash); 0 0 0
classname = HvNAME_get(stash); 0 0 0
classname = HvNAME_get(stash); 0 0 0
classname = HvNAME_get(stash); 0 0 0
classname = HvNAME_get(stash); 0 0 0
classname = HvNAME_get(stash); 0 0 0
1650 0 ▶ classname_len = HvNAMELEN_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
classname_len = HvNAMELEN_get(stash); 0 0 0
classname_len = HvNAMELEN_get(stash); 0 0 0
classname_len = HvNAMELEN_get(stash); 0 0 0
classname_len = HvNAMELEN_get(stash); 0 0 0
classname_len = HvNAMELEN_get(stash); 0 0 0
classname_len = HvNAMELEN_get(stash); 0 0 0
1651 #if PERL_VERSION >= 16
1652 0 ▶ is_utf8 = HvNAMEUTF8(stash) ? 1 : 0;
Branch: 8 branches
BranchTrueFalseTotal
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
1653 #else
1654 is_utf8 = 0;
1655 #endif
1656  
1657 0 ▶ if (allow_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow_hash) { 0 0 0
1658 0 ▶ return hv_exists(allow_hash, classname,
Branch: 1 branch
BranchTrueFalseTotal
return hv_exists(allow_hash, classname, 0 0 0
1659 0 is_utf8 ? -(I32)classname_len : (I32)classname_len) ? 1 : 0;
1660 }
1661 else {
1662 /* allowed_classes receives the name; freeze_allow_classes also
1663 * receives the object, retaining its established API. */
1664 0 int allowed = 0;
1665 int count;
1666 SV *name_sv;
1667 0 dSP;
1668  
1669 0 ENTER;
1670 0 SAVETMPS;
1671 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1672 0 ▶ EXTEND(SP, with_object ? 2 : 1);
Branch: 3 branches
BranchTrueFalseTotal
EXTEND(SP, with_object ? 2 : 1); 0 0 0
EXTEND(SP, with_object ? 2 : 1); 0 0 0
EXTEND(SP, with_object ? 2 : 1); 0 0 0
1673 0 name_sv = sv_2mortal(newSVpvn(classname, classname_len));
1674 0 ▶ if (is_utf8) SvUTF8_on(name_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (is_utf8) SvUTF8_on(name_sv); 0 0 0
1675 0 PUSHs(name_sv);
1676 0 ▶ if (with_object)
Branch: 1 branch
BranchTrueFalseTotal
if (with_object) 0 0 0
1677 0 PUSHs(src);
1678 0 PUTBACK;
1679 0 srl_callback_begin(aTHX_ enc, src, referent);
1680 0 count = call_sv(allow_cb, G_SCALAR);
1681 0 SPAGAIN;
1682 0 ▶ if (count >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count >= 1) { 0 0 0
1683 0 SV *res = POPs;
1684 0 allowed = SvTRUE(res) ? 1 : 0;
1685 }
1686 /* Boolean overload may execute user code just like the predicate. */
1687 0 srl_callback_end(aTHX_ enc);
1688 0 PUTBACK;
1689 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1690 0 LEAVE;
1691 0 return allowed;
1692 }
1693 }
1694  
1695 /* Dumps the tag and class name of an object doing all necessary callbacks or
1696 * exception-throwing.
1697 * The provided SV must already have been identified as a Perl object
1698 * using sv_isobject().
1699 * If the return value is not NULL, then it's the actual object content that
1700 * needs to be serialized by the caller. */
1701 static SRL_NOINLINE SV *
1702 0 srl_get_frozen_object(pTHX_ srl_encoder_t *enc, SV *src, SV *referent)
1703 {
1704 assert(SvROK(src) && SvOBJECT(referent));
1705  
1706 /* Check for FREEZE support */
1707 0 ▶ if (expect_false( SRL_ENC_HAVE_OPTION(enc, SRL_F_ENABLE_FREEZE_SUPPORT) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( SRL_ENC_HAVE_OPTION(enc, SRL_F_ENABLE_FREEZE_SUPPORT) )) { 0 0 0
1708 0 HV *stash = SvSTASH(referent);
1709 0 GV *method = NULL;
1710 assert(stash != NULL);
1711 0 method = gv_fetchmethod_autoload(stash, "FREEZE", 0);
1712  
1713 0 ▶ if (expect_false( method != NULL )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( method != NULL )) { 0 0 0
1714 0 SV *replacement= NULL;
1715 PTABLE_t *freezeobj_svhash;
1716 0 CV *freeze_cv = GvCV(method);
1717 /* Weak RVs do not contribute to the referent's refcount. Capture
1718 * this before a predicate can clear the last ordinary owner or
1719 * overwrite src; our keepalive may then be its only reference. */
1720 0 ▶ const int may_have_frozen = SvREFCNT(referent) > 1 || SvWEAKREF(src);
Branch: 2 branches
BranchTrueFalseTotal
const int may_have_frozen = SvREFCNT(referent) > 1 || SvWEAKREF(src); 0 0 0
const int may_have_frozen = SvREFCNT(referent) > 1 || SvWEAKREF(src); 0 0 0
1721  
1722 /* The callback guard retains src and its original referent for
1723 * either user-code entry. Do not also retain them on the save
1724 * stack: that duplicates ownership and defeats the cache's
1725 * unshared-referent shortcut below. */
1726 /* The predicate may delete or replace the method's glob too. */
1727 0 SvREFCNT_inc_simple_void_NN((SV *)freeze_cv);
1728 0 SAVEFREESV((SV *)freeze_cv);
1729  
1730 /* Enforce the freeze allow-list (if any). A disallowed class either
1731 * croaks (default) or is serialized normally (freeze_deny_action
1732 * => 'serialize'), which we signal by returning NULL here. */
1733 0 ▶ if (expect_false( !srl_class_allowed(aTHX_ enc,
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( !srl_class_allowed(aTHX_ enc, 0 0 0
1734 enc->freeze_allow_hash, enc->freeze_allow_cb,
1735 stash, src, referent, 1) )) {
1736 0 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_FREEZE_DENY_SERIALIZE))
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_FREEZE_DENY_SERIALIZE)) 0 0 0
1737 0 return NULL;
1738 0 ▶ croak("FREEZE not allowed for class '%s' (not in freeze_allow_classes)",
Branch: 6 branches
BranchTrueFalseTotal
croak("FREEZE not allowed for class '%s' (not in freeze_allow_classes)", 0 0 0
croak("FREEZE not allowed for class '%s' (not in freeze_allow_classes)", 0 0 0
croak("FREEZE not allowed for class '%s' (not in freeze_allow_classes)", 0 0 0
croak("FREEZE not allowed for class '%s' (not in freeze_allow_classes)", 0 0 0
croak("FREEZE not allowed for class '%s' (not in freeze_allow_classes)", 0 0 0
croak("FREEZE not allowed for class '%s' (not in freeze_allow_classes)", 0 0 0
1739 HvNAME(stash));
1740 }
1741  
1742 0 ▶ freezeobj_svhash = SRL_GET_FREEZEOBJ_SVHASH(enc);
Branch: 1 branch
BranchTrueFalseTotal
freezeobj_svhash = SRL_GET_FREEZEOBJ_SVHASH(enc); 0 0 0
1743 0 ▶ if (may_have_frozen) {
Branch: 1 branch
BranchTrueFalseTotal
if (may_have_frozen) { 0 0 0
1744 0 replacement= (SV *) PTABLE_fetch(freezeobj_svhash, referent);
1745 }
1746 0 ▶ if (!replacement) {
Branch: 1 branch
BranchTrueFalseTotal
if (!replacement) { 0 0 0
1747 int count;
1748 0 dSP;
1749  
1750 0 ENTER;
1751 0 SAVETMPS;
1752 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1753  
1754 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
1755 0 PUSHs(src);
1756 0 PUSHs(enc->sereal_string_sv); /* not NULL if SRL_F_ENABLE_FREEZE_SUPPORT is set */
1757 0 replacement= (SV*)newAV();
1758 0 PTABLE_store(freezeobj_svhash, referent, replacement);
1759  
1760 0 PUTBACK;
1761 0 srl_callback_begin(aTHX_ enc, src, referent);
1762 0 count = call_sv((SV *)freeze_cv, G_ARRAY);
1763 0 srl_callback_end(aTHX_ enc);
1764 /* TODO explore method lookup caching */
1765 0 SPAGAIN;
1766  
1767 0 ▶ while ( count-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ( count-- > 0) { 0 0 0
1768 0 SV *tmp = POPs;
1769 0 SvREFCNT_inc(tmp);
1770 0 ▶ if (!av_store((AV*)replacement,count,tmp))
Branch: 1 branch
BranchTrueFalseTotal
if (!av_store((AV*)replacement,count,tmp)) 0 0 0
1771 0 croak("Failed to push value into array");
1772 }
1773  
1774 0 PUTBACK;
1775 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1776 0 LEAVE;
1777 }
1778 0 return replacement;
1779 }
1780 }
1781 0 return NULL;
1782  
1783 }
1784  
1785 /* Count serialized object tags, including OBJECTV and frozen forms. REFP
1786 * references have no class tag and are not counted. */
1787 SRL_STATIC_INLINE void
1788 0 srl_track_class(pTHX_ srl_encoder_t *enc, const HV *stash)
1789 {
1790 const char *name;
1791 STRLEN len;
1792 int is_utf8;
1793 SV **count;
1794 0 ▶ if (!enc->track_classes)
Branch: 1 branch
BranchTrueFalseTotal
if (!enc->track_classes) 0 0 0
1795 0 return;
1796 0 ▶ name = HvNAME_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 0 0 0
1797 0 ▶ len = HvNAMELEN_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
len = HvNAMELEN_get(stash); 0 0 0
len = HvNAMELEN_get(stash); 0 0 0
len = HvNAMELEN_get(stash); 0 0 0
len = HvNAMELEN_get(stash); 0 0 0
len = HvNAMELEN_get(stash); 0 0 0
len = HvNAMELEN_get(stash); 0 0 0
1798 #if PERL_VERSION >= 16
1799 0 ▶ is_utf8 = HvNAMEUTF8(stash) ? 1 : 0;
Branch: 8 branches
BranchTrueFalseTotal
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
1800 #else
1801 is_utf8 = 0;
1802 #endif
1803 0 ▶ count = hv_fetch(enc->track_classes, name,
Branch: 1 branch
BranchTrueFalseTotal
count = hv_fetch(enc->track_classes, name, 0 0 0
1804 is_utf8 ? -(I32)len : (I32)len, 1);
1805 0 ▶ if (!count)
Branch: 1 branch
BranchTrueFalseTotal
if (!count) 0 0 0
1806 0 croak("Could not update track_classes");
1807 0 sv_inc(*count);
1808 }
1809  
1810 /* Outputs a bless header and the class name (as some form of string or COPY).
1811 * Caller then has to output the actual reference payload.
1812 * If it returns 1 it means the classname was written out and should NOT
1813 * be overwritten by the ref rewrite logic (which handles REFP).
1814 * If it returns 0 it means no classname was output. */
1815 static SRL_NOINLINE int
1816 0 srl_dump_classname(pTHX_ srl_encoder_t *enc, SV *referent, SV *replacement)
1817 {
1818 /* Check that we actually want to support objects */
1819 0 ▶ if (expect_false( SRL_ENC_HAVE_OPTION(enc, SRL_F_CROAK_ON_BLESS)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( SRL_ENC_HAVE_OPTION(enc, SRL_F_CROAK_ON_BLESS)) ) { 0 0 0
1820 0 croak("Attempted to serialize blessed reference. Serializing objects "
1821 "using Sereal::Encoder was explicitly disabled using the "
1822 "'croak_on_bless' option.");
1823 0 ▶ } else if (expect_false( SRL_ENC_HAVE_OPTION(enc, SRL_F_NO_BLESS_OBJECTS) )) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (expect_false( SRL_ENC_HAVE_OPTION(enc, SRL_F_NO_BLESS_OBJECTS) )) { 0 0 0
1824 0 return 0;
1825 } else {
1826 0 const HV *stash = SvSTASH(referent);
1827 0 ▶ PTABLE_t *string_seenhash = SRL_GET_STR_PTR_SEENHASH(enc);
Branch: 1 branch
BranchTrueFalseTotal
PTABLE_t *string_seenhash = SRL_GET_STR_PTR_SEENHASH(enc); 0 0 0
1828 0 svtype svt= SvTYPE(referent);
1829 0 ▶ int is_av_or_hv= (svt == SVt_PVAV || svt== SVt_PVHV);
Branch: 2 branches
BranchTrueFalseTotal
int is_av_or_hv= (svt == SVt_PVAV || svt== SVt_PVHV); 0 0 0
int is_av_or_hv= (svt == SVt_PVAV || svt== SVt_PVHV); 0 0 0
1830 0 ptrdiff_t oldoffset= is_av_or_hv
1831 ? 0
1832 0 ▶ : (ptrdiff_t)PTABLE_fetch(string_seenhash, SRL_CLASS_ID(referent));
Branch: 1 branch
BranchTrueFalseTotal
: (ptrdiff_t)PTABLE_fetch(string_seenhash, SRL_CLASS_ID(referent)); 0 0 0
1833  
1834 0 ▶ if (oldoffset) {
Branch: 1 branch
BranchTrueFalseTotal
if (oldoffset) { 0 0 0
1835 0 return 0;
1836 } else {
1837 0 ▶ svt= replacement ? SvTYPE(replacement) : SvTYPE(referent);
Branch: 1 branch
BranchTrueFalseTotal
svt= replacement ? SvTYPE(replacement) : SvTYPE(referent); 0 0 0
1838 0 ▶ if (SRL_UNSUPPORTED_SvTYPE(svt)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SRL_UNSUPPORTED_SvTYPE(svt)) { 0 0 0
if (SRL_UNSUPPORTED_SvTYPE(svt)) { 0 0 0
if (SRL_UNSUPPORTED_SvTYPE(svt)) { 0 0 0
if (SRL_UNSUPPORTED_SvTYPE(svt)) { 0 0 0
1839 0 return 0;
1840 }
1841 0 oldoffset= (ptrdiff_t)PTABLE_fetch(string_seenhash, SRL_CLASS_ID(stash));
1842 }
1843  
1844 0 ▶ if (oldoffset != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (oldoffset != 0) { 0 0 0
1845 /* Issue COPY instead of literal class name string */
1846 0 srl_buf_cat_varint(aTHX_ &enc->buf,
1847 0 ▶ expect_false(replacement) ? SRL_HDR_OBJECTV_FREEZE : SRL_HDR_OBJECTV,
Branch: 1 branch
BranchTrueFalseTotal
expect_false(replacement) ? SRL_HDR_OBJECTV_FREEZE : SRL_HDR_OBJECTV, 0 0 0
1848 (UV)oldoffset);
1849 }
1850 else {
1851 0 ▶ const char *class_name = HvNAME_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
const char *class_name = HvNAME_get(stash); 0 0 0
const char *class_name = HvNAME_get(stash); 0 0 0
const char *class_name = HvNAME_get(stash); 0 0 0
const char *class_name = HvNAME_get(stash); 0 0 0
const char *class_name = HvNAME_get(stash); 0 0 0
const char *class_name = HvNAME_get(stash); 0 0 0
1852 0 ▶ const size_t len = HvNAMELEN_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
const size_t len = HvNAMELEN_get(stash); 0 0 0
const size_t len = HvNAMELEN_get(stash); 0 0 0
const size_t len = HvNAMELEN_get(stash); 0 0 0
const size_t len = HvNAMELEN_get(stash); 0 0 0
const size_t len = HvNAMELEN_get(stash); 0 0 0
const size_t len = HvNAMELEN_get(stash); 0 0 0
1853  
1854 /* First save this new string (well, the HV * that it is represented by) into the string
1855 * dedupe table.
1856 * By saving the ptr to the HV, we only dedupe class names with class names, though
1857 * this seems a small price to pay for not having to keep a full string table.
1858 * At least, we can safely use the same PTABLE to store the ptrs to hashkeys since
1859 * the set of pointers will never collide.
1860 * /me bows to Yves for the delightfully evil hack. */
1861 0 ▶ srl_buf_cat_char(&enc->buf, expect_false(replacement) ? SRL_HDR_OBJECT_FREEZE : SRL_HDR_OBJECT);
Branch: 1 branch
BranchTrueFalseTotal
srl_buf_cat_char(&enc->buf, expect_false(replacement) ? SRL_HDR_OBJECT_FREEZE : SRL_HDR_OBJECT); 0 0 0
1862  
1863 /* remember current offset before advancing it */
1864 0 PTABLE_store(string_seenhash, SRL_CLASS_ID(stash), INT2PTR(void *, BODY_POS_OFS(&enc->buf)));
1865 0 ▶ if (SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS))
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS)) 0 0 0
1866 0 srl_callback_hold(aTHX_ enc, (SV *)stash);
1867  
1868 /* HvNAMEUTF8 not in older perls and it would be 0 for those anyway */
1869 #if PERL_VERSION >= 16
1870 0 ▶ srl_dump_pv(aTHX_ enc, class_name, len, HvNAMEUTF8(stash));
Branch: 7 branches
BranchTrueFalseTotal
srl_dump_pv(aTHX_ enc, class_name, len, HvNAMEUTF8(stash)); 0 0 0
srl_dump_pv(aTHX_ enc, class_name, len, HvNAMEUTF8(stash)); 0 0 0
srl_dump_pv(aTHX_ enc, class_name, len, HvNAMEUTF8(stash)); 0 0 0
srl_dump_pv(aTHX_ enc, class_name, len, HvNAMEUTF8(stash)); 0 0 0
srl_dump_pv(aTHX_ enc, class_name, len, HvNAMEUTF8(stash)); 0 0 0
srl_dump_pv(aTHX_ enc, class_name, len, HvNAMEUTF8(stash)); 0 0 0
srl_dump_pv(aTHX_ enc, class_name, len, HvNAMEUTF8(stash)); 0 0 0
1871 #else
1872 srl_dump_pv(aTHX_ enc, class_name, len, 0);
1873 #endif
1874 }
1875 /* A repeated aggregate may have had this header written only to be
1876 * replaced with REFP when its referent is reached below. */
1877 0 ▶ if (!enc->ref_seenhash || !PTABLE_fetch(enc->ref_seenhash, referent))
Branch: 2 branches
BranchTrueFalseTotal
if (!enc->ref_seenhash || !PTABLE_fetch(enc->ref_seenhash, referent)) 0 0 0
if (!enc->ref_seenhash || !PTABLE_fetch(enc->ref_seenhash, referent)) 0 0 0
1878 0 srl_track_class(aTHX_ enc, stash);
1879 0 ▶ if (is_av_or_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_av_or_hv) { 0 0 0
1880 0 return 0;
1881 } else {
1882 /* use the string_seenhash to track which items we have seen before */
1883 0 PTABLE_store(string_seenhash, SRL_CLASS_ID(referent), INT2PTR(void *, BODY_POS_OFS(&enc->buf)));
1884 0 ▶ if (SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS))
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS)) 0 0 0
1885 0 srl_callback_hold(aTHX_ enc, referent);
1886 0 return 1;
1887 }
1888 }
1889 return 0;
1890 }
1891  
1892  
1893 /* Prepare encoder for encoding: Clone if already in use since
1894 * encoders aren't "reentrant". Set as in use and register cleanup
1895 * routine with Perl. */
1896 SRL_STATIC_INLINE srl_encoder_t *
1897 7 srl_prepare_encoder(pTHX_ srl_encoder_t *enc)
1898 {
1899 /* Check whether encoder is in use and create a new one on the
1900 * fly if necessary. Should only happen in edge cases such as
1901 * FREEZE hooks that serialize things using the same encoder
1902 * object. */
1903 7 ▶ if (SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_DIRTY)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_DIRTY)) { 0 7 7
1904 0 srl_encoder_t * const proto = enc;
1905 0 enc = srl_build_encoder_struct_alike(aTHX_ proto);
1906 0 SRL_ENC_RESET_OPTION(enc, SRL_F_REUSE_ENCODER);
1907 }
1908 /* Set to being in use */;
1909 7 SRL_ENC_SET_OPER_FLAG(enc, SRL_OF_ENCODER_DIRTY);
1910  
1911 /* Register our structure for destruction on scope exit */
1912 7 SAVEDESTRUCTOR_X(&srl_destructor_hook, (void *)enc);
1913  
1914 7 ▶ if (expect_false(enc->buf.start == NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(enc->buf.start == NULL)) { 0 7 7
1915 0 ▶ if (expect_false(srl_buf_init_buffer(aTHX_ &enc->buf, INITIALIZATION_SIZE) != 0))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(srl_buf_init_buffer(aTHX_ &enc->buf, INITIALIZATION_SIZE) != 0)) 0 0 0
1916 0 croak("Out of memory");
1917 }
1918  
1919 7 return enc;
1920 }
1921  
1922 /* Selecting native output establishes a layout for the whole document.
1923 * Emit its declaration before traversing values, even if that traversal
1924 * eventually finds no extended value. A preliminary scan would add a second
1925 * walk and could invoke magic or callbacks before normal serialization.
1926 * Callers place this prefix inside the body so compression and checksums
1927 * cover the declaration together with the values that depend on it. */
1928 SRL_STATIC_INLINE void
1929 7 srl_write_declarations(pTHX_ srl_encoder_t *enc)
1930 {
1931 7 ▶ if (enc->protocol_version >= 7
Branch: 1 branch
BranchTrueFalseTotal
if (enc->protocol_version >= 7 1 6 7
1932 1 ▶ && SRL_ENC_HAVE_OPTION(enc, SRL_F_USE_NATIVE_LONG_DOUBLE)) {
Branch: 1 branch
BranchTrueFalseTotal
&& SRL_ENC_HAVE_OPTION(enc, SRL_F_USE_NATIVE_LONG_DOUBLE)) { 0 1 1
1933 0 ▶ BUF_SIZE_ASSERT(&enc->buf, 6);
Branch: 4 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(&enc->buf, 6); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 6); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 6); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 6); 0 0 0
1934 0 srl_buf_cat_char_nocheck(&enc->buf, SRL_HDR_EXTEND);
1935 0 srl_buf_cat_char_nocheck(&enc->buf, SRL_EXT_DECLARE);
1936 0 srl_buf_cat_char_nocheck(&enc->buf, 3); /* declaration payload length */
1937 0 srl_buf_cat_char_nocheck(&enc->buf, SRL_DECL_LONG_DOUBLE);
1938 0 srl_buf_cat_char_nocheck(&enc->buf, srl_native_long_double_declaration_format());
1939 0 srl_buf_cat_char_nocheck(&enc->buf, sizeof(long double));
1940 }
1941 7 }
1942  
1943 SRL_STATIC_INLINE srl_encoder_t *
1944 7 srl_dump_data_structure(pTHX_ srl_encoder_t *enc, SV *src, SV *user_header_src)
1945 {
1946 U32 compress_flags;
1947  
1948 7 enc = srl_prepare_encoder(aTHX_ enc);
1949 7 enc->encoding_header = 0;
1950 7 enc->root_src = src;
1951 7 enc->header_src = user_header_src;
1952 7 ▶ if (srl_checksum_required_mode(enc->checksum_policy) && !enc->checksum_key_set)
Branch: 2 branches
BranchTrueFalseTotal
if (srl_checksum_required_mode(enc->checksum_policy) && !enc->checksum_key_set) 0 7 7
if (srl_checksum_required_mode(enc->checksum_policy) && !enc->checksum_key_set) 0 0 0
1953 0 croak("checksum_policy '%s' requires a checksum key to encode",
1954 srl_checksum_policy_name(enc->checksum_policy));
1955 7 compress_flags= SRL_ENC_HAVE_OPTION(enc, SRL_F_COMPRESS_FLAGS_MASK);
1956  
1957 7 ▶ if (expect_false(compress_flags))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(compress_flags)) 0 7 7
1958 { /* Have some sort of compression */
1959 ptrdiff_t sereal_header_len;
1960 STRLEN uncompressed_body_length;
1961  
1962 /* Alas, have to write entire packet first since the header length
1963 * will determine offsets. */
1964 0 srl_write_header(aTHX_ enc, user_header_src, compress_flags);
1965 0 sereal_header_len = BUF_POS_OFS(&enc->buf);
1966 0 ▶ SRL_ENC_UPDATE_BODY_POS(enc);
Branch: 1 branch
BranchTrueFalseTotal
SRL_ENC_UPDATE_BODY_POS(enc); 0 0 0
1967 0 srl_write_declarations(aTHX_ enc);
1968 0 srl_dump_sv(aTHX_ enc, src);
1969 0 srl_fixup_weakrefs(aTHX_ enc);
1970 assert(BUF_POS_OFS(&enc->buf) > sereal_header_len);
1971 0 uncompressed_body_length = BUF_POS_OFS(&enc->buf) - sereal_header_len;
1972  
1973 0 ▶ if (uncompressed_body_length < (STRLEN)enc->compress_threshold) {
Branch: 1 branch
BranchTrueFalseTotal
if (uncompressed_body_length < (STRLEN)enc->compress_threshold) { 0 0 0
1974 /* Don't bother with compression at all if we have less than $threshold bytes of payload */
1975 0 srl_reset_compression_header_flag(&enc->buf);
1976 }
1977 else { /* Do Snappy, zlib, or zstd compression of body */
1978 0 srl_compress_body_ctx(aTHX_ &enc->buf, sereal_header_len,
1979 0 compress_flags, enc->compress_level,
1980 &enc->snappy_workmem,
1981 0 ▶ SRL_ENC_HAVE_OPTION(enc, SRL_F_REUSE_ENCODER) ? &enc->zstd_cctx : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SRL_ENC_HAVE_OPTION(enc, SRL_F_REUSE_ENCODER) ? &enc->zstd_cctx : NULL); 0 0 0
1982  
1983 0 ▶ SRL_ENC_UPDATE_BODY_POS(enc);
Branch: 1 branch
BranchTrueFalseTotal
SRL_ENC_UPDATE_BODY_POS(enc); 0 0 0
1984 DEBUG_ASSERT_BUF_SANE(&enc->buf);
1985 }
1986 } /* End of "want compression?" */
1987 else
1988 {
1989 7 srl_write_header(aTHX_ enc, user_header_src, compress_flags);
1990 7 ▶ SRL_ENC_UPDATE_BODY_POS(enc);
Branch: 1 branch
BranchTrueFalseTotal
SRL_ENC_UPDATE_BODY_POS(enc); 1 6 7
1991 7 srl_write_declarations(aTHX_ enc);
1992 7 srl_dump_sv(aTHX_ enc, src);
1993 7 srl_fixup_weakrefs(aTHX_ enc);
1994 }
1995  
1996 7 ▶ if (enc->checksum_key_set)
Branch: 1 branch
BranchTrueFalseTotal
if (enc->checksum_key_set) 0 7 7
1997 0 srl_protect_packet(aTHX_ enc);
1998  
1999 /* NOT doing a
2000 * SRL_ENC_RESET_OPER_FLAG(enc, SRL_OF_ENCODER_DIRTY);
2001 * here because we're relying on the SAVEDESTRUCTOR_X call. */
2002 7 return enc;
2003 }
2004  
2005 SV *
2006 7 srl_dump_data_structure_mortal_sv(pTHX_ srl_encoder_t *enc, SV *src, SV *user_header_src, const U32 flags)
2007 {
2008 assert(enc);
2009 7 enc = srl_dump_data_structure(aTHX_ enc, src, user_header_src);
2010 assert(enc->buf.start && enc->buf.pos && enc->buf.pos > enc->buf.start);
2011  
2012 7 ▶ if ( (flags || !SRL_ENC_HAVE_OPTION(enc, SRL_F_REUSE_ENCODER)
Branch: 2 branches
BranchTrueFalseTotal
if ( (flags || !SRL_ENC_HAVE_OPTION(enc, SRL_F_REUSE_ENCODER) 7 0 7
if ( (flags || !SRL_ENC_HAVE_OPTION(enc, SRL_F_REUSE_ENCODER) 7 0 7
2013 7 ▶ || (size_t)BUF_SIZE(&enc->buf) > (size_t)SRL_ENC_BUF_REUSE_MAX_SIZE) &&
Branch: 1 branch
BranchTrueFalseTotal
|| (size_t)BUF_SIZE(&enc->buf) > (size_t)SRL_ENC_BUF_REUSE_MAX_SIZE) && 0 7 7
2014 0 ▶ (BUF_POS_OFS(&enc->buf) > 20 && BUF_SPACE(&enc->buf) < BUF_POS_OFS(&enc->buf)) )
Branch: 2 branches
BranchTrueFalseTotal
(BUF_POS_OFS(&enc->buf) > 20 && BUF_SPACE(&enc->buf) < BUF_POS_OFS(&enc->buf)) ) 0 0 0
(BUF_POS_OFS(&enc->buf) > 20 && BUF_SPACE(&enc->buf) < BUF_POS_OFS(&enc->buf)) ) 0 0 0
2015 {
2016 /* Steal buffer if not wasting more than 2x memory (functional calls,
2017 * one-off clones, or reusable encoders whose buffer exceeded the reuse cap). */
2018 SV *sv;
2019 0 ▶ if (expect_false(BUF_SPACE(&enc->buf) < 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(BUF_SPACE(&enc->buf) < 1)) { 0 0 0
2020 0 const size_t pos_ofs_ = (size_t)BUF_POS_OFS(&enc->buf);
2021 0 ▶ if (expect_false(pos_ofs_ >= SRL_BUF_MAX_SIZE))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(pos_ofs_ >= SRL_BUF_MAX_SIZE)) 0 0 0
2022 0 croak("Sereal output buffer is too large");
2023 0 Renew(enc->buf.start, pos_ofs_ + 1, srl_buffer_char);
2024 0 ▶ if (expect_false(enc->buf.start == NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(enc->buf.start == NULL)) 0 0 0
2025 0 croak("Out of memory!");
2026 0 enc->buf.pos = enc->buf.start + pos_ofs_;
2027 0 enc->buf.end = enc->buf.start + pos_ofs_ + 1;
2028 }
2029 0 *enc->buf.pos = '\0'; /* terminate the PV without adding to the packet */
2030 0 sv = sv_2mortal(newSV_type(SVt_PV));
2031 0 SvPV_set(sv, (char *) enc->buf.start);
2032 0 SvLEN_set(sv, BUF_SIZE(&enc->buf));
2033 0 SvCUR_set(sv, BUF_POS_OFS(&enc->buf));
2034 0 SvPOK_on(sv);
2035 0 enc->buf.start = enc->buf.pos = enc->buf.end = enc->buf.body_pos = NULL;
2036 0 return sv;
2037 }
2038  
2039 7 return sv_2mortal(newSVpvn((char *)enc->buf.start, (STRLEN)BUF_POS_OFS(&enc->buf)));
2040 }
2041  
2042 SRL_STATIC_INLINE void
2043 7 srl_fixup_weakrefs(pTHX_ srl_encoder_t *enc)
2044 {
2045 7 PTABLE_t *weak_seenhash = SRL_GET_WEAK_SEENHASH_OR_NULL(enc);
2046 7 ▶ if (!weak_seenhash)
Branch: 1 branch
BranchTrueFalseTotal
if (!weak_seenhash) 7 0 7
2047 7 return;
2048  
2049 {
2050 0 PTABLE_ITER_t *it = PTABLE_iter_new(weak_seenhash);
2051 PTABLE_ENTRY_t *ent;
2052  
2053 /* we now walk the weak_seenhash and set any tags it points
2054 * at to the PAD opcode, this basically turns the first weakref
2055 * we encountered into a normal ref when there is only a weakref
2056 * pointing at the structure. */
2057 0 ▶ while ( NULL != (ent = PTABLE_iter_next(it)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( NULL != (ent = PTABLE_iter_next(it)) ) { 0 0 0
2058 0 const ptrdiff_t offset = (ptrdiff_t)ent->value;
2059 0 ▶ if ( offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( offset ) { 0 0 0
2060 0 srl_buffer_char *pos = enc->buf.body_pos + offset;
2061 assert((*pos & ~SRL_HDR_TRACK_FLAG) == SRL_HDR_WEAKEN);
2062 if (DEBUGHACK) warn("setting byte at offset %"UVuf" to PAD", (UV)offset);
2063 0 *pos = (srl_buffer_char)((*pos & SRL_HDR_TRACK_FLAG) | SRL_HDR_PAD);
2064 }
2065 }
2066  
2067 0 PTABLE_iter_free(it);
2068 }
2069 }
2070  
2071  
2072  
2073 static SRL_NOINLINE void
2074 0 srl_dump_regexp(pTHX_ srl_encoder_t *enc, SV *sv)
2075 {
2076 0 STRLEN left = 0;
2077 const char *fptr;
2078 char ch;
2079 U16 match_flags;
2080 #ifdef MODERN_REGEXP
2081 0 REGEXP *re= SvRX(sv);
2082 #else
2083 regexp *re = (regexp *)(((MAGIC*)sv)->mg_obj);
2084 #endif
2085  
2086 char reflags[sizeof(INT_PAT_MODS) + MAX_CHARSET_NAME_LENGTH];
2087  
2088 /*
2089 we are in list context so stringify
2090 the modifiers that apply. We ignore "negative
2091 modifiers" in this scenario, and the default character set
2092 */
2093  
2094 #if PERL_VERSION >= 14 || (PERL_VERSION == 13 && PERL_SUBVERSION >= 10)
2095 /* REGEX_DEPENDS_CHARSET is an enum, not a preprocessor feature macro.
2096 * Charset modifiers first appeared in 5.13.10 (stable 5.14). Omit /d:
2097 * the decoder compiles with zero flags, independently of caller hints. */
2098 0 ▶ if (get_regex_charset(RX_EXTFLAGS(re)) != REGEX_DEPENDS_CHARSET) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_regex_charset(RX_EXTFLAGS(re)) != REGEX_DEPENDS_CHARSET) { 0 0 0
2099 0 switch (get_regex_charset(RX_EXTFLAGS(re))) {
2100 0 case REGEX_LOCALE_CHARSET: reflags[left++] = 'l'; break;
2101 0 case REGEX_UNICODE_CHARSET: reflags[left++] = 'u'; break;
2102 0 case REGEX_ASCII_RESTRICTED_CHARSET: reflags[left++] = 'a'; break;
2103 0 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
2104 0 reflags[left++] = 'a'; reflags[left++] = 'a'; break;
2105 0 default: croak("Cannot serialize regexp with unknown character set");
2106 }
2107 }
2108 #endif
2109 0 fptr = INT_PAT_MODS;
2110 0 match_flags = (U16)((RX_EXTFLAGS(re) & RXf_PMf_COMPILETIME)
2111 >> RXf_PMf_STD_PMMOD_SHIFT);
2112  
2113 0 ▶ while((ch = *fptr++)) {
Branch: 1 branch
BranchTrueFalseTotal
while((ch = *fptr++)) { 0 0 0
2114 0 ▶ if(match_flags & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(match_flags & 1) { 0 0 0
2115 0 reflags[left++] = ch;
2116 }
2117 0 match_flags >>= 1;
2118 }
2119  
2120 0 srl_buf_cat_char(&enc->buf, SRL_HDR_REGEXP);
2121 0 srl_dump_pv(aTHX_ enc, RX_PRECOMP(re),RX_PRELEN(re), (RX_UTF8(re) ? SVf_UTF8 : 0));
2122 0 srl_dump_pv(aTHX_ enc, reflags, left, 0);
2123 0 return;
2124 }
2125  
2126 /* qr// has been an SVt_REGEXP on newer Perls and a magical scalar on
2127 * older ones. Require the real referent and the canonical class. */
2128 SRL_STATIC_INLINE int
2129 0 srl_is_canonical_regexp_object(pTHX_ SV *referent)
2130 {
2131 0 HV *stash = SvSTASH(referent);
2132 0 ▶ const char *class_name = stash ? HvNAME_get(stash) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
const char *class_name = stash ? HvNAME_get(stash) : NULL; 0 0 0
const char *class_name = stash ? HvNAME_get(stash) : NULL; 0 0 0
const char *class_name = stash ? HvNAME_get(stash) : NULL; 0 0 0
const char *class_name = stash ? HvNAME_get(stash) : NULL; 0 0 0
const char *class_name = stash ? HvNAME_get(stash) : NULL; 0 0 0
const char *class_name = stash ? HvNAME_get(stash) : NULL; 0 0 0
const char *class_name = stash ? HvNAME_get(stash) : NULL; 0 0 0
2133 0 ▶ if (!class_name || HvNAMELEN_get(stash) != 6 ||
Branch: 8 branches
BranchTrueFalseTotal
if (!class_name || HvNAMELEN_get(stash) != 6 || 0 0 0
if (!class_name || HvNAMELEN_get(stash) != 6 || 0 0 0
if (!class_name || HvNAMELEN_get(stash) != 6 || 0 0 0
if (!class_name || HvNAMELEN_get(stash) != 6 || 0 0 0
if (!class_name || HvNAMELEN_get(stash) != 6 || 0 0 0
if (!class_name || HvNAMELEN_get(stash) != 6 || 0 0 0
if (!class_name || HvNAMELEN_get(stash) != 6 || 0 0 0
if (!class_name || HvNAMELEN_get(stash) != 6 || 0 0 0
2134 0 ▶ !memEQ(class_name, "Regexp", 6))
Branch: 1 branch
BranchTrueFalseTotal
!memEQ(class_name, "Regexp", 6)) 0 0 0
2135 0 return 0;
2136 #ifdef MODERN_REGEXP
2137 0 return SvTYPE(referent) == SVt_REGEXP;
2138 #else
2139 return SvTYPE(referent) == SVt_PVMG &&
2140 mg_find(referent, PERL_MAGIC_qr) != NULL;
2141 #endif
2142 }
2143  
2144 #define ASSUME_BYTES_PER_TAG 1
2145 #define BUF_SIZE_ASSERT_CONTAINER(b, n, per_elem) \
2146 STMT_START { \
2147 const UV n_ = (UV)(n); \
2148 const size_t overhead_ = (size_t)(2 + SRL_MAX_VARINT_LENGTH); \
2149 const size_t max_n_ = (SRL_BUF_MAX_SIZE - overhead_) / (size_t)(per_elem); \
2150 if (expect_false(n_ > max_n_)) \
2151 croak("Sereal output buffer is too large"); \
2152 BUF_SIZE_ASSERT((b), overhead_ + (size_t)(per_elem) * (size_t)n_); \
2153 } STMT_END
2154 #define BUF_SIZE_ASSERT_AV(b, n) BUF_SIZE_ASSERT_CONTAINER((b), (n), 1 * ASSUME_BYTES_PER_TAG)
2155 #define BUF_SIZE_ASSERT_HV(b, n) BUF_SIZE_ASSERT_CONTAINER((b), (n), 2 * ASSUME_BYTES_PER_TAG)
2156  
2157 static SRL_NOINLINE void
2158 20 srl_dump_av(pTHX_ srl_encoder_t *enc, AV *src, U32 refcount)
2159 {
2160 UV n;
2161 SV **svp;
2162 srl_encoder_frame frame;
2163 33 ▶ int magic = SvRMAGICAL(src) && (mg_find((SV *)src, PERL_MAGIC_tied)
Branch: 2 branches
BranchTrueFalseTotal
int magic = SvRMAGICAL(src) && (mg_find((SV *)src, PERL_MAGIC_tied) 13 7 20
int magic = SvRMAGICAL(src) && (mg_find((SV *)src, PERL_MAGIC_tied) 13 0 13
2164 13 ▶ || mg_find((SV *)src, PERL_MAGIC_regdata));
Branch: 1 branch
BranchTrueFalseTotal
|| mg_find((SV *)src, PERL_MAGIC_regdata)); 13 0 13
2165  
2166 20 srl_frame_push(enc, &frame, (SV *)src);
2167 20 ▶ if (magic)
Branch: 1 branch
BranchTrueFalseTotal
if (magic) 13 7 20
2168 13 srl_callback_begin(aTHX_ enc, NULL, NULL);
2169 20 n = av_len(src)+1;
2170 20 ▶ if (magic)
Branch: 1 branch
BranchTrueFalseTotal
if (magic) 13 7 20
2171 13 srl_callback_end(aTHX_ enc);
2172  
2173 /* heuristic: n is virtually the min. size of any element */
2174 20 ▶ BUF_SIZE_ASSERT_AV(&enc->buf, n);
Branch: 5 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT_AV(&enc->buf, n); 0 20 20
BUF_SIZE_ASSERT_AV(&enc->buf, n); 20 0 20
BUF_SIZE_ASSERT_AV(&enc->buf, n); 0 20 20
BUF_SIZE_ASSERT_AV(&enc->buf, n); 0 20 20
BUF_SIZE_ASSERT_AV(&enc->buf, n); 0 0 0
2175  
2176 20 ▶ if (n < 16 && refcount == 1 && !SRL_ENC_HAVE_OPTION(enc,SRL_F_CANONICAL_REFS)) {
Branch: 3 branches
BranchTrueFalseTotal
if (n < 16 && refcount == 1 && !SRL_ENC_HAVE_OPTION(enc,SRL_F_CANONICAL_REFS)) { 20 0 20
if (n < 16 && refcount == 1 && !SRL_ENC_HAVE_OPTION(enc,SRL_F_CANONICAL_REFS)) { 7 13 20
if (n < 16 && refcount == 1 && !SRL_ENC_HAVE_OPTION(enc,SRL_F_CANONICAL_REFS)) { 7 0 7
2177 7 enc->buf.pos--; /* backup over previous REFN */
2178 7 srl_buf_cat_char_nocheck(&enc->buf, SRL_HDR_ARRAYREF + n);
2179 } else {
2180 /* header and num. elements */
2181 13 srl_buf_cat_varint_nocheck(aTHX_ &enc->buf, SRL_HDR_ARRAY, n);
2182 }
2183 20 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 20 0 20
2184 UV i;
2185 20 ▶ if (magic) {
Branch: 1 branch
BranchTrueFalseTotal
if (magic) { 13 7 20
2186 64 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 51 13 64
2187 SV *value;
2188 51 srl_callback_begin(aTHX_ enc, NULL, NULL);
2189 51 svp = av_fetch(src, i, 0);
2190 51 srl_callback_end(aTHX_ enc);
2191 51 ▶ value = svp ? *svp : NULL;
Branch: 1 branch
BranchTrueFalseTotal
value = svp ? *svp : NULL; 51 0 51
2192 51 ▶ CALL_SRL_DUMP_SV(enc, value);
Branch: 51 branches
BranchTrueFalseTotal
CALL_SRL_DUMP_SV(enc, value); 0 51 51
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 51 51
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
2193 }
2194 } else {
2195 7 svp = AvARRAY(src);
2196 20 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 13 7 20
2197 13 ▶ CALL_SRL_DUMP_SV(enc, svp[i]);
Branch: 51 branches
BranchTrueFalseTotal
CALL_SRL_DUMP_SV(enc, svp[i]); 0 13 13
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 13 0 13
CALL_SRL_DUMP_SV(enc, svp[i]); 13 0 13
CALL_SRL_DUMP_SV(enc, svp[i]); 0 13 13
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
CALL_SRL_DUMP_SV(enc, svp[i]); 0 0 0
2198 }
2199 }
2200 }
2201 20 enc->frames = frame.parent;
2202 20 }
2203  
2204 /* Magical container APIs may enter Perl before returning their borrowed SV. */
2205 SRL_STATIC_INLINE void
2206 0 srl_hv_iterinit(pTHX_ srl_encoder_t *enc, HV *hash)
2207 {
2208 0 int magic = SvMAGICAL(hash);
2209 0 ▶ if (magic) srl_callback_begin(aTHX_ enc, NULL, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (magic) srl_callback_begin(aTHX_ enc, NULL, NULL); 0 0 0
2210 0 (void)hv_iterinit(hash);
2211 0 ▶ if (magic) srl_callback_end(aTHX_ enc);
Branch: 1 branch
BranchTrueFalseTotal
if (magic) srl_callback_end(aTHX_ enc); 0 0 0
2212 0 }
2213  
2214 SRL_STATIC_INLINE HE *
2215 0 srl_hv_iternext(pTHX_ srl_encoder_t *enc, HV *hash)
2216 {
2217 HE *entry;
2218 0 int magic = SvMAGICAL(hash);
2219 0 ▶ if (magic) srl_callback_begin(aTHX_ enc, NULL, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (magic) srl_callback_begin(aTHX_ enc, NULL, NULL); 0 0 0
2220 0 entry = hv_iternext(hash);
2221 0 ▶ if (magic) srl_callback_end(aTHX_ enc);
Branch: 1 branch
BranchTrueFalseTotal
if (magic) srl_callback_end(aTHX_ enc); 0 0 0
2222 0 return entry;
2223 }
2224  
2225 SRL_STATIC_INLINE SV *
2226 0 srl_hv_iterval(pTHX_ srl_encoder_t *enc, HV *hash, HE *entry)
2227 {
2228 SV *value;
2229 0 int magic = SvMAGICAL(hash);
2230 0 ▶ if (magic) srl_callback_begin(aTHX_ enc, NULL, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (magic) srl_callback_begin(aTHX_ enc, NULL, NULL); 0 0 0
2231 0 value = hv_iterval(hash, entry);
2232 0 ▶ if (magic) srl_callback_end(aTHX_ enc);
Branch: 1 branch
BranchTrueFalseTotal
if (magic) srl_callback_end(aTHX_ enc); 0 0 0
2233 0 return value;
2234 }
2235  
2236 SRL_STATIC_INLINE void
2237 0 srl_dump_hv_unsorted_nomg(pTHX_ srl_encoder_t *enc, HV *src, UV n)
2238 {
2239 0 HE *he = NULL;
2240 0 const int do_share_keys = HvSHAREKEYS((SV *)src);
2241 #ifdef XPERL_VERSION
2242 UV i;
2243 const UV limit = HvENTRY_LIMIT(src);
2244 enc->frames->cursor = &he;
2245  
2246 /* XPerl stores entries by position without bucket chains. */
2247 for (i = 0; i < limit && n; i++) {
2248 for (he = HvENTRY_FIRST(src, i); he;
2249 he = HvENTRY_NEXT(src, he)) {
2250 SV *v = HeVAL(he);
2251 if (v != &PL_sv_placeholder) {
2252 srl_dump_hk(aTHX_ enc, he, do_share_keys);
2253 v = HeVAL(he);
2254 CALL_SRL_DUMP_SV(enc, v);
2255 if (--n == 0)
2256 break;
2257 }
2258 }
2259 }
2260 #else
2261 0 HE **he_ptr= HvARRAY(src);
2262 0 HE **he_end= he_ptr + HvMAX(src) + 1;
2263 0 enc->frames->cursor = &he;
2264  
2265 do {
2266 0 ▶ for (he= *he_ptr++; he; he= HeNEXT(he) ) {
Branch: 1 branch
BranchTrueFalseTotal
for (he= *he_ptr++; he; he= HeNEXT(he) ) { 0 0 0
2267 0 SV *v= HeVAL(he);
2268 0 ▶ if (v != &PL_sv_placeholder) {
Branch: 1 branch
BranchTrueFalseTotal
if (v != &PL_sv_placeholder) { 0 0 0
2269 0 srl_dump_hk(aTHX_ enc, he, do_share_keys);
2270 0 v = HeVAL(he);
2271 0 ▶ CALL_SRL_DUMP_SV(enc, v);
Branch: 51 branches
BranchTrueFalseTotal
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
2272 0 ▶ if (--n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--n == 0) { 0 0 0
2273 0 he_ptr= he_end;
2274 0 break;
2275 }
2276 }
2277 }
2278 0 ▶ } while ( he_ptr < he_end );
Branch: 1 branch
BranchTrueFalseTotal
} while ( he_ptr < he_end ); 0 0 0
2279 #endif
2280 0 ▶ if (n)
Branch: 1 branch
BranchTrueFalseTotal
if (n) 0 0 0
2281 0 croak("Sereal: input hash was modified during encoding");
2282 0 }
2283  
2284 /* Walking a hash with hv_iterinit()/hv_iternext() resets the iterator that
2285 * a caller's each() relies on. For hashes that keep their iterator in the
2286 * HV (i.e. not tied ones), save it before such a walk and restore it after. */
2287 typedef struct {
2288 I32 riter;
2289 HE *eiter;
2290 int lazydel;
2291 } srl_hv_iter_state_t;
2292  
2293 SRL_STATIC_INLINE void
2294 0 srl_hv_iter_save(pTHX_ HV *hv, srl_hv_iter_state_t *st)
2295 {
2296 0 ▶ st->riter = HvRITER_get(hv);
Branch: 1 branch
BranchTrueFalseTotal
st->riter = HvRITER_get(hv); 0 0 0
2297 0 ▶ st->eiter = HvEITER_get(hv);
Branch: 1 branch
BranchTrueFalseTotal
st->eiter = HvEITER_get(hv); 0 0 0
2298 /* When the caller deleted its current each() entry, perl unlinks the HE
2299 * but defers freeing it (HvLAZYDEL) until the iterator moves on;
2300 * hv_iterinit() would free it now, while the caller still owns it. Hide
2301 * the flag across the walk and restore it together with the entry. */
2302 0 st->lazydel = HvLAZYDEL(hv) ? 1 : 0;
2303 0 HvLAZYDEL_off(hv);
2304 0 }
2305  
2306 SRL_STATIC_INLINE void
2307 0 srl_hv_iter_restore(pTHX_ HV *hv, const srl_hv_iter_state_t *st)
2308 {
2309 0 HE *eiter = st->eiter;
2310 0 ▶ if (eiter && !st->lazydel) {
Branch: 2 branches
BranchTrueFalseTotal
if (eiter && !st->lazydel) { 0 0 0
if (eiter && !st->lazydel) { 0 0 0
2311 /* A callback that ran during the walk (FETCH, FREEZE, DESTROY, ...) may
2312 * have deleted or moved the caller's current entry. While the walk owns
2313 * HvEITER, perl frees a deleted entry immediately instead of deferring
2314 * it, so only restore the pointer if it is still linked where it was. */
2315 0 HE *he = NULL;
2316 #ifdef XPERL_VERSION
2317 if (HvARRAY(hv) && HvHasAUX(hv) && st->riter >= 0) {
2318 UV i;
2319 const UV limit = HvENTRY_LIMIT(hv);
2320 for (i = 0; i < limit && !he; i++) {
2321 for (he = HvENTRY_FIRST(hv, i); he;
2322 he = HvENTRY_NEXT(hv, he))
2323 if (he == eiter) break;
2324 }
2325 }
2326 #else
2327 /* SvOOK: belt and braces, the walk's hv_iternext() re-creates the aux. */
2328 0 ▶ if (HvARRAY(hv) && SvOOK((SV *)hv) && st->riter >= 0) {
Branch: 6 branches
BranchTrueFalseTotal
if (HvARRAY(hv) && SvOOK((SV *)hv) && st->riter >= 0) { 0 0 0
if (HvARRAY(hv) && SvOOK((SV *)hv) && st->riter >= 0) { 0 0 0
if (HvARRAY(hv) && SvOOK((SV *)hv) && st->riter >= 0) { 0 0 0
if (HvARRAY(hv) && SvOOK((SV *)hv) && st->riter >= 0) { 0 0 0
if (HvARRAY(hv) && SvOOK((SV *)hv) && st->riter >= 0) { 0 0 0
if (HvARRAY(hv) && SvOOK((SV *)hv) && st->riter >= 0) { 0 0 0
2329 /* Under PERL_HASH_RANDOMIZE_KEYS, RITER is a position in
2330 * iteration order, not a bucket number: the bucket is
2331 * (riter ^ xhv_rand) & HvMAX, as PERL_HASH_ITER_BUCKET()
2332 * computes it. If xhv_rand changed during the walk (a split),
2333 * the lookup legitimately fails and EITER is dropped; perl
2334 * already documents iteration after insertion as undefined.
2335 * RITER >= 0 implies the HV has its aux struct. */
2336 #ifdef PERL_HASH_RANDOMIZE_KEYS
2337 0 const STRLEN bucket = ((STRLEN)st->riter ^ (STRLEN)HvAUX(hv)->xhv_rand) & HvMAX(hv);
2338 #else
2339 const STRLEN bucket = (STRLEN)st->riter;
2340 #endif
2341 0 ▶ if (bucket <= HvMAX(hv))
Branch: 1 branch
BranchTrueFalseTotal
if (bucket <= HvMAX(hv)) 0 0 0
2342 0 ▶ for (he = HvARRAY(hv)[bucket]; he; he = HeNEXT(he))
Branch: 1 branch
BranchTrueFalseTotal
for (he = HvARRAY(hv)[bucket]; he; he = HeNEXT(he)) 0 0 0
2343 0 ▶ if (he == eiter) break;
Branch: 1 branch
BranchTrueFalseTotal
if (he == eiter) break; 0 0 0
2344 }
2345 #endif
2346 0 ▶ if (!he) eiter = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!he) eiter = NULL; 0 0 0
2347 }
2348 /* A lazily-deleted entry is already unlinked and owned by the iterator
2349 * alone; with the flag off nothing freed it during the walk. */
2350 0 HvRITER_set(hv, st->riter);
2351 0 HvEITER_set(hv, eiter);
2352 0 ▶ if (st->lazydel && eiter)
Branch: 2 branches
BranchTrueFalseTotal
if (st->lazydel && eiter) 0 0 0
if (st->lazydel && eiter) 0 0 0
2353 0 HvLAZYDEL_on(hv);
2354 0 }
2355  
2356 static SRL_NOINLINE void
2357 0 srl_dump_hv_unsorted_mg(pTHX_ srl_encoder_t *enc, HV *src, const UV n)
2358 {
2359 0 HE *he = NULL;
2360 0 UV i= 0;
2361 0 const int do_share_keys = HvSHAREKEYS((SV *)src);
2362 /* Tied hashes keep their iterator in Perl (FIRSTKEY/NEXTKEY). */
2363 0 const int keep_iter = !mg_find((SV *)src, PERL_MAGIC_tied);
2364 srl_hv_iter_state_t iter_state;
2365 0 enc->frames->cursor = &he;
2366  
2367 0 ▶ if (keep_iter)
Branch: 1 branch
BranchTrueFalseTotal
if (keep_iter) 0 0 0
2368 0 srl_hv_iter_save(aTHX_ src, &iter_state);
2369 0 srl_hv_iterinit(aTHX_ enc, src); /* return value not reliable according to API docs */
2370 0 ▶ while ((he = srl_hv_iternext(aTHX_ enc, src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = srl_hv_iternext(aTHX_ enc, src))) { 0 0 0
2371 SV *v;
2372 0 ▶ if (expect_false( i == n ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( i == n )) 0 0 0
2373 0 croak("Panic: cannot serialize a tied hash which changes its size!");
2374 0 srl_dump_hk(aTHX_ enc, he, do_share_keys);
2375 0 v= srl_hv_iterval(aTHX_ enc, src, he);
2376 0 ▶ CALL_SRL_DUMP_SV(enc, v);
Branch: 51 branches
BranchTrueFalseTotal
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
2377 0 ++i;
2378 }
2379 0 ▶ if (expect_false( i != n ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( i != n )) 0 0 0
2380 0 croak("Panic: cannot serialize a tied hash which changes its size!");
2381 0 ▶ if (keep_iter)
Branch: 1 branch
BranchTrueFalseTotal
if (keep_iter) 0 0 0
2382 0 srl_hv_iter_restore(aTHX_ src, &iter_state);
2383 0 }
2384  
2385 /* sorting hashes - nothing in perl is easy. ever.
2386 *
2387 * Some things to keep in mind about perl hashes as you read this code:
2388 *
2389 * Hashes may be shared or not. Usually shared. This means they share their
2390 * key data via PL_strtab.
2391 *
2392 * Hashes may be tied or not. Usually not. When tied the keys from the hash
2393 * are available only as SV *'s, and when untied, the keys from the hash are
2394 * accessed via HE *'s.
2395 *
2396 * Some HE's actually contains SV's but most contain a ptr/len combo with
2397 * an utf8 flag. To make things even more interesting utf8 keys are
2398 * normalized to latin1 by perl where possible before being stored in the HE,
2399 * with the utf8 flag indicating "was utf8" instead of "is utf8" or "not utf8".
2400 *
2401 * The complexity about accessing the key for a hash can be managed away by
2402 * perl via API's like hv_iterkeysv(), but using that means constructing mortal
2403 * SV's for each key as we go.
2404 *
2405 * We could in theory use the HePV() interface, but one annoying result of the
2406 * "was utf8" logic is that we can't use a sort comparator which looks
2407 * at the raw binary of the keys when the keys might contain utf8. A utf8 key
2408 * like "\xDF" will be downgraded to ascii in the HE form, but will be upgraded
2409 * to the utf8 representation in the SV form. So if we want to do "fast" sorting
2410 * we have to restrict it to non-utf8/non-sv keys, and force the use of the SV
2411 * based API (which we have to use for tie's anyway) when we see a UTF8 key.
2412 *
2413 * Which is what we do below. In order to sort a hash we need to construct an
2414 * array of its contents, in srl_dump_sorted_nomg() we walk the hash, checking
2415 * each key, and copying each HE over into a scratch buffer which it then sorts.
2416 * If during the transcription process it sees any utf8 or SV keys it exits
2417 * immediately, and falls through to srl_dump_sort_mg(), which uses hv_iterkeysv()
2418 * to construct an array of HE_SV instead, which we then sort.
2419 */
2420  
2421  
2422  
2423 SRL_STATIC_INLINE int
2424 he_islt(const HE *a, const HE *b)
2425 {
2426 /* no need for a dTHX here, we don't use anything that needs it */
2427 const STRLEN la = HeKLEN(a);
2428 const STRLEN lb = HeKLEN(b);
2429 const int cmp = memcmp(HeKEY(a), HeKEY(b), la < lb ? la : lb);
2430 if (cmp) {
2431 return cmp < 0;
2432 } else {
2433 return la < lb;
2434 }
2435 }
2436  
2437 SRL_STATIC_INLINE int
2438 0 he_sv_islt_fast(const HE_SV *a, const HE_SV *b)
2439 {
2440 /* no need for a dTHX here, we don't use anything that needs it */
2441 char *a_ptr;
2442 char *b_ptr;
2443 int a_isutf8;
2444 int b_isutf8;
2445 0 ▶ const STRLEN a_len= a->key.sv ? SvCUR(a->key.sv) : HeKLEN(a->val.he);
Branch: 1 branch
BranchTrueFalseTotal
const STRLEN a_len= a->key.sv ? SvCUR(a->key.sv) : HeKLEN(a->val.he); 0 0 0
2446 0 ▶ const STRLEN b_len= b->key.sv ? SvCUR(b->key.sv) : HeKLEN(b->val.he);
Branch: 1 branch
BranchTrueFalseTotal
const STRLEN b_len= b->key.sv ? SvCUR(b->key.sv) : HeKLEN(b->val.he); 0 0 0
2447 0 ▶ if (a_len != b_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (a_len != b_len) { 0 0 0
2448 0 return a_len < b_len;
2449 }
2450 0 ▶ a_isutf8= (a->key.sv ? SvUTF8(a->key.sv) : HeKUTF8(a->val.he)) ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
a_isutf8= (a->key.sv ? SvUTF8(a->key.sv) : HeKUTF8(a->val.he)) ? 0 : 1; 0 0 0
a_isutf8= (a->key.sv ? SvUTF8(a->key.sv) : HeKUTF8(a->val.he)) ? 0 : 1; 0 0 0
2451 0 ▶ b_isutf8= (b->key.sv ? SvUTF8(b->key.sv) : HeKUTF8(b->val.he)) ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
b_isutf8= (b->key.sv ? SvUTF8(b->key.sv) : HeKUTF8(b->val.he)) ? 0 : 1; 0 0 0
b_isutf8= (b->key.sv ? SvUTF8(b->key.sv) : HeKUTF8(b->val.he)) ? 0 : 1; 0 0 0
2452 0 ▶ if (a_isutf8 != b_isutf8) {
Branch: 1 branch
BranchTrueFalseTotal
if (a_isutf8 != b_isutf8) { 0 0 0
2453 0 return a_isutf8 < b_isutf8;
2454 }
2455 0 ▶ a_ptr= a->key.sv ? SvPVX(a->key.sv) : HeKEY(a->val.he);
Branch: 1 branch
BranchTrueFalseTotal
a_ptr= a->key.sv ? SvPVX(a->key.sv) : HeKEY(a->val.he); 0 0 0
2456 0 ▶ b_ptr= b->key.sv ? SvPVX(b->key.sv) : HeKEY(b->val.he);
Branch: 1 branch
BranchTrueFalseTotal
b_ptr= b->key.sv ? SvPVX(b->key.sv) : HeKEY(b->val.he); 0 0 0
2457 0 return memcmp(a_ptr, b_ptr, a_len < b_len ? a_len : b_len ) < 0;
2458 }
2459  
2460 #define ISLT_HE_SV(a,b) he_sv_islt_fast( a, b )
2461 #define ISLT_SV_CMP(a,b) sv_cmp(a->key.sv, b->key.sv) == sort_dir
2462  
2463  
2464 SRL_STATIC_INLINE void
2465 0 srl_qsort(pTHX_ srl_encoder_t *enc, const UV n, HE_SV *array)
2466 {
2467 0 ▶ if ( SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS_PERL) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS_PERL) ) { 0 0 0
2468 0 ▶ int sort_dir= SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS_PERL_REV) ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
int sort_dir= SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS_PERL_REV) ? 1 : -1; 0 0 0
2469 /* hack to forcefully disable "use bytes" */
2470 0 COP cop= *PL_curcop;
2471 0 cop.op_private= 0;
2472  
2473 0 ENTER;
2474 0 SAVETMPS;
2475  
2476 0 SAVEVPTR (PL_curcop);
2477 0 PL_curcop= &cop;
2478
2479 /* now sort */
2480 0 ▶ QSORT(HE_SV, array, n, ISLT_SV_CMP);
Branch: 25 branches
BranchTrueFalseTotal
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
QSORT(HE_SV, array, n, ISLT_SV_CMP); 0 0 0
2481  
2482 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
2483 0 LEAVE;
2484 } else {
2485 /* now sort */
2486 0 ▶ QSORT(HE_SV, array, n, ISLT_HE_SV);
Branch: 25 branches
BranchTrueFalseTotal
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
QSORT(HE_SV, array, n, ISLT_HE_SV); 0 0 0
2487 }
2488 0 }
2489  
2490  
2491 static SRL_NOINLINE void
2492 0 srl_dump_hv_sv(pTHX_ srl_encoder_t *enc, HV *src, const UV n, HE_SV *array)
2493 {
2494 0 HE *he = NULL;
2495 0 UV i= 0;
2496 0 const int is_tie= SvMAGICAL(src);
2497 /* Tied hashes keep their iterator in Perl (FIRSTKEY/NEXTKEY). */
2498 0 ▶ const int keep_iter = !(is_tie && mg_find((SV *)src, PERL_MAGIC_tied));
Branch: 2 branches
BranchTrueFalseTotal
const int keep_iter = !(is_tie && mg_find((SV *)src, PERL_MAGIC_tied)); 0 0 0
const int keep_iter = !(is_tie && mg_find((SV *)src, PERL_MAGIC_tied)); 0 0 0
2499 srl_hv_iter_state_t iter_state;
2500  
2501 /* This sub is used for ties, hashes with SV keys, and SORT_KEYS_PERL.
2502 * Only keys are collected for sorting; values are fetched at use.
2503 * It is the slowest way
2504 * and most memory hungry way to serialize a hash. We will use the
2505 * full perl api for extracting the contents of the hash, which fortifies
2506 * us against ties, and we will convert all keys into mortal
2507 * sv's where necessary. This means we can use sv_cmp on the keys
2508 * if we wish.
2509 */
2510  
2511 0 ▶ if (keep_iter)
Branch: 1 branch
BranchTrueFalseTotal
if (keep_iter) 0 0 0
2512 0 srl_hv_iter_save(aTHX_ src, &iter_state);
2513 0 enc->frames->cursor = &he;
2514 0 srl_hv_iterinit(aTHX_ enc, src); /* return value not reliable according to API docs */
2515 {
2516 HE_SV *array_end;
2517 0 ▶ if (!array) {
Branch: 1 branch
BranchTrueFalseTotal
if (!array) { 0 0 0
2518 0 ▶ Newx(array, n, HE_SV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(array, n, HE_SV); 0 0 0
2519 0 SAVEFREEPV(array);
2520 }
2521 0 array_end= array + n;
2522 0 ▶ while ((he = srl_hv_iternext(aTHX_ enc, src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = srl_hv_iternext(aTHX_ enc, src))) { 0 0 0
2523 SV *key;
2524 0 ▶ int magic_key = is_tie || HeKLEN(he) == HEf_SVKEY;
Branch: 2 branches
BranchTrueFalseTotal
int magic_key = is_tie || HeKLEN(he) == HEf_SVKEY; 0 0 0
int magic_key = is_tie || HeKLEN(he) == HEf_SVKEY; 0 0 0
2525 0 ▶ if (expect_false( i == n ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( i == n )) 0 0 0
2526 0 ▶ croak("Panic: cannot serialize a %s hash which changes its size!",is_tie ? "tied" : "untied");
Branch: 1 branch
BranchTrueFalseTotal
croak("Panic: cannot serialize a %s hash which changes its size!",is_tie ? "tied" : "untied"); 0 0 0
2527 0 ▶ if (magic_key) srl_callback_begin(aTHX_ enc, NULL, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (magic_key) srl_callback_begin(aTHX_ enc, NULL, NULL); 0 0 0
2528 0 key = hv_iterkeysv(he);
2529 0 ▶ if (magic_key) srl_callback_end(aTHX_ enc);
Branch: 1 branch
BranchTrueFalseTotal
if (magic_key) srl_callback_end(aTHX_ enc); 0 0 0
2530 0 ▶ if (SvGMAGICAL(key) || SvROK(key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvGMAGICAL(key) || SvROK(key)) { 0 0 0
if (SvGMAGICAL(key) || SvROK(key)) { 0 0 0
2531 STRLEN len;
2532 const char *pv;
2533 0 srl_callback_begin(aTHX_ enc, key, NULL);
2534 0 SvGETMAGIC(key);
2535 0 pv = SvPV_nomg(key, len);
2536 0 array[i].key.sv = sv_2mortal(newSVpvn(pv, len));
2537 0 ▶ if (SvUTF8(key)) SvUTF8_on(array[i].key.sv);
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(key)) SvUTF8_on(array[i].key.sv); 0 0 0
2538 0 srl_callback_end(aTHX_ enc);
2539 } else {
2540 0 int magical = SvMAGICAL(key);
2541 0 ▶ if (magical) srl_callback_begin(aTHX_ enc, key, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (magical) srl_callback_begin(aTHX_ enc, key, NULL); 0 0 0
2542 0 array[i].key.sv = sv_2mortal(newSVsv_flags(key, 0));
2543 0 ▶ if (magical) srl_callback_end(aTHX_ enc);
Branch: 1 branch
BranchTrueFalseTotal
if (magical) srl_callback_end(aTHX_ enc); 0 0 0
2544 }
2545 0 i++;
2546 }
2547 0 ▶ if (expect_false( i != n ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( i != n )) 0 0 0
2548 0 ▶ croak("Panic: can not serialize a %s hash which changes it size!", is_tie ? "tied" : "untied");
Branch: 1 branch
BranchTrueFalseTotal
croak("Panic: can not serialize a %s hash which changes it size!", is_tie ? "tied" : "untied"); 0 0 0
2549 0 ▶ if (keep_iter)
Branch: 1 branch
BranchTrueFalseTotal
if (keep_iter) 0 0 0
2550 0 srl_hv_iter_restore(aTHX_ src, &iter_state);
2551  
2552 0 he = NULL;
2553 0 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS))
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS)) 0 0 0
2554 0 srl_qsort(aTHX_ enc, n, array);
2555  
2556 0 ▶ while ( array < array_end ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( array < array_end ) { 0 0 0
2557 SV *value;
2558 0 ▶ if (is_tie) srl_callback_begin(aTHX_ enc, NULL, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (is_tie) srl_callback_begin(aTHX_ enc, NULL, NULL); 0 0 0
2559 0 he = hv_fetch_ent(src, array->key.sv, 0, 0);
2560 0 ▶ if (is_tie) srl_callback_end(aTHX_ enc);
Branch: 1 branch
BranchTrueFalseTotal
if (is_tie) srl_callback_end(aTHX_ enc); 0 0 0
2561 0 ▶ if (!he)
Branch: 1 branch
BranchTrueFalseTotal
if (!he) 0 0 0
2562 0 croak("Sereal: input hash was modified during encoding");
2563 0 value = HeVAL(he);
2564 0 srl_dump_hk_sv(aTHX_ enc, array->key.sv);
2565 0 ▶ CALL_SRL_DUMP_SV(enc, value);
Branch: 51 branches
BranchTrueFalseTotal
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
CALL_SRL_DUMP_SV(enc, value); 0 0 0
2566 0 array++;
2567 }
2568 }
2569 0 }
2570  
2571  
2572 static SRL_NOINLINE void
2573 0 srl_dump_hv_sorted_nomg(pTHX_ srl_encoder_t *enc, HV *src, const UV n)
2574 {
2575 0 HE *he = NULL;
2576 0 const int do_share_keys = HvSHAREKEYS((SV *)src);
2577 srl_hv_iter_state_t iter_state;
2578 0 srl_encoder_frame *frame = enc->frames;
2579 0 frame->cursor = &he;
2580 0 frame->sorted_keyed = 0;
2581  
2582 /* This sub is used only for untied hashes and when the user wants
2583 * sorted keys, but not necessarily the order that perl would use.
2584 */
2585  
2586 0 srl_hv_iter_save(aTHX_ src, &iter_state);
2587 0 srl_hv_iterinit(aTHX_ enc, src); /* return value not reliable according to API docs */
2588 {
2589 HE_SV *array;
2590 HE_SV *array_ptr;
2591 HE_SV *array_end;
2592 0 ▶ Newx(array, n, HE_SV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(array, n, HE_SV); 0 0 0
2593 0 SAVEFREEPV(array);
2594 0 array_ptr = array;
2595 0 ▶ while ((he = srl_hv_iternext(aTHX_ enc, src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = srl_hv_iternext(aTHX_ enc, src))) { 0 0 0
2596 0 ▶ if (HeKLEN(he) == HEf_SVKEY) {
Branch: 1 branch
BranchTrueFalseTotal
if (HeKLEN(he) == HEf_SVKEY) { 0 0 0
2597 0 srl_hv_iter_restore(aTHX_ src, &iter_state);
2598 0 srl_dump_hv_sv(aTHX_ enc, src, n, array);
2599 0 return;
2600 }
2601 0 ▶ if ( HeKWASUTF8(he) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( HeKWASUTF8(he) ) { 0 0 0
2602 0 array_ptr->key.sv= hv_iterkeysv(he);
2603 } else {
2604 0 ▶ array_ptr->key.sv = HeSVKEY(he);
Branch: 2 branches
BranchTrueFalseTotal
array_ptr->key.sv = HeSVKEY(he); 0 0 0
array_ptr->key.sv = HeSVKEY(he); 0 0 0
2605 }
2606 0 array_ptr->val.he = he;
2607 0 array_ptr++;
2608 }
2609 0 srl_hv_iter_restore(aTHX_ src, &iter_state);
2610
2611 0 frame->sorted = array;
2612 0 frame->sorted_count = n;
2613 0 srl_qsort(aTHX_ enc, n, array);
2614  
2615 0 array_end = array + n;
2616 0 ▶ for ( array_end= array + n; array < array_end; array++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( array_end= array + n; array < array_end; array++ ) { 0 0 0
2617 SV *v;
2618 /* A previous callback retained the future keys. Reuse that
2619 * stable key for validation of this entry's callbacks. */
2620 0 ▶ frame->active_sorted_key = frame->sorted_keyed ? array->key.sv : NULL;
Branch: 1 branch
BranchTrueFalseTotal
frame->active_sorted_key = frame->sorted_keyed ? array->key.sv : NULL; 0 0 0
2621 0 ▶ if (frame->sorted_keyed) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame->sorted_keyed) { 0 0 0
2622 0 he = hv_fetch_ent(src, array->key.sv, 0, 0);
2623 0 ▶ if (!he || he != array->val.he)
Branch: 2 branches
BranchTrueFalseTotal
if (!he || he != array->val.he) 0 0 0
if (!he || he != array->val.he) 0 0 0
2624 0 croak("Sereal: input hash was modified during encoding");
2625 } else {
2626 0 he = array->val.he;
2627 }
2628 0 srl_dump_hk(aTHX_ enc, he, do_share_keys);
2629 0 v = srl_hv_iterval(aTHX_ enc, src, he);
2630 0 ▶ CALL_SRL_DUMP_SV(enc, v);
Branch: 51 branches
BranchTrueFalseTotal
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
CALL_SRL_DUMP_SV(enc, v); 0 0 0
2631 0 frame->sorted = array + 1;
2632 0 --frame->sorted_count;
2633 }
2634 }
2635 }
2636  
2637 static SRL_NOINLINE void
2638 0 srl_dump_hv(pTHX_ srl_encoder_t *enc, HV *src, U32 refcount)
2639 {
2640 0 HE *he = NULL;
2641 UV n;
2642 srl_encoder_frame frame;
2643 0 srl_frame_push(enc, &frame, (SV *)src);
2644 0 frame.cursor = NULL;
2645 0 frame.sorted = NULL;
2646 0 frame.active_sorted_key = NULL;
2647 0 ▶ if ( SvMAGICAL(src) && mg_find((SV *)src, PERL_MAGIC_tied) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SvMAGICAL(src) && mg_find((SV *)src, PERL_MAGIC_tied) ) { 0 0 0
if ( SvMAGICAL(src) && mg_find((SV *)src, PERL_MAGIC_tied) ) { 0 0 0
2648 /* for tied hashes, we have to iterate to find the number of entries. Alas... */
2649 0 n= 0;
2650 0 srl_hv_iterinit(aTHX_ enc, src); /* return value not reliable according to API docs */
2651 0 ▶ while ((he = srl_hv_iternext(aTHX_ enc, src))) { ++n; }
Branch: 1 branch
BranchTrueFalseTotal
while ((he = srl_hv_iternext(aTHX_ enc, src))) { ++n; } 0 0 0
2652 }
2653 else {
2654 0 ▶ n= HvUSEDKEYS(src);
Branch: 1 branch
BranchTrueFalseTotal
n= HvUSEDKEYS(src); 0 0 0
2655 }
2656  
2657 0 ▶ BUF_SIZE_ASSERT_HV(&enc->buf, n);
Branch: 5 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT_HV(&enc->buf, n); 0 0 0
BUF_SIZE_ASSERT_HV(&enc->buf, n); 0 0 0
BUF_SIZE_ASSERT_HV(&enc->buf, n); 0 0 0
BUF_SIZE_ASSERT_HV(&enc->buf, n); 0 0 0
BUF_SIZE_ASSERT_HV(&enc->buf, n); 0 0 0
2658 0 ▶ if (n < 16 && refcount == 1 && !SRL_ENC_HAVE_OPTION(enc,SRL_F_CANONICAL_REFS)) {
Branch: 3 branches
BranchTrueFalseTotal
if (n < 16 && refcount == 1 && !SRL_ENC_HAVE_OPTION(enc,SRL_F_CANONICAL_REFS)) { 0 0 0
if (n < 16 && refcount == 1 && !SRL_ENC_HAVE_OPTION(enc,SRL_F_CANONICAL_REFS)) { 0 0 0
if (n < 16 && refcount == 1 && !SRL_ENC_HAVE_OPTION(enc,SRL_F_CANONICAL_REFS)) { 0 0 0
2659 0 enc->buf.pos--; /* backup over the previous REFN */
2660 0 srl_buf_cat_char_nocheck(&enc->buf, SRL_HDR_HASHREF + n);
2661 } else {
2662 0 srl_buf_cat_varint_nocheck(aTHX_ &enc->buf, SRL_HDR_HASH, n);
2663 }
2664  
2665 0 ▶ if ( n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( n ) { 0 0 0
2666 0 ▶ if ( SvMAGICAL(src) || SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS_PERL) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SvMAGICAL(src) || SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS_PERL) ) { 0 0 0
if ( SvMAGICAL(src) || SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS_PERL) ) { 0 0 0
2667 /* SORT_KEYS_PERL implies SORT_KEYS, but we check for either just to be
2668 * careful - yves*/
2669 0 ▶ if ( SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS|SRL_F_SORT_KEYS_PERL) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS|SRL_F_SORT_KEYS_PERL) ) { 0 0 0
2670 0 srl_dump_hv_sv(aTHX_ enc, src, n, NULL);
2671 }
2672 else {
2673 0 srl_dump_hv_unsorted_mg(aTHX_ enc, src, n);
2674 }
2675 }
2676 else {
2677 0 ▶ if ( SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SRL_ENC_HAVE_OPTION(enc, SRL_F_SORT_KEYS) ) { 0 0 0
2678 0 srl_dump_hv_sorted_nomg(aTHX_ enc, src, n);
2679 }
2680 else {
2681 0 srl_dump_hv_unsorted_nomg(aTHX_ enc, src, n);
2682 }
2683 }
2684 }
2685 0 enc->frames = frame.parent;
2686 0 }
2687  
2688  
2689  
2690 /* Shared key strings are immutable, so their pointers can identify COPY
2691 * targets. This table is separate from scalar-value alias deduplication. */
2692 SRL_STATIC_INLINE int
2693 0 srl_dump_shared_hk(pTHX_ srl_encoder_t *enc, HEK *hek)
2694 {
2695 0 ▶ PTABLE_t *seen = SRL_GET_STR_PTR_SEENHASH(enc);
Branch: 1 branch
BranchTrueFalseTotal
PTABLE_t *seen = SRL_GET_STR_PTR_SEENHASH(enc); 0 0 0
2696 0 const ptrdiff_t offset = (ptrdiff_t)PTABLE_fetch(seen, hek);
2697 0 ▶ if (HEK_FLAGS(hek) & SRL_HVhek_NOTSHARED)
Branch: 1 branch
BranchTrueFalseTotal
if (HEK_FLAGS(hek) & SRL_HVhek_NOTSHARED) 0 0 0
2698 0 return 0;
2699 0 ▶ if (offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset) { 0 0 0
2700 0 srl_buf_cat_varint(aTHX_ &enc->buf, SRL_HDR_COPY, (UV)offset);
2701 0 return 1;
2702 }
2703 0 PTABLE_store(seen, hek, INT2PTR(void *, BODY_POS_OFS(&enc->buf)));
2704 0 ▶ if (SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS))
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS)) 0 0 0
2705 0 srl_callback_hold_key(aTHX_ enc, hek);
2706 0 return 0;
2707 }
2708  
2709 SRL_STATIC_INLINE void
2710 0 srl_dump_hk_pv(pTHX_ srl_encoder_t *enc, const char *str, STRLEN len, int is_utf8)
2711 {
2712 /* Keys share literal definitions with values, but never enter alias_ofs.
2713 * Keep only literal COPY targets. */
2714 0 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_DEDUPE_STRINGS)
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_DEDUPE_STRINGS) 0 0 0
2715 0 ▶ && SRL_ENC_HAVE_OPTION(enc, SRL_F_SHARED_HASHKEYS))
Branch: 1 branch
BranchTrueFalseTotal
&& SRL_ENC_HAVE_OPTION(enc, SRL_F_SHARED_HASHKEYS)) 0 0 0
2716 {
2717 int found;
2718 0 srl_str_dedupe_slot_t *slot = srl_str_dedupe_lookup_or_insert(
2719 0 aTHX_ &enc->str_dedupe_tbl, &enc->buf,
2720 str, len, (U32)(is_utf8 ? 1U : 0U),
2721 0 ▶ (UV)BODY_POS_OFS(&enc->buf), &found
Branch: 1 branch
BranchTrueFalseTotal
(UV)BODY_POS_OFS(&enc->buf), &found 0 0 0
2722 );
2723 0 ▶ if (found) {
Branch: 1 branch
BranchTrueFalseTotal
if (found) { 0 0 0
2724 0 srl_buf_cat_varint(aTHX_ &enc->buf, SRL_HDR_COPY, slot->copy_ofs);
2725 0 return;
2726 }
2727 }
2728 0 srl_dump_pv(aTHX_ enc, str, len, is_utf8);
2729 }
2730  
2731 SRL_STATIC_INLINE void
2732 0 srl_dump_hk_sv(pTHX_ srl_encoder_t *enc, SV *src)
2733 {
2734 STRLEN len;
2735 0 const char *str = SvPV(src, len);
2736 0 ▶ if (!SRL_ENC_HAVE_OPTION(enc, SRL_F_DEDUPE_STRINGS)
Branch: 1 branch
BranchTrueFalseTotal
if (!SRL_ENC_HAVE_OPTION(enc, SRL_F_DEDUPE_STRINGS) 0 0 0
2737 0 ▶ && SvIsCOW_shared_hash(src)
Branch: 2 branches
BranchTrueFalseTotal
&& SvIsCOW_shared_hash(src) 0 0 0
&& SvIsCOW_shared_hash(src) 0 0 0
2738 0 ▶ && SRL_ENC_HAVE_OPTION(enc, SRL_F_SHARED_HASHKEYS)
Branch: 1 branch
BranchTrueFalseTotal
&& SRL_ENC_HAVE_OPTION(enc, SRL_F_SHARED_HASHKEYS) 0 0 0
2739 0 ▶ && srl_dump_shared_hk(aTHX_ enc, SvSHARED_HEK_FROM_PV(SvPVX(src))))
Branch: 1 branch
BranchTrueFalseTotal
&& srl_dump_shared_hk(aTHX_ enc, SvSHARED_HEK_FROM_PV(SvPVX(src)))) 0 0 0
2740 0 return;
2741  
2742 0 srl_dump_hk_pv(aTHX_ enc, str, len, SvUTF8(src));
2743 }
2744  
2745 SRL_STATIC_INLINE void
2746 0 srl_dump_hk(pTHX_ srl_encoder_t *enc, HE *src, const int share_keys)
2747 {
2748 char *str;
2749 STRLEN len;
2750 char mode;
2751  
2752 0 ▶ if (HeKLEN(src) == HEf_SVKEY) {
Branch: 1 branch
BranchTrueFalseTotal
if (HeKLEN(src) == HEf_SVKEY) { 0 0 0
2753 0 ▶ SV *sv = HeSVKEY(src);
Branch: 2 branches
BranchTrueFalseTotal
SV *sv = HeSVKEY(src); 0 0 0
SV *sv = HeSVKEY(src); 0 0 0
2754  
2755 0 srl_getmagic(aTHX_ enc, sv, NULL);
2756 0 str = SvPV_nomg(sv, len);
2757 0 mode= SvUTF8(sv) ? 1 : 0;
2758  
2759 }
2760 else {
2761 0 str = HeKEY(src);
2762 /* This logic is an optimization for output space: We keep track of
2763 * all seen hash key strings that are in perl's shared string storage.
2764 * If we see one again, we just emit a COPY instruction.
2765 * This means that we only need to keep a ptr table since the strings
2766 * don't move in the shared key storage -- otherwise, we'd have to
2767 * compare strings / keep a full string hash table. */
2768 0 ▶ if ( share_keys && SRL_ENC_HAVE_OPTION(enc, SRL_F_SHARED_HASHKEYS) /* only enter branch if shared hk's enabled */
Branch: 2 branches
BranchTrueFalseTotal
if ( share_keys && SRL_ENC_HAVE_OPTION(enc, SRL_F_SHARED_HASHKEYS) /* only enter branch if shared hk's enabled */ 0 0 0
if ( share_keys && SRL_ENC_HAVE_OPTION(enc, SRL_F_SHARED_HASHKEYS) /* only enter branch if shared hk's enabled */ 0 0 0
2769 0 && !SRL_ENC_HAVE_OPTION(enc, SRL_F_DEDUPE_STRINGS)
2770 #if PERL_VERSION >= 10
2771 0 ▶ && (!DO_SHARED_HASH_ENTRY_REFCOUNT_CHECK
Branch: 1 branch
BranchTrueFalseTotal
&& (!DO_SHARED_HASH_ENTRY_REFCOUNT_CHECK 0 0 0
2772 || src->he_valu.hent_refcount > 1)
2773 #endif
2774 )
2775 {
2776 0 ▶ if (srl_dump_shared_hk(aTHX_ enc, HeKEY_hek(src)))
Branch: 1 branch
BranchTrueFalseTotal
if (srl_dump_shared_hk(aTHX_ enc, HeKEY_hek(src))) 0 0 0
2777 0 return;
2778 }
2779 0 len= HeKLEN(src);
2780 0 ▶ mode= HeKWASUTF8(src) ? 2 : HeKUTF8(src) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
mode= HeKWASUTF8(src) ? 2 : HeKUTF8(src) ? 1 : 0; 0 0 0
2781 }
2782 0 ▶ if (mode == 2) { /* must convert back to utf8 */
Branch: 1 branch
BranchTrueFalseTotal
if (mode == 2) { /* must convert back to utf8 */ 0 0 0
2783 0 char* utf8= (char *)Perl_bytes_to_utf8(aTHX_ (U8 *)str, &len);
2784 0 srl_dump_hk_pv(aTHX_ enc, utf8, len, 1);
2785 0 Safefree(utf8);
2786 } else {
2787 0 srl_dump_hk_pv(aTHX_ enc, str, len, mode);
2788 }
2789 }
2790  
2791 static SRL_NOINLINE void
2792 0 srl_dump_svpv_dedupe(pTHX_ srl_encoder_t *enc, const char *str, STRLEN len, int is_utf8, int allow_alias)
2793 {
2794 int found;
2795 0 UV current_ofs = (UV)BODY_POS_OFS(&enc->buf);
2796 0 ▶ srl_str_dedupe_slot_t *slot = srl_str_dedupe_lookup_or_insert(
Branch: 1 branch
BranchTrueFalseTotal
srl_str_dedupe_slot_t *slot = srl_str_dedupe_lookup_or_insert( 0 0 0
2797 0 aTHX_ &enc->str_dedupe_tbl, &enc->buf,
2798 str, len, (U32)(is_utf8 ? 1U : 0U), current_ofs, &found
2799 );
2800 0 ▶ if (found) {
Branch: 1 branch
BranchTrueFalseTotal
if (found) { 0 0 0
2801 0 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_ALIASED_DEDUPE_STRINGS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_ALIASED_DEDUPE_STRINGS)) { 0 0 0
2802 /* REFN needs a new referent, not a container-slot ALIAS. Leave
2803 * its scalar in ref_seenhash and use COPY for repeated text. */
2804 0 ▶ if (slot->alias_ofs != 0 && allow_alias) {
Branch: 2 branches
BranchTrueFalseTotal
if (slot->alias_ofs != 0 && allow_alias) { 0 0 0
if (slot->alias_ofs != 0 && allow_alias) { 0 0 0
2805 0 SRL_SET_TRACK_FLAG(*(enc->buf.body_pos + slot->alias_ofs));
2806 0 srl_buf_cat_varint(aTHX_ &enc->buf, SRL_HDR_ALIAS, slot->alias_ofs);
2807 0 return;
2808 }
2809 /* This value will be emitted as COPY of a prior literal (e.g. a
2810 * hash key). Record its offset so later values may ALIAS it. */
2811 0 ▶ if (slot->alias_ofs == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (slot->alias_ofs == 0) 0 0 0
2812 0 slot->alias_ofs = current_ofs;
2813 }
2814 0 srl_buf_cat_varint(aTHX_ &enc->buf, SRL_HDR_COPY, slot->copy_ofs);
2815 0 return;
2816 }
2817 0 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_ALIASED_DEDUPE_STRINGS))
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_ALIASED_DEDUPE_STRINGS)) 0 0 0
2818 0 slot->alias_ofs = current_ofs;
2819 0 srl_dump_pv(aTHX_ enc, str, len, is_utf8);
2820 }
2821  
2822 SRL_STATIC_INLINE void
2823 3 srl_dump_svpv_pv(pTHX_ srl_encoder_t *enc, SV *src, const char *str, STRLEN len, int allow_alias)
2824 {
2825 3 int is_utf8 = SvUTF8(src) ? 1 : 0;
2826 3 ▶ if (expect_false( SRL_ENC_HAVE_OPTION(enc, SRL_F_DEDUPE_STRINGS) && len > 3 )) {
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( SRL_ENC_HAVE_OPTION(enc, SRL_F_DEDUPE_STRINGS) && len > 3 )) { 0 3 3
if (expect_false( SRL_ENC_HAVE_OPTION(enc, SRL_F_DEDUPE_STRINGS) && len > 3 )) { 0 0 0
if (expect_false( SRL_ENC_HAVE_OPTION(enc, SRL_F_DEDUPE_STRINGS) && len > 3 )) { 0 3 3
2827 0 srl_dump_svpv_dedupe(aTHX_ enc, str, len, is_utf8, allow_alias);
2828 0 return;
2829 }
2830 3 srl_dump_pv(aTHX_ enc, str, len, is_utf8);
2831 }
2832  
2833 SRL_STATIC_INLINE void
2834 0 srl_dump_svpv(pTHX_ srl_encoder_t *enc, SV *src, int allow_alias)
2835 {
2836 STRLEN len;
2837 0 const char *str = SvPV_nomg(src, len);
2838 0 srl_dump_svpv_pv(aTHX_ enc, src, str, len, allow_alias);
2839 0 }
2840  
2841 SRL_STATIC_INLINE void
2842 3 srl_dump_pv(pTHX_ srl_encoder_t *enc, const char* src, STRLEN src_len, int is_utf8)
2843 {
2844 3 ▶ if (expect_false((size_t)src_len > SRL_BUF_MAX_SIZE - (size_t)(1 + SRL_MAX_VARINT_LENGTH)))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false((size_t)src_len > SRL_BUF_MAX_SIZE - (size_t)(1 + SRL_MAX_VARINT_LENGTH))) 0 3 3
2845 0 croak("Sereal output buffer is too large");
2846 3 ▶ BUF_SIZE_ASSERT(&enc->buf, 1 + SRL_MAX_VARINT_LENGTH + src_len); /* overallocate a bit sometimes */
Branch: 4 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(&enc->buf, 1 + SRL_MAX_VARINT_LENGTH + src_len); /* overallocate a bit sometimes */ 3 0 3
BUF_SIZE_ASSERT(&enc->buf, 1 + SRL_MAX_VARINT_LENGTH + src_len); /* overallocate a bit sometimes */ 0 3 3
BUF_SIZE_ASSERT(&enc->buf, 1 + SRL_MAX_VARINT_LENGTH + src_len); /* overallocate a bit sometimes */ 0 3 3
BUF_SIZE_ASSERT(&enc->buf, 1 + SRL_MAX_VARINT_LENGTH + src_len); /* overallocate a bit sometimes */ 0 0 0
2847 3 ▶ if (is_utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_utf8) { 0 3 3
2848 0 srl_buf_cat_varint_nocheck(aTHX_ &enc->buf, SRL_HDR_STR_UTF8, src_len);
2849 3 ▶ } else if (src_len <= SRL_MASK_SHORT_BINARY_LEN) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (src_len <= SRL_MASK_SHORT_BINARY_LEN) { 3 0 3
2850 3 srl_buf_cat_char_nocheck(&enc->buf, SRL_HDR_SHORT_BINARY_LOW | (char)src_len);
2851 } else {
2852 0 srl_buf_cat_varint_nocheck(aTHX_ &enc->buf, SRL_HDR_BINARY, src_len);
2853 }
2854 3 ▶ if (src_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (src_len) { 3 0 3
2855 3 Copy(src, enc->buf.pos, src_len, char);
2856 3 enc->buf.pos += src_len;
2857 }
2858 3 }
2859  
2860 static SRL_NOINLINE void
2861 0 srl_dump_vstring(pTHX_ srl_encoder_t *enc, SV *src, int allow_alias)
2862 {
2863 STRLEN len;
2864 const char *spelling;
2865 0 ▶ const U8 ext_tag = SvUTF8(src) ? SRL_EXT_VSTRING_UTF8 : SRL_EXT_VSTRING_BINARY;
Branch: 1 branch
BranchTrueFalseTotal
const U8 ext_tag = SvUTF8(src) ? SRL_EXT_VSTRING_UTF8 : SRL_EXT_VSTRING_BINARY; 0 0 0
2866 #ifdef SvVSTRING
2867 0 spelling = SvVSTRING(src, len);
2868 #else
2869 {
2870 MAGIC *vstring_mg = SvVSTRING_mg(src);
2871 if (!vstring_mg || !vstring_mg->mg_ptr || vstring_mg->mg_len < 0) {
2872 srl_dump_svpv(aTHX_ enc, src, allow_alias);
2873 return;
2874 }
2875 spelling = vstring_mg->mg_ptr;
2876 len = (STRLEN)vstring_mg->mg_len;
2877 }
2878 #endif
2879 0 ▶ if (!spelling) {
Branch: 1 branch
BranchTrueFalseTotal
if (!spelling) { 0 0 0
2880 0 srl_dump_svpv(aTHX_ enc, src, allow_alias);
2881 0 return;
2882 }
2883 0 ▶ if (len >= I32_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= I32_MAX) { 0 0 0
2884 0 srl_dump_svpv(aTHX_ enc, src, allow_alias);
2885 0 return;
2886 }
2887 {
2888 0 SV *roundtrip = sv_2mortal(newSV(0));
2889 0 const char *spelling_end = spelling + len;
2890 const char *parsed;
2891 STRLEN src_len, roundtrip_len;
2892 const char *src_pv;
2893 const char *roundtrip_pv;
2894 0 (void)sv_grow(roundtrip, len + 1);
2895 0 parsed = scan_vstring(spelling, spelling_end, roundtrip);
2896 0 ▶ if (parsed != spelling_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (parsed != spelling_end) { 0 0 0
2897 0 srl_dump_svpv(aTHX_ enc, src, allow_alias);
2898 0 return;
2899 }
2900 0 ▶ src_pv = SvPV_nomg(src, src_len);
Branch: 1 branch
BranchTrueFalseTotal
src_pv = SvPV_nomg(src, src_len); 0 0 0
2901 0 roundtrip_pv = SvPV(roundtrip, roundtrip_len);
2902 0 ▶ if (src_len != roundtrip_len
Branch: 1 branch
BranchTrueFalseTotal
if (src_len != roundtrip_len 0 0 0
2903 0 ▶ || !!SvUTF8(src) != !!SvUTF8(roundtrip)
Branch: 1 branch
BranchTrueFalseTotal
|| !!SvUTF8(src) != !!SvUTF8(roundtrip) 0 0 0
2904 0 ▶ || memNE(src_pv, roundtrip_pv, src_len))
Branch: 1 branch
BranchTrueFalseTotal
|| memNE(src_pv, roundtrip_pv, src_len)) 0 0 0
2905 {
2906 /* The magic spelling and PV can diverge after mutation. In that
2907 * case preserve the actual scalar value as an ordinary string. */
2908 0 srl_dump_svpv(aTHX_ enc, src, allow_alias);
2909 0 return;
2910 }
2911 }
2912 0 ▶ BUF_SIZE_ASSERT(&enc->buf, 2 + SRL_MAX_VARINT_LENGTH + len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(&enc->buf, 2 + SRL_MAX_VARINT_LENGTH + len); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 2 + SRL_MAX_VARINT_LENGTH + len); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 2 + SRL_MAX_VARINT_LENGTH + len); 0 0 0
BUF_SIZE_ASSERT(&enc->buf, 2 + SRL_MAX_VARINT_LENGTH + len); 0 0 0
2913 0 srl_buf_cat_char_nocheck(&enc->buf, SRL_HDR_EXTEND);
2914 0 srl_buf_cat_char_nocheck(&enc->buf, ext_tag);
2915 0 srl_buf_cat_varint_nocheck(aTHX_ &enc->buf, 0, len);
2916 0 Copy(spelling, enc->buf.pos, len, char);
2917 0 enc->buf.pos += len;
2918 }
2919  
2920 #ifdef HAS_HV_BACKREFS
2921 AV *
2922 0 srl_hv_backreferences_p_safe(pTHX_ HV *hv) {
2923 0 ▶ if (SvOOK(hv)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvOOK(hv)) { 0 0 0
if (SvOOK(hv)) { 0 0 0
if (SvOOK(hv)) { 0 0 0
if (SvOOK(hv)) { 0 0 0
2924 0 struct xpvhv_aux * const iter = HvAUX(hv);
2925 0 return iter->xhv_backreferences;
2926 } else {
2927 0 return NULL;
2928 }
2929 }
2930 #endif
2931  
2932 /* Build diagnostics from the referent's type and stash, never from overloaded
2933 * stringification. Keep the class name's UTF-8 flag for warn/croak. */
2934 SRL_STATIC_INLINE SV *
2935 0 srl_unsupported_type_name(pTHX_ SV *src)
2936 {
2937 0 SV *name = sv_2mortal(newSVpvs(""));
2938 0 ▶ if (SvOBJECT(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(src)) { 0 0 0
2939 0 sv_setpv(name, sv_reftype(src, 1));
2940 #if PERL_VERSION >= 16
2941 0 ▶ if (HvNAMEUTF8(SvSTASH(src)))
Branch: 7 branches
BranchTrueFalseTotal
if (HvNAMEUTF8(SvSTASH(src))) 0 0 0
if (HvNAMEUTF8(SvSTASH(src))) 0 0 0
if (HvNAMEUTF8(SvSTASH(src))) 0 0 0
if (HvNAMEUTF8(SvSTASH(src))) 0 0 0
if (HvNAMEUTF8(SvSTASH(src))) 0 0 0
if (HvNAMEUTF8(SvSTASH(src))) 0 0 0
if (HvNAMEUTF8(SvSTASH(src))) 0 0 0
2942 0 SvUTF8_on(name);
2943 #endif
2944 0 sv_catpvs(name, "=");
2945 }
2946 0 sv_catpv(name, sv_reftype(src, 0));
2947 0 return name;
2948 }
2949  
2950 static SRL_NOINLINE void
2951 0 srl_handle_unsupported_svtype(pTHX_ srl_encoder_t *enc, SV *src, svtype svt, SV *refsv, SSize_t ref_rewrite_pos, UV weak_rewrite_ofs)
2952 {
2953 0 ▶ if ( SRL_ENC_HAVE_OPTION(enc, SRL_F_UNDEF_UNKNOWN) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SRL_ENC_HAVE_OPTION(enc, SRL_F_UNDEF_UNKNOWN) ) { 0 0 0
2954 0 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_WARN_UNKNOWN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_WARN_UNKNOWN)) { 0 0 0
2955 0 srl_callback_begin(aTHX_ enc, src, refsv);
2956 0 warn("Found type %u %"SVf"(0x%p), but it is not representable "
2957 "by the Sereal encoding format; will encode as an "
2958 "undefined value", (svt), SVfARG(srl_unsupported_type_name(aTHX_ src)), (src));
2959 0 srl_callback_end(aTHX_ enc);
2960 }
2961 0 ▶ if (ref_rewrite_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref_rewrite_pos) { 0 0 0
2962 /* make sure we don't keep a reference to the thing that we do not
2963 * want to serialize around for REFP and ALIAS output */
2964 0 ▶ PTABLE_t *ref_seenhash= SRL_GET_REF_SEENHASH(enc);
Branch: 1 branch
BranchTrueFalseTotal
PTABLE_t *ref_seenhash= SRL_GET_REF_SEENHASH(enc); 0 0 0
2965 0 PTABLE_delete(ref_seenhash, src);
2966 0 ▶ if (weak_rewrite_ofs) {
Branch: 1 branch
BranchTrueFalseTotal
if (weak_rewrite_ofs) { 0 0 0
2967 0 srl_buffer_char *weak_pos= enc->buf.body_pos + weak_rewrite_ofs;
2968 0 *weak_pos= (srl_buffer_char)((*weak_pos & SRL_HDR_TRACK_FLAG) |
2969 SRL_HDR_PAD);
2970 0 ▶ PTABLE_delete(SRL_GET_WEAK_SEENHASH(enc), src);
Branch: 1 branch
BranchTrueFalseTotal
PTABLE_delete(SRL_GET_WEAK_SEENHASH(enc), src); 0 0 0
2971 }
2972 0 enc->buf.pos= enc->buf.body_pos + ref_rewrite_pos;
2973 }
2974 0 srl_buf_cat_char(&enc->buf, SRL_HDR_UNDEF);
2975 }
2976 0 ▶ else if ( SRL_ENC_HAVE_OPTION(enc, SRL_F_STRINGIFY_UNKNOWN) ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( SRL_ENC_HAVE_OPTION(enc, SRL_F_STRINGIFY_UNKNOWN) ) { 0 0 0
2977 STRLEN len;
2978 char *str;
2979 SV *stringified;
2980 SV *string_source;
2981 0 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_WARN_UNKNOWN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_WARN_UNKNOWN)) { 0 0 0
2982 /* In theory, we need to warn about stringifying this unsupported
2983 * item. However, if the SRL_F_NOWARN_UNKNOWN_OVERLOAD option is set,
2984 * then we DO NOT warn about stringifying this unsupported item if
2985 * it is an object with string overloading (assuming it's done on
2986 * purpose to stringify in cases like these).
2987 */
2988 0 ▶ if (!SRL_ENC_HAVE_OPTION(enc, SRL_F_NOWARN_UNKNOWN_OVERLOAD)
Branch: 1 branch
BranchTrueFalseTotal
if (!SRL_ENC_HAVE_OPTION(enc, SRL_F_NOWARN_UNKNOWN_OVERLOAD) 0 0 0
2989 0 ▶ || !SvOBJECT(src)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvOBJECT(src) 0 0 0
2990 0 ▶ || !Gv_AMG(SvSTASH(src)))
Branch: 8 branches
BranchTrueFalseTotal
|| !Gv_AMG(SvSTASH(src))) 0 0 0
|| !Gv_AMG(SvSTASH(src))) 0 0 0
|| !Gv_AMG(SvSTASH(src))) 0 0 0
|| !Gv_AMG(SvSTASH(src))) 0 0 0
|| !Gv_AMG(SvSTASH(src))) 0 0 0
|| !Gv_AMG(SvSTASH(src))) 0 0 0
|| !Gv_AMG(SvSTASH(src))) 0 0 0
|| !Gv_AMG(SvSTASH(src))) 0 0 0
2991 {
2992 0 srl_callback_begin(aTHX_ enc, src, refsv);
2993 0 warn("Found type %u %"SVf"(0x%p), but it is not representable "
2994 "by the Sereal encoding format; will encode as a "
2995 "stringified form", (svt), SVfARG(srl_unsupported_type_name(aTHX_ src)), (src));
2996 0 srl_callback_end(aTHX_ enc);
2997 }
2998 }
2999 0 ▶ if (ref_rewrite_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref_rewrite_pos) { 0 0 0
3000 /* make sure we don't keep a reference to the thing that we do not
3001 * want to serialize around for REFP and ALIAS output */
3002 0 ▶ PTABLE_t *ref_seenhash= SRL_GET_REF_SEENHASH(enc);
Branch: 1 branch
BranchTrueFalseTotal
PTABLE_t *ref_seenhash= SRL_GET_REF_SEENHASH(enc); 0 0 0
3003 0 PTABLE_delete(ref_seenhash, src);
3004 0 ▶ if (weak_rewrite_ofs) {
Branch: 1 branch
BranchTrueFalseTotal
if (weak_rewrite_ofs) { 0 0 0
3005 0 srl_buffer_char *weak_pos= enc->buf.body_pos + weak_rewrite_ofs;
3006 0 *weak_pos= (srl_buffer_char)((*weak_pos & SRL_HDR_TRACK_FLAG) |
3007 SRL_HDR_PAD);
3008 0 ▶ PTABLE_delete(SRL_GET_WEAK_SEENHASH(enc), src);
Branch: 1 branch
BranchTrueFalseTotal
PTABLE_delete(SRL_GET_WEAK_SEENHASH(enc), src); 0 0 0
3009 }
3010 0 enc->buf.pos= enc->buf.body_pos + ref_rewrite_pos;
3011 }
3012 0 stringified = sv_2mortal(newSV(0));
3013 0 srl_callback_begin(aTHX_ enc, src, refsv);
3014 0 ▶ string_source = sv_2mortal(newSVsv_flags(ref_rewrite_pos ? refsv : src, 0));
Branch: 1 branch
BranchTrueFalseTotal
string_source = sv_2mortal(newSVsv_flags(ref_rewrite_pos ? refsv : src, 0)); 0 0 0
3015 0 sv_copypv(stringified, string_source);
3016 0 srl_callback_end(aTHX_ enc);
3017 0 str = SvPV_nomg(stringified, len);
3018 0 srl_dump_pv(aTHX_ enc, str, len, SvUTF8(stringified));
3019 }
3020 else {
3021 0 croak("Found type %u %"SVf"(0x%p), but it is not representable "
3022 "by the Sereal encoding format", (svt), SVfARG(srl_unsupported_type_name(aTHX_ src)), (src));
3023 }
3024 0 }
3025  
3026 /* Dumps generic SVs and delegates
3027 * to more specialized functions for RVs, etc. */
3028 /* TODO decide when to use the IV, when to use the PV, and when
3029 * to use the NV slots of the SV.
3030 * Safest simple solution seems "prefer string" (fuck dualvars).
3031 * Potentially better but slower: If we would choose the string,
3032 * then try int-to-string (respective float-to-string) conversion
3033 * and strcmp. If same, then use int or float.
3034 */
3035 static void
3036 71 srl_dump_sv(pTHX_ srl_encoder_t *enc, SV *src)
3037 {
3038 UV refcount;
3039 svtype svt;
3040 MAGIC *mg;
3041 AV *backrefs;
3042 71 SV* refsv= NULL;
3043 71 SV* replacement= NULL;
3044 71 UV weakref_ofs= 0; /* preserved between loops */
3045 71 UV weak_rewrite_ofs= 0; /* WEAKEN tag for the current RV traversal */
3046 71 SSize_t ref_rewrite_pos= 0; /* preserved between loops - note SSize_t is a perl define */
3047 assert(src);
3048  
3049 71 ▶ if (expect_false( ++enc->recursion_depth == enc->max_recursion_depth )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( ++enc->recursion_depth == enc->max_recursion_depth )) { 71 0 71
3050 0 croak("Hit maximum recursion depth (%"UVuf"), aborting serialization",
3051 (UV)enc->max_recursion_depth);
3052 }
3053  
3054 71 redo_dump:
3055 91 mg= NULL;
3056 91 backrefs= NULL;
3057 91 svt = SvTYPE(src);
3058 91 refcount = SvREFCNT(src);
3059 DEBUG_ASSERT_BUF_SANE(&enc->buf);
3060 91 ▶ if ( SvMAGICAL(src) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvMAGICAL(src) ) { 64 27 91
3061 /* An aliased magical value already has its wire representation.
3062 * Do not FETCH it again merely to emit an ALIAS/REFP to that value. */
3063 64 ▶ if (SvGMAGICAL(src) && enc->ref_seenhash) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvGMAGICAL(src) && enc->ref_seenhash) { 51 13 64
if (SvGMAGICAL(src) && enc->ref_seenhash) { 51 0 51
3064 51 const ptrdiff_t offset = (ptrdiff_t)PTABLE_fetch(enc->ref_seenhash, src);
3065 51 ▶ if (offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset) { 0 51 51
3066 0 ▶ if (ref_rewrite_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref_rewrite_pos) { 0 0 0
3067 0 enc->buf.pos = enc->buf.body_pos + ref_rewrite_pos;
3068 0 srl_buf_cat_varint(aTHX_ &enc->buf, SRL_HDR_REFP, (UV)offset);
3069 } else {
3070 0 srl_buf_cat_varint(aTHX_ &enc->buf, SRL_HDR_ALIAS, (UV)offset);
3071 }
3072 0 SRL_SET_TRACK_FLAG(*(enc->buf.body_pos + offset));
3073 0 --enc->recursion_depth;
3074 0 return;
3075 }
3076 }
3077 64 srl_getmagic(aTHX_ enc, src, refsv);
3078 64 svt = SvTYPE(src);
3079 #ifdef HAS_HV_BACKREFS
3080 64 ▶ if (svt != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (svt != SVt_PVHV) 64 0 64
3081 #endif
3082 64 mg = mg_find(src, PERL_MAGIC_backref);
3083 }
3084 #ifdef HAS_HV_BACKREFS
3085 91 ▶ if (expect_false( svt == SVt_PVHV && SvOOK(src) )) {
Branch: 6 branches
BranchTrueFalseTotal
if (expect_false( svt == SVt_PVHV && SvOOK(src) )) { 0 91 91
if (expect_false( svt == SVt_PVHV && SvOOK(src) )) { 0 0 0
if (expect_false( svt == SVt_PVHV && SvOOK(src) )) { 0 0 0
if (expect_false( svt == SVt_PVHV && SvOOK(src) )) { 0 0 0
if (expect_false( svt == SVt_PVHV && SvOOK(src) )) { 0 0 0
if (expect_false( svt == SVt_PVHV && SvOOK(src) )) { 0 91 91
3086 0 backrefs= srl_hv_backreferences_p_safe(aTHX_ (HV *)src);
3087 if (DEBUGHACK) warn("backreferences %p", src);
3088 }
3089 #endif
3090 91 ▶ if (expect_false( mg || backrefs )) {
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( mg || backrefs )) { 91 0 91
if (expect_false( mg || backrefs )) { 0 91 91
if (expect_false( mg || backrefs )) { 0 91 91
3091 0 ▶ PTABLE_t *weak_seenhash= SRL_GET_WEAK_SEENHASH(enc);
Branch: 1 branch
BranchTrueFalseTotal
PTABLE_t *weak_seenhash= SRL_GET_WEAK_SEENHASH(enc); 0 0 0
3092 0 PTABLE_ENTRY_t *pe= PTABLE_find(weak_seenhash, src);
3093 0 ▶ if (!pe) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pe) { 0 0 0
3094 /* not seen it before */
3095 if (DEBUGHACK) warn("scalar %p - is weak referent, storing %"UVuf, src, weakref_ofs);
3096 /* if weakref_ofs is false we got here some way that holds a refcount on this item */
3097 0 PTABLE_store(weak_seenhash, src, INT2PTR(void *, weakref_ofs));
3098 } else {
3099 if (DEBUGHACK) warn("scalar %p - is weak referent, seen before value:%"UVuf" weakref_ofs:%"UVuf,
3100 src, (UV)pe->value, (UV)weakref_ofs);
3101 0 ▶ if (pe->value)
Branch: 1 branch
BranchTrueFalseTotal
if (pe->value) 0 0 0
3102 0 pe->value= INT2PTR(void *, weakref_ofs);
3103 }
3104 0 refcount++;
3105 0 weakref_ofs= 0;
3106 }
3107  
3108 /* check if we have seen this scalar before, and track it so
3109 * if we see it again we recognize it */
3110 91 ▶ if ( expect_false( refcount > 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( expect_false( refcount > 1 ) ) { 13 78 91
3111 13 ▶ if (src == &PL_sv_undef && enc->protocol_version >=3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (src == &PL_sv_undef && enc->protocol_version >=3 ) { 0 13 13
if (src == &PL_sv_undef && enc->protocol_version >=3 ) { 0 0 0
3112 0 srl_buf_cat_char(&enc->buf, SRL_HDR_CANONICAL_UNDEF);
3113 0 --enc->recursion_depth;
3114 0 return;
3115 }
3116 else
3117 13 ▶ if (src == &PL_sv_yes) {
Branch: 1 branch
BranchTrueFalseTotal
if (src == &PL_sv_yes) { 0 13 13
3118 0 srl_buf_cat_char(&enc->buf, SRL_HDR_TRUE);
3119 0 --enc->recursion_depth;
3120 0 return;
3121 }
3122 else
3123 13 ▶ if (src == &PL_sv_no) {
Branch: 1 branch
BranchTrueFalseTotal
if (src == &PL_sv_no) { 0 13 13
3124 0 srl_buf_cat_char(&enc->buf, SRL_HDR_FALSE);
3125 0 --enc->recursion_depth;
3126 0 return;
3127 }
3128 else {
3129 13 ▶ PTABLE_t *ref_seenhash= SRL_GET_REF_SEENHASH(enc);
Branch: 1 branch
BranchTrueFalseTotal
PTABLE_t *ref_seenhash= SRL_GET_REF_SEENHASH(enc); 7 6 13
3130 13 const ptrdiff_t oldoffset = (ptrdiff_t)PTABLE_fetch(ref_seenhash, src);
3131 13 ▶ if (expect_false(oldoffset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(oldoffset)) { 0 13 13
3132 /* we have seen it before, so we do not need to bless it again */
3133 0 ▶ if (ref_rewrite_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref_rewrite_pos) { 0 0 0
3134 if (DEBUGHACK) warn("ref to %p as %"UVuf, src, (UV)oldoffset);
3135 0 enc->buf.pos= enc->buf.body_pos + ref_rewrite_pos;
3136 0 srl_buf_cat_varint(aTHX_ &enc->buf, SRL_HDR_REFP, (UV)oldoffset);
3137 } else {
3138 if (DEBUGHACK) warn("alias to %p as %"UVuf, src, (UV)oldoffset);
3139 0 srl_buf_cat_varint(aTHX_ &enc->buf, SRL_HDR_ALIAS, (UV)oldoffset);
3140 }
3141 0 SRL_SET_TRACK_FLAG(*(enc->buf.body_pos + oldoffset));
3142 0 --enc->recursion_depth;
3143 0 return;
3144 }
3145 if (DEBUGHACK) warn("storing %p as %"UVuf, src, (UV)BODY_POS_OFS(&enc->buf));
3146 13 PTABLE_store(ref_seenhash, src, INT2PTR(void *, BODY_POS_OFS(&enc->buf)));
3147 13 ▶ if (SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS))
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPER_FLAG(enc, SRL_OF_ENCODER_HAS_CALLBACKS)) 6 7 13
3148 6 srl_callback_hold(aTHX_ enc, src);
3149 }
3150 }
3151  
3152 91 ▶ if (expect_false( weakref_ofs != 0 )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( weakref_ofs != 0 )) { 0 91 91
3153 0 sv_dump(src);
3154 0 croak("Corrupted weakref? weakref_ofs should be 0, but got %"UVuf" (this should not happen)", weakref_ofs);
3155 }
3156  
3157 91 ▶ if (replacement) {
Branch: 1 branch
BranchTrueFalseTotal
if (replacement) { 0 91 91
3158 0 ▶ if (SvROK(replacement)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(replacement)) { 0 0 0
3159 0 src= SvRV(replacement);
3160 } else {
3161 0 src= replacement;
3162 }
3163 0 replacement= NULL;
3164 0 svt = SvTYPE(src);
3165 /* plus one ensures that later on we get REFN/ARRAY and not ARRAYREF - This is horrible tho. needs to be revisited another day */
3166 0 refcount= SvREFCNT(src) + 1;
3167 /* We could, but do not do the following:*/
3168 /* goto redo_dump; */
3169 /* Probably a "proper" solution would, but there are nits there that I dont want to chase right now. */
3170 }
3171  
3172 /* --------------------------------- */
3173 91 ▶ if (enc->protocol_version >= 6 && SvVOK(src)) {
Branch: 3 branches
BranchTrueFalseTotal
if (enc->protocol_version >= 6 && SvVOK(src)) { 21 70 91
if (enc->protocol_version >= 6 && SvVOK(src)) { 14 7 21
if (enc->protocol_version >= 6 && SvVOK(src)) { 0 14 14
3174 0 srl_dump_vstring(aTHX_ enc, src, !ref_rewrite_pos);
3175 }
3176 91 ▶ else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos)
Branch: 45 branches
BranchTrueFalseTotal
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 3 88 91
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 3 0 3
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 3 3
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 3 3
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 3 3
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 48 40 88
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 40 40
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 40 40
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 0 0
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 40 40
else _SRL_IF_SIMPLE_DIRECT_DUMP_SV(enc, src, svt, !ref_rewrite_pos) 0 40 40
3177 else
3178 #if defined(MODERN_REGEXP) && defined(REGEXP_NO_LONGER_POK)
3179 /* Only need to enter here if we have rather modern regexps AND they're
3180 * NO LONGER POK (5.17.6 and up). */
3181 if ( expect_false( svt == SVt_REGEXP ) ) {
3182 srl_dump_regexp(aTHX_ enc, src);
3183 }
3184 else
3185 #endif
3186 40 ▶ if (SvROK(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(src)) { 20 20 40
3187 /* dump references */
3188 20 SV *referent= SvRV(src);
3189 20 weak_rewrite_ofs= 0;
3190 /* assert()-like hack to be compiled out by default */
3191 #ifndef NDEBUG
3192 if (!referent) {
3193 sv_dump(src);
3194 assert(referent);
3195 }
3196 #endif
3197 20 ▶ if (expect_false( SvWEAKREF(src) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( SvWEAKREF(src) )) { 0 20 20
3198 if (DEBUGHACK) warn("Is weakref %p", src);
3199 0 weakref_ofs= BODY_POS_OFS(&enc->buf);
3200 0 weak_rewrite_ofs= weakref_ofs;
3201 0 srl_buf_cat_char(&enc->buf, SRL_HDR_WEAKEN);
3202 }
3203  
3204 20 ref_rewrite_pos= BODY_POS_OFS(&enc->buf);
3205  
3206 20 ▶ if ( expect_false( SvOBJECT(referent) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( expect_false( SvOBJECT(referent) ) ) { 0 20 20
3207 0 HV *class_stash = SvSTASH(referent);
3208 0 int refused_regexp = SRL_ENC_HAVE_OPTION(enc, SRL_F_REFUSE_OBJECTS)
3209 0 ▶ && srl_is_canonical_regexp_object(aTHX_ referent);
Branch: 2 branches
BranchTrueFalseTotal
&& srl_is_canonical_regexp_object(aTHX_ referent); 0 0 0
&& srl_is_canonical_regexp_object(aTHX_ referent); 0 0 0
3210 0 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_CROAK_ON_BLESS))
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_CROAK_ON_BLESS)) 0 0 0
3211 0 croak("Attempted to serialize blessed reference. Serializing objects "
3212 "using Sereal::Encoder was explicitly disabled using the "
3213 "'croak_on_bless' option.");
3214 0 ▶ if (SRL_ENC_HAVE_OPTION(enc, SRL_F_REFUSE_OBJECTS) && !refused_regexp)
Branch: 2 branches
BranchTrueFalseTotal
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_REFUSE_OBJECTS) && !refused_regexp) 0 0 0
if (SRL_ENC_HAVE_OPTION(enc, SRL_F_REFUSE_OBJECTS) && !refused_regexp) 0 0 0
3215 0 croak("Attempted to serialize object while refuse_objects is enabled");
3216 0 ▶ if (!SRL_ENC_HAVE_OPTION(enc, SRL_F_NO_BLESS_OBJECTS)
Branch: 1 branch
BranchTrueFalseTotal
if (!SRL_ENC_HAVE_OPTION(enc, SRL_F_NO_BLESS_OBJECTS) 0 0 0
3217 0 ▶ && !srl_class_allowed(aTHX_ enc, enc->allow_hash,
Branch: 1 branch
BranchTrueFalseTotal
&& !srl_class_allowed(aTHX_ enc, enc->allow_hash, 0 0 0
3218 enc->allow_cb, class_stash, src, referent, 0))
3219 0 ▶ croak("Object class '%s' is not in allow_classes", HvNAME(class_stash));
Branch: 6 branches
BranchTrueFalseTotal
croak("Object class '%s' is not in allow_classes", HvNAME(class_stash)); 0 0 0
croak("Object class '%s' is not in allow_classes", HvNAME(class_stash)); 0 0 0
croak("Object class '%s' is not in allow_classes", HvNAME(class_stash)); 0 0 0
croak("Object class '%s' is not in allow_classes", HvNAME(class_stash)); 0 0 0
croak("Object class '%s' is not in allow_classes", HvNAME(class_stash)); 0 0 0
croak("Object class '%s' is not in allow_classes", HvNAME(class_stash)); 0 0 0
3220 0 ▶ if ((enc->allow_hash || enc->allow_cb)
Branch: 2 branches
BranchTrueFalseTotal
if ((enc->allow_hash || enc->allow_cb) 0 0 0
if ((enc->allow_hash || enc->allow_cb) 0 0 0
3221 0 ▶ && (!SvROK(src) || SvRV(src) != referent
Branch: 2 branches
BranchTrueFalseTotal
&& (!SvROK(src) || SvRV(src) != referent 0 0 0
&& (!SvROK(src) || SvRV(src) != referent 0 0 0
3222 0 ▶ || SvSTASH(referent) != class_stash))
Branch: 1 branch
BranchTrueFalseTotal
|| SvSTASH(referent) != class_stash)) 0 0 0
3223 0 croak("Object changed during allow_classes callback");
3224 /* Write bless operator with class name */
3225 /* An exempt regexp must stay a REGEXP, even if its package has
3226 * acquired a FREEZE method. */
3227 0 ▶ replacement= refused_regexp ? NULL :
Branch: 1 branch
BranchTrueFalseTotal
replacement= refused_regexp ? NULL : 0 0 0
3228 0 srl_get_frozen_object(aTHX_ enc, src, referent);
3229 0 ▶ if ((enc->allow_hash || enc->allow_cb)
Branch: 2 branches
BranchTrueFalseTotal
if ((enc->allow_hash || enc->allow_cb) 0 0 0
if ((enc->allow_hash || enc->allow_cb) 0 0 0
3230 0 ▶ && (!SvROK(src) || SvRV(src) != referent
Branch: 2 branches
BranchTrueFalseTotal
&& (!SvROK(src) || SvRV(src) != referent 0 0 0
&& (!SvROK(src) || SvRV(src) != referent 0 0 0
3231 0 ▶ || SvSTASH(referent) != class_stash))
Branch: 1 branch
BranchTrueFalseTotal
|| SvSTASH(referent) != class_stash)) 0 0 0
3232 0 croak("Object changed during FREEZE callback");
3233 0 ▶ if (srl_dump_classname(aTHX_ enc, referent, replacement)) {
Branch: 1 branch
BranchTrueFalseTotal
if (srl_dump_classname(aTHX_ enc, referent, replacement)) { 0 0 0
3234 /* 1 means we should not rewrite away the classname */
3235 0 ref_rewrite_pos= BODY_POS_OFS(&enc->buf);
3236 }
3237 }
3238  
3239 20 srl_buf_cat_char(&enc->buf, SRL_HDR_REFN);
3240 20 refsv= src;
3241 20 src= referent;
3242  
3243 if (DEBUGHACK) warn("Going to redo %p", src);
3244 20 goto redo_dump;
3245 }
3246 else
3247 #ifndef MODERN_REGEXP
3248 if (
3249 svt == SVt_PVMG &&
3250 ((SvFLAGS(src) & (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) == (SVs_OBJECT|BFD_Svs_SMG_OR_RMG)) &&
3251 (mg = mg_find(src, PERL_MAGIC_qr))
3252 ) {
3253 /* Houston, we have a regex! */
3254 srl_dump_regexp(aTHX_ enc, (SV*)mg); /* yes the SV* cast makes me feel dirty too */
3255 }
3256 else
3257 #endif
3258 20 ▶ if (svt == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (svt == SVt_PVHV) { 0 20 20
3259 0 srl_dump_hv(aTHX_ enc, (HV *)src, refcount);
3260 }
3261 else
3262 20 ▶ if (svt == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (svt == SVt_PVAV) { 20 0 20
3263 20 srl_dump_av(aTHX_ enc, (AV *)src, refcount);
3264 }
3265 else
3266 0 ▶ if ( ! SvOK(src) ) { /* undef and weird shit */
Branch: 1 branch
BranchTrueFalseTotal
if ( ! SvOK(src) ) { /* undef and weird shit */ 0 0 0
3267 0 ▶ if ( SRL_UNSUPPORTED_SvTYPE(svt) ) {
Branch: 4 branches
BranchTrueFalseTotal
if ( SRL_UNSUPPORTED_SvTYPE(svt) ) { 0 0 0
if ( SRL_UNSUPPORTED_SvTYPE(svt) ) { 0 0 0
if ( SRL_UNSUPPORTED_SvTYPE(svt) ) { 0 0 0
if ( SRL_UNSUPPORTED_SvTYPE(svt) ) { 0 0 0
3268 /* we exclude magic, because magic sv's can be undef too */
3269 /* called when we find an unsupported type/reference. May either throw exception
3270 * or write ONE (nested or single) item to the buffer. */
3271 0 srl_handle_unsupported_svtype(aTHX_ enc, src, svt, refsv, ref_rewrite_pos, weak_rewrite_ofs);
3272 }
3273 0 ▶ else if (src == &PL_sv_undef && enc->protocol_version >= 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
else if (src == &PL_sv_undef && enc->protocol_version >= 3 ) { 0 0 0
else if (src == &PL_sv_undef && enc->protocol_version >= 3 ) { 0 0 0
3274 0 srl_buf_cat_char(&enc->buf, SRL_HDR_CANONICAL_UNDEF);
3275 } else {
3276 0 srl_buf_cat_char(&enc->buf, SRL_HDR_UNDEF);
3277 }
3278 }
3279 else {
3280 0 srl_handle_unsupported_svtype(aTHX_ enc, src, svt, refsv, ref_rewrite_pos, weak_rewrite_ofs);
3281 }
3282 71 --enc->recursion_depth;
3283 }
« srl_compress.h ↑ Summary srl_checksum_policy.h »