srl_encoder.c

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