1 /* -*- c-basic-offset: 4 -*-
3 * Fast store and retrieve mechanism.
5 * Copyright (c) 1995-2000, Raphael Manfredi
6 * Copyright (c) 2016, 2017 cPanel Inc
7 * Copyright (c) 2017 Reini Urban
9 * You may redistribute only under the same terms as Perl 5, as specified
10 * in the README file that comes with the distribution.
14 #define PERL_NO_GET_CONTEXT /* we want efficiency */
20 #include <patchlevel.h> /* Perl's one, needed since 5.6 */
23 #if !defined(PERL_VERSION) || PERL_VERSION < 10 || (PERL_VERSION == 10 && PERL_SUBVERSION < 1)
24 #define NEED_PL_parser
25 #define NEED_sv_2pv_flags
26 #define NEED_load_module
27 #define NEED_vload_module
28 #define NEED_newCONSTSUB
29 #define NEED_newSVpvn_flags
30 #define NEED_newRV_noinc
31 #include "ppport.h" /* handle old perls */
35 #define DEBUGME /* Debug mode, turns assertions on as well */
36 #define DASSERT /* Assertion mode */
40 * Pre PerlIO time when none of USE_PERLIO and PERLIO_IS_STDIO is defined
41 * Provide them with the necessary defines so they can build with pre-5.004.
44 #ifndef PERLIO_IS_STDIO
46 #define PerlIO_getc(x) getc(x)
47 #define PerlIO_putc(f,x) putc(x,f)
48 #define PerlIO_read(x,y,z) fread(y,1,z,x)
49 #define PerlIO_write(x,y,z) fwrite(y,1,z,x)
50 #define PerlIO_stdoutf printf
51 #endif /* PERLIO_IS_STDIO */
52 #endif /* USE_PERLIO */
55 * Earlier versions of perl might be used, we can't assume they have the latest!
58 #ifndef HvSHAREKEYS_off
59 #define HvSHAREKEYS_off(hv) /* Ignore */
62 /* perl <= 5.8.2 needs this */
64 # define SvIsCOW(sv) 0
68 # define HvRITER_set(hv,r) (HvRITER(hv) = r)
71 # define HvEITER_set(hv,r) (HvEITER(hv) = r)
75 # define HvRITER_get HvRITER
78 # define HvEITER_get HvEITER
81 #ifndef HvPLACEHOLDERS_get
82 # define HvPLACEHOLDERS_get HvPLACEHOLDERS
86 # define HvTOTALKEYS(hv) HvKEYS(hv)
90 # define HvUSEDKEYS(hv) HvKEYS(hv)
94 # define SvTRULYREADONLY(sv) SvREADONLY(sv)
96 # define SvTRULYREADONLY(sv) (SvREADONLY(sv) && !SvIsCOW(sv))
100 # define SvPVCLEAR(sv) sv_setpvs(sv, "")
104 # define strEQc(s,c) memEQ(s, ("" c ""), sizeof(c))
114 * TRACEME() will only output things when the $Storable::DEBUGME is true,
115 * using the value traceme cached in the context.
118 * TRACEMED() directly looks at the variable, for use before traceme has been
125 { PerlIO_stdoutf x; PerlIO_stdoutf("\n"); } \
128 #define TRACEMED(x) \
130 if (SvTRUE(get_sv("Storable::DEBUGME", GV_ADD))) \
131 { PerlIO_stdoutf x; PerlIO_stdoutf("\n"); } \
134 #define INIT_TRACEME \
136 cxt->traceme = SvTRUE(get_sv("Storable::DEBUGME", GV_ADD)); \
146 #define ASSERT(x,y) \
149 PerlIO_stdoutf("ASSERT FAILED (\"%s\", line %d): ", \
150 __FILE__, (int)__LINE__); \
151 PerlIO_stdoutf y; PerlIO_stdoutf("\n"); \
162 #define C(x) ((char) (x)) /* For markers with dynamic retrieval handling */
164 #define SX_OBJECT C(0) /* Already stored object */
165 #define SX_LSCALAR C(1) /* Scalar (large binary) follows (length, data) */
166 #define SX_ARRAY C(2) /* Array forthcoming (size, item list) */
167 #define SX_HASH C(3) /* Hash forthcoming (size, key/value pair list) */
168 #define SX_REF C(4) /* Reference to object forthcoming */
169 #define SX_UNDEF C(5) /* Undefined scalar */
170 #define SX_INTEGER C(6) /* Integer forthcoming */
171 #define SX_DOUBLE C(7) /* Double forthcoming */
172 #define SX_BYTE C(8) /* (signed) byte forthcoming */
173 #define SX_NETINT C(9) /* Integer in network order forthcoming */
174 #define SX_SCALAR C(10) /* Scalar (binary, small) follows (length, data) */
175 #define SX_TIED_ARRAY C(11) /* Tied array forthcoming */
176 #define SX_TIED_HASH C(12) /* Tied hash forthcoming */
177 #define SX_TIED_SCALAR C(13) /* Tied scalar forthcoming */
178 #define SX_SV_UNDEF C(14) /* Perl's immortal PL_sv_undef */
179 #define SX_SV_YES C(15) /* Perl's immortal PL_sv_yes */
180 #define SX_SV_NO C(16) /* Perl's immortal PL_sv_no */
181 #define SX_BLESS C(17) /* Object is blessed */
182 #define SX_IX_BLESS C(18) /* Object is blessed, classname given by index */
183 #define SX_HOOK C(19) /* Stored via hook, user-defined */
184 #define SX_OVERLOAD C(20) /* Overloaded reference */
185 #define SX_TIED_KEY C(21) /* Tied magic key forthcoming */
186 #define SX_TIED_IDX C(22) /* Tied magic index forthcoming */
187 #define SX_UTF8STR C(23) /* UTF-8 string forthcoming (small) */
188 #define SX_LUTF8STR C(24) /* UTF-8 string forthcoming (large) */
189 #define SX_FLAG_HASH C(25) /* Hash with flags forthcoming (size, flags, key/flags/value triplet list) */
190 #define SX_CODE C(26) /* Code references as perl source code */
191 #define SX_WEAKREF C(27) /* Weak reference to object forthcoming */
192 #define SX_WEAKOVERLOAD C(28) /* Overloaded weak reference */
193 #define SX_VSTRING C(29) /* vstring forthcoming (small) */
194 #define SX_LVSTRING C(30) /* vstring forthcoming (large) */
195 #define SX_SVUNDEF_ELEM C(31) /* array element set to &PL_sv_undef */
196 #define SX_REGEXP C(32) /* Regexp */
197 #define SX_LOBJECT C(33) /* Large object: string, array or hash (size >2G) */
198 #define SX_LAST C(34) /* invalid. marker only */
201 * Those are only used to retrieve "old" pre-0.6 binary images.
203 #define SX_ITEM 'i' /* An array item introducer */
204 #define SX_IT_UNDEF 'I' /* Undefined array item */
205 #define SX_KEY 'k' /* A hash key introducer */
206 #define SX_VALUE 'v' /* A hash value introducer */
207 #define SX_VL_UNDEF 'V' /* Undefined hash value */
210 * Those are only used to retrieve "old" pre-0.7 binary images
213 #define SX_CLASS 'b' /* Object is blessed, class name length <255 */
214 #define SX_LG_CLASS 'B' /* Object is blessed, class name length >255 */
215 #define SX_STORED 'X' /* End of object */
218 * Limits between short/long length representation.
221 #define LG_SCALAR 255 /* Large scalar length limit */
222 #define LG_BLESS 127 /* Large classname bless limit */
228 #define ST_STORE 0x1 /* Store operation */
229 #define ST_RETRIEVE 0x2 /* Retrieval operation */
230 #define ST_CLONE 0x4 /* Deep cloning operation */
233 * The following structure is used for hash table key retrieval. Since, when
234 * retrieving objects, we'll be facing blessed hash references, it's best
235 * to pre-allocate that buffer once and resize it as the need arises, never
236 * freeing it (keys will be saved away someplace else anyway, so even large
237 * keys are not enough a motivation to reclaim that space).
239 * This structure is also used for memory store/retrieve operations which
240 * happen in a fixed place before being malloc'ed elsewhere if persistence
241 * is required. Hence the aptr pointer.
244 char *arena; /* Will hold hash key strings, resized as needed */
245 STRLEN asiz; /* Size of aforementioned buffer */
246 char *aptr; /* Arena pointer, for in-place read/write ops */
247 char *aend; /* First invalid address */
252 * A hash table records the objects which have already been stored.
253 * Those are referred to as SX_OBJECT in the file, and their "tag" (i.e.
254 * an arbitrary sequence number) is used to identify them.
257 * An array table records the objects which have already been retrieved,
258 * as seen by the tag determined by counting the objects themselves. The
259 * reference to that retrieved object is kept in the table, and is returned
260 * when an SX_OBJECT is found bearing that same tag.
262 * The same processing is used to record "classname" for blessed objects:
263 * indexing by a hash at store time, and via an array at retrieve time.
266 typedef unsigned long stag_t; /* Used by pre-0.6 binary format */
269 * Make the tag type 64-bit on 64-bit platforms.
271 * If the tag number is low enough it's stored as a 32-bit value, but
272 * with very large arrays and hashes it's possible to go over 2**32
276 typedef STRLEN ntag_t;
278 /* used for where_is_undef - marks an unset value */
279 #define UNSET_NTAG_T (~(ntag_t)0)
282 * The following "thread-safe" related defines were contributed by
283 * Murray Nesbitt <murray@activestate.com> and integrated by RAM, who
284 * only renamed things a little bit to ensure consistency with surrounding
285 * code. -- RAM, 14/09/1999
287 * The original patch suffered from the fact that the stcxt_t structure
288 * was global. Murray tried to minimize the impact on the code as much as
291 * Starting with 0.7, Storable can be re-entrant, via the STORABLE_xxx hooks
292 * on objects. Therefore, the notion of context needs to be generalized,
296 #define MY_VERSION "Storable(" XS_VERSION ")"
300 * Conditional UTF8 support.
304 #define STORE_UTF8STR(pv, len) STORE_PV_LEN(pv, len, SX_UTF8STR, SX_LUTF8STR)
305 #define HAS_UTF8_SCALARS
307 #define HAS_UTF8_HASHES
310 /* 5.6 perl has utf8 scalars but not hashes */
314 #define STORE_UTF8STR(pv, len) CROAK(("panic: storing UTF8 in non-UTF8 perl"))
317 #define UTF8_CROAK() CROAK(("Cannot retrieve UTF8 data in non-UTF8 perl"))
320 #define WEAKREF_CROAK() CROAK(("Cannot retrieve weak references in this perl"))
323 #define VSTRING_CROAK() CROAK(("Cannot retrieve vstring in this perl"))
326 #ifdef HvPLACEHOLDERS
327 #define HAS_RESTRICTED_HASHES
329 #define HVhek_PLACEHOLD 0x200
330 #define RESTRICTED_HASH_CROAK() CROAK(("Cannot retrieve restricted hash"))
334 #define HAS_HASH_KEY_FLAGS
338 #define USE_PTR_TABLE
341 /* do we need/want to clear padding on NVs? */
342 #if defined(LONG_DOUBLEKIND) && defined(USE_LONG_DOUBLE)
343 # if LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN || \
344 LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
345 # define NV_PADDING (NVSIZE - 10)
347 # define NV_PADDING 0
350 /* This is kind of a guess - it means we'll get an unneeded clear on 128-bit NV
351 but an upgraded perl will fix that
356 # define NV_PADDING 0
361 U8 bytes[sizeof(NV)];
364 /* Needed for 32bit with lengths > 2G - 4G, and 64bit */
370 * Fields s_tainted and s_dirty are prefixed with s_ because Perl's include
371 * files remap tainted and dirty when threading is enabled. That's bad for
372 * perl to remap such common words. -- RAM, 29/09/00
376 typedef struct stcxt {
377 int entry; /* flags recursion */
378 int optype; /* type of traversal operation */
379 /* which objects have been seen, store time.
380 tags are numbers, which are cast to (SV *) and stored directly */
382 /* use pseen if we have ptr_tables. We have to store tag+1, because
383 tag numbers start at 0, and we can't store (SV *) 0 in a ptr_table
384 without it being confused for a fetch lookup failure. */
385 struct ptr_tbl *pseen;
386 /* Still need hseen for the 0.6 file format code. */
389 AV *hook_seen; /* which SVs were returned by STORABLE_freeze() */
390 AV *aseen; /* which objects have been seen, retrieve time */
391 ntag_t where_is_undef; /* index in aseen of PL_sv_undef */
392 HV *hclass; /* which classnames have been seen, store time */
393 AV *aclass; /* which classnames have been seen, retrieve time */
394 HV *hook; /* cache for hook methods per class name */
395 IV tagnum; /* incremented at store time for each seen object */
396 IV classnum; /* incremented at store time for each seen classname */
397 int netorder; /* true if network order used */
398 int s_tainted; /* true if input source is tainted, at retrieve time */
399 int forgive_me; /* whether to be forgiving... */
400 int deparse; /* whether to deparse code refs */
401 SV *eval; /* whether to eval source code */
402 int canonical; /* whether to store hashes sorted by key */
403 #ifndef HAS_RESTRICTED_HASHES
404 int derestrict; /* whether to downgrade restricted hashes */
407 int use_bytes; /* whether to bytes-ify utf8 */
409 int accept_future_minor; /* croak immediately on future minor versions? */
410 int s_dirty; /* context is dirty due to CROAK() -- can be cleaned */
411 int membuf_ro; /* true means membuf is read-only and msaved is rw */
412 struct extendable keybuf; /* for hash key retrieval */
413 struct extendable membuf; /* for memory store/retrieve operations */
414 struct extendable msaved; /* where potentially valid mbuf is saved */
415 PerlIO *fio; /* where I/O are performed, NULL for memory */
416 int ver_major; /* major of version for retrieved object */
417 int ver_minor; /* minor of version for retrieved object */
418 SV *(**retrieve_vtbl)(pTHX_ struct stcxt *, const char *); /* retrieve dispatch table */
419 SV *prev; /* contexts chained backwards in real recursion */
420 SV *my_sv; /* the blessed scalar who's SvPVX() I am */
424 A hashref of hashrefs or arrayref of arrayrefs is actually a
425 chain of four SVs, eg for an array ref containing an array ref:
427 RV -> AV (element) -> RV -> AV
429 To make this depth appear natural from a perl level we only
430 want to count this as two levels, so store_ref() stores it's RV
431 into recur_sv and store_array()/store_hash() will only count
432 that level if the AV/HV *isn't* recur_sv.
434 We can't just have store_hash()/store_array() not count that
435 level, since it's possible for XS code to store an AV or HV
436 directly as an element (though perl code trying to access such
437 an object will generally croak.)
439 SV *recur_sv; /* check only one recursive SV */
440 int in_retrieve_overloaded; /* performance hack for retrieving overloaded objects */
441 int flags; /* controls whether to bless or tie objects */
442 IV recur_depth; /* avoid stack overflows RT #97526 */
443 IV max_recur_depth; /* limit for recur_depth */
444 IV max_recur_depth_hash; /* limit for recur_depth for hashes */
446 int traceme; /* TRACEME() produces output */
450 #define RECURSION_TOO_DEEP() \
451 (cxt->max_recur_depth != -1 && ++cxt->recur_depth > cxt->max_recur_depth)
453 /* There's cases where we need to check whether the hash recursion
454 limit has been reached without bumping the recursion levels, so the
455 hash check doesn't bump the depth.
457 #define RECURSION_TOO_DEEP_HASH() \
458 (cxt->max_recur_depth_hash != -1 && cxt->recur_depth > cxt->max_recur_depth_hash)
459 #define MAX_DEPTH_ERROR "Max. recursion depth with nested structures exceeded"
461 static int storable_free(pTHX_ SV *sv, MAGIC* mg);
463 static MGVTBL vtbl_storable = {
480 /* From Digest::MD5. */
482 # define sv_magicext(sv, obj, type, vtbl, name, namlen) \
483 THX_sv_magicext(aTHX_ sv, obj, type, vtbl, name, namlen)
484 static MAGIC *THX_sv_magicext(pTHX_
485 SV *sv, SV *obj, int type,
486 MGVTBL const *vtbl, char const *name, I32 namlen)
490 /* exceeded intended usage of this reserve implementation */
493 mg->mg_virtual = (MGVTBL*)vtbl;
495 mg->mg_ptr = (char *)name;
497 (void) SvUPGRADE(sv, SVt_PVMG);
498 mg->mg_moremagic = SvMAGIC(sv);
506 #define NEW_STORABLE_CXT_OBJ(cxt) \
508 SV *self = newSV(sizeof(stcxt_t) - 1); \
509 SV *my_sv = newRV_noinc(self); \
510 sv_magicext(self, NULL, PERL_MAGIC_ext, &vtbl_storable, NULL, 0); \
511 cxt = (stcxt_t *)SvPVX(self); \
512 Zero(cxt, 1, stcxt_t); \
513 cxt->my_sv = my_sv; \
516 #if defined(MULTIPLICITY) || defined(PERL_OBJECT) || defined(PERL_CAPI)
518 #if (PATCHLEVEL <= 4) && (SUBVERSION < 68)
520 SV *perinterp_sv = get_sv(MY_VERSION, 0)
521 #else /* >= perl5.004_68 */
523 SV *perinterp_sv = *hv_fetch(PL_modglobal, \
524 MY_VERSION, sizeof(MY_VERSION)-1, TRUE)
525 #endif /* < perl5.004_68 */
527 #define dSTCXT_PTR(T,name) \
528 T name = ((perinterp_sv \
529 && SvIOK(perinterp_sv) && SvIVX(perinterp_sv) \
530 ? (T)SvPVX(SvRV(INT2PTR(SV*,SvIVX(perinterp_sv)))) : (T) 0))
533 dSTCXT_PTR(stcxt_t *, cxt)
537 NEW_STORABLE_CXT_OBJ(cxt); \
538 assert(perinterp_sv); \
539 sv_setiv(perinterp_sv, PTR2IV(cxt->my_sv))
541 #define SET_STCXT(x) \
544 sv_setiv(perinterp_sv, PTR2IV(x->my_sv)); \
547 #else /* !MULTIPLICITY && !PERL_OBJECT && !PERL_CAPI */
549 static stcxt_t *Context_ptr = NULL;
550 #define dSTCXT stcxt_t *cxt = Context_ptr
551 #define SET_STCXT(x) Context_ptr = x
554 NEW_STORABLE_CXT_OBJ(cxt); \
558 #endif /* MULTIPLICITY || PERL_OBJECT || PERL_CAPI */
562 * Croaking implies a memory leak, since we don't use setjmp/longjmp
563 * to catch the exit and free memory used during store or retrieve
564 * operations. This is not too difficult to fix, but I need to understand
565 * how Perl does it, and croaking is exceptional anyway, so I lack the
566 * motivation to do it.
568 * The current workaround is to mark the context as dirty when croaking,
569 * so that data structures can be freed whenever we renter Storable code
570 * (but only *then*: it's a workaround, not a fix).
572 * This is also imperfect, because we don't really know how far they trapped
573 * the croak(), and when we were recursing, we won't be able to clean anything
574 * but the topmost context stacked.
577 #define CROAK(x) STMT_START { cxt->s_dirty = 1; croak x; } STMT_END
580 * End of "thread-safe" related definitions.
586 * Keep only the low 32 bits of a pointer (used for tags, which are not
591 #define LOW_32BITS(x) ((I32) (x))
593 #define LOW_32BITS(x) ((I32) ((STRLEN) (x) & 0xffffffffUL))
599 * Convert a pointer into an ntag_t.
602 #define PTR2TAG(x) ((ntag_t)(x))
604 #define TAG2PTR(x, type) ((y)(x))
609 * Hack for Crays, where sizeof(I32) == 8, and which are big-endians.
610 * Used in the WLEN and RLEN macros.
614 #define oI(x) ((I32 *) ((char *) (x) + 4))
615 #define oS(x) ((x) - 4)
617 #define oC(x) (x = 0)
627 * key buffer handling
629 #define kbuf (cxt->keybuf).arena
630 #define ksiz (cxt->keybuf).asiz
634 TRACEME(("** allocating kbuf of 128 bytes")); \
635 New(10003, kbuf, 128, char); \
643 CROAK(("Too large size > I32_MAX")); \
644 TRACEME(("** extending kbuf to %d bytes (had %d)", \
645 (int)(x+1), (int)ksiz)); \
646 Renew(kbuf, x+1, char); \
652 * memory buffer handling
654 #define mbase (cxt->membuf).arena
655 #define msiz (cxt->membuf).asiz
656 #define mptr (cxt->membuf).aptr
657 #define mend (cxt->membuf).aend
659 #define MGROW (1 << 13)
660 #define MMASK (MGROW - 1)
662 #define round_mgrow(x) \
663 ((STRLEN) (((STRLEN) (x) + MMASK) & ~MMASK))
664 #define trunc_int(x) \
665 ((STRLEN) ((STRLEN) (x) & ~(sizeof(int)-1)))
666 #define int_aligned(x) \
667 ((STRLEN)(x) == trunc_int(x))
669 #define MBUF_INIT(x) \
672 TRACEME(("** allocating mbase of %d bytes", MGROW)); \
673 New(10003, mbase, (int)MGROW, char); \
674 msiz = (STRLEN)MGROW; \
680 mend = mbase + msiz; \
683 #define MBUF_TRUNC(x) mptr = mbase + x
684 #define MBUF_SIZE() (mptr - mbase)
690 * Those macros are used in do_retrieve() to save the current memory
691 * buffer into cxt->msaved, before MBUF_LOAD() can be used to retrieve
692 * data from a string.
694 #define MBUF_SAVE_AND_LOAD(in) \
696 ASSERT(!cxt->membuf_ro, ("mbase not already saved")); \
697 cxt->membuf_ro = 1; \
698 TRACEME(("saving mbuf")); \
699 StructCopy(&cxt->membuf, &cxt->msaved, struct extendable); \
703 #define MBUF_RESTORE() \
705 ASSERT(cxt->membuf_ro, ("mbase is read-only")); \
706 cxt->membuf_ro = 0; \
707 TRACEME(("restoring mbuf")); \
708 StructCopy(&cxt->msaved, &cxt->membuf, struct extendable); \
712 * Use SvPOKp(), because SvPOK() fails on tainted scalars.
713 * See store_scalar() for other usage of this workaround.
715 #define MBUF_LOAD(v) \
717 ASSERT(cxt->membuf_ro, ("mbase is read-only")); \
719 CROAK(("Not a scalar string")); \
720 mptr = mbase = SvPV(v, msiz); \
721 mend = mbase + msiz; \
724 #define MBUF_XTEND(x) \
726 STRLEN nsz = (STRLEN) round_mgrow((x)+msiz); \
727 STRLEN offset = mptr - mbase; \
728 ASSERT(!cxt->membuf_ro, ("mbase is not read-only")); \
729 TRACEME(("** extending mbase from %ld to %ld bytes (wants %ld new)", \
730 (long)msiz, nsz, (long)(x))); \
731 Renew(mbase, nsz, char); \
733 mptr = mbase + offset; \
734 mend = mbase + nsz; \
737 #define MBUF_CHK(x) \
739 if ((mptr + (x)) > mend) \
743 #define MBUF_GETC(x) \
746 x = (int) (unsigned char) *mptr++; \
752 #define MBUF_GETINT(x) \
755 if ((mptr + 4) <= mend) { \
756 memcpy(oI(&x), mptr, 4); \
762 #define MBUF_GETINT(x) \
764 if ((mptr + sizeof(int)) <= mend) { \
765 if (int_aligned(mptr)) \
768 memcpy(&x, mptr, sizeof(int)); \
769 mptr += sizeof(int); \
775 #define MBUF_READ(x,s) \
777 if ((mptr + (s)) <= mend) { \
778 memcpy(x, mptr, s); \
784 #define MBUF_SAFEREAD(x,s,z) \
786 if ((mptr + (s)) <= mend) { \
787 memcpy(x, mptr, s); \
795 #define MBUF_SAFEPVREAD(x,s,z) \
797 if ((mptr + (s)) <= mend) { \
798 memcpy(x, mptr, s); \
806 #define MBUF_PUTC(c) \
809 *mptr++ = (char) c; \
812 *mptr++ = (char) c; \
817 #define MBUF_PUTINT(i) \
820 memcpy(mptr, oI(&i), 4); \
824 #define MBUF_PUTINT(i) \
826 MBUF_CHK(sizeof(int)); \
827 if (int_aligned(mptr)) \
830 memcpy(mptr, &i, sizeof(int)); \
831 mptr += sizeof(int); \
835 #define MBUF_PUTLONG(l) \
838 memcpy(mptr, &l, 8); \
841 #define MBUF_WRITE(x,s) \
844 memcpy(mptr, x, s); \
849 * Possible return values for sv_type().
853 #define svis_SCALAR 1
857 #define svis_TIED_ITEM 5
859 #define svis_REGEXP 7
866 #define SHF_TYPE_MASK 0x03
867 #define SHF_LARGE_CLASSLEN 0x04
868 #define SHF_LARGE_STRLEN 0x08
869 #define SHF_LARGE_LISTLEN 0x10
870 #define SHF_IDX_CLASSNAME 0x20
871 #define SHF_NEED_RECURSE 0x40
872 #define SHF_HAS_LIST 0x80
875 * Types for SX_HOOK (last 2 bits in flags).
881 #define SHT_EXTRA 3 /* Read extra byte for type */
884 * The following are held in the "extra byte"...
887 #define SHT_TSCALAR 4 /* 4 + 0 -- tied scalar */
888 #define SHT_TARRAY 5 /* 4 + 1 -- tied array */
889 #define SHT_THASH 6 /* 4 + 2 -- tied hash */
892 * per hash flags for flagged hashes
895 #define SHV_RESTRICTED 0x01
898 * per key flags for flagged hashes
901 #define SHV_K_UTF8 0x01
902 #define SHV_K_WASUTF8 0x02
903 #define SHV_K_LOCKED 0x04
904 #define SHV_K_ISSV 0x08
905 #define SHV_K_PLACEHOLDER 0x10
908 * flags to allow blessing and/or tieing data the data we load
910 #define FLAG_BLESS_OK 2
911 #define FLAG_TIE_OK 4
914 * Flags for SX_REGEXP.
917 #define SHR_U32_RE_LEN 0x01
920 * Before 0.6, the magic string was "perl-store" (binary version number 0).
922 * Since 0.6 introduced many binary incompatibilities, the magic string has
923 * been changed to "pst0" to allow an old image to be properly retrieved by
924 * a newer Storable, but ensure a newer image cannot be retrieved with an
927 * At 0.7, objects are given the ability to serialize themselves, and the
928 * set of markers is extended, backward compatibility is not jeopardized,
929 * so the binary version number could have remained unchanged. To correctly
930 * spot errors if a file making use of 0.7-specific extensions is given to
931 * 0.6 for retrieval, the binary version was moved to "2". And I'm introducing
932 * a "minor" version, to better track this kind of evolution from now on.
935 static const char old_magicstr[] = "perl-store"; /* Magic number before 0.6 */
936 static const char magicstr[] = "pst0"; /* Used as a magic number */
938 #define MAGICSTR_BYTES 'p','s','t','0'
939 #define OLDMAGICSTR_BYTES 'p','e','r','l','-','s','t','o','r','e'
941 /* 5.6.x introduced the ability to have IVs as long long.
942 However, Configure still defined BYTEORDER based on the size of a long.
943 Storable uses the BYTEORDER value as part of the header, but doesn't
944 explicitly store sizeof(IV) anywhere in the header. Hence on 5.6.x built
945 with IV as long long on a platform that uses Configure (ie most things
946 except VMS and Windows) headers are identical for the different IV sizes,
947 despite the files containing some fields based on sizeof(IV)
949 5.8 is consistent - the following redefinition kludge is only needed on
950 5.6.x, but the interwork is needed on 5.8 while data survives in files
955 #if defined (IVSIZE) && (IVSIZE == 8) && (LONGSIZE == 4)
956 #ifndef NO_56_INTERWORK_KLUDGE
957 #define USE_56_INTERWORK_KLUDGE
959 #if BYTEORDER == 0x1234
961 #define BYTEORDER 0x12345678
963 #if BYTEORDER == 0x4321
965 #define BYTEORDER 0x87654321
970 #if BYTEORDER == 0x1234
971 #define BYTEORDER_BYTES '1','2','3','4'
973 #if BYTEORDER == 0x12345678
974 #define BYTEORDER_BYTES '1','2','3','4','5','6','7','8'
975 #ifdef USE_56_INTERWORK_KLUDGE
976 #define BYTEORDER_BYTES_56 '1','2','3','4'
979 #if BYTEORDER == 0x87654321
980 #define BYTEORDER_BYTES '8','7','6','5','4','3','2','1'
981 #ifdef USE_56_INTERWORK_KLUDGE
982 #define BYTEORDER_BYTES_56 '4','3','2','1'
985 #if BYTEORDER == 0x4321
986 #define BYTEORDER_BYTES '4','3','2','1'
988 #error Unknown byteorder. Please append your byteorder to Storable.xs
995 # define INT32_MAX 2147483647
997 #if IVSIZE > 4 && !defined(INT64_MAX)
998 # define INT64_MAX 9223372036854775807LL
1001 static const char byteorderstr[] = {BYTEORDER_BYTES, 0};
1002 #ifdef USE_56_INTERWORK_KLUDGE
1003 static const char byteorderstr_56[] = {BYTEORDER_BYTES_56, 0};
1006 #define STORABLE_BIN_MAJOR 2 /* Binary major "version" */
1007 #define STORABLE_BIN_MINOR 11 /* Binary minor "version" */
1009 #if (PATCHLEVEL <= 5)
1010 #define STORABLE_BIN_WRITE_MINOR 4
1011 #elif !defined (SvVOK)
1013 * Perl 5.6.0-5.8.0 can do weak references, but not vstring magic.
1015 #define STORABLE_BIN_WRITE_MINOR 8
1016 #elif PATCHLEVEL >= 19
1017 /* Perl 5.19 takes away the special meaning of PL_sv_undef in arrays. */
1018 /* With 3.x we added LOBJECT */
1019 #define STORABLE_BIN_WRITE_MINOR 11
1021 #define STORABLE_BIN_WRITE_MINOR 9
1022 #endif /* (PATCHLEVEL <= 5) */
1024 #if (PATCHLEVEL < 8 || (PATCHLEVEL == 8 && SUBVERSION < 1))
1025 #define PL_sv_placeholder PL_sv_undef
1029 * Useful store shortcuts...
1033 * Note that if you put more than one mark for storing a particular
1034 * type of thing, *and* in the retrieve_foo() function you mark both
1035 * the thingy's you get off with SEEN(), you *must* increase the
1036 * tagnum with cxt->tagnum++ along with this macro!
1039 #define PUTMARK(x) \
1043 else if (PerlIO_putc(cxt->fio, x) == EOF) \
1047 #define WRITE_I32(x) \
1049 ASSERT(sizeof(x) == sizeof(I32), ("writing an I32")); \
1052 else if (PerlIO_write(cxt->fio, oI(&x), \
1053 oS(sizeof(x))) != oS(sizeof(x))) \
1057 #define WRITE_U64(x) \
1059 ASSERT(sizeof(x) == sizeof(UV), ("writing an UV")); \
1062 else if (PerlIO_write(cxt->fio, oL(&x), \
1063 oS(sizeof(x))) != oS(sizeof(x))) \
1070 ASSERT(sizeof(x) == sizeof(int), ("WLEN writing an int")); \
1071 if (cxt->netorder) { \
1072 int y = (int) htonl(x); \
1075 else if (PerlIO_write(cxt->fio,oI(&y),oS(sizeof(y))) != oS(sizeof(y))) \
1080 else if (PerlIO_write(cxt->fio,oI(&x), \
1081 oS(sizeof(x))) != oS(sizeof(x))) \
1090 ASSERT(sizeof(x) == 8, ("W64LEN writing a U64")); \
1091 if (cxt->netorder) { \
1093 buf[1] = htonl(x & 0xffffffffUL); \
1094 buf[0] = htonl(x >> 32); \
1096 MBUF_PUTLONG(buf); \
1097 else if (PerlIO_write(cxt->fio, buf, \
1098 sizeof(buf)) != sizeof(buf)) \
1103 else if (PerlIO_write(cxt->fio,oI(&x), \
1104 oS(sizeof(x))) != oS(sizeof(x))) \
1111 #define W64LEN(x) CROAK(("No 64bit UVs"))
1116 #define WLEN(x) WRITE_I32(x)
1118 #define W64LEN(x) WRITE_U64(x)
1120 #define W64LEN(x) CROAK(("no 64bit UVs"))
1124 #define WRITE(x,y) \
1128 else if (PerlIO_write(cxt->fio, x, y) != (SSize_t)y) \
1132 #define STORE_PV_LEN(pv, len, small, large) \
1134 if (len <= LG_SCALAR) { \
1135 int ilen = (int) len; \
1136 unsigned char clen = (unsigned char) len; \
1141 } else if (sizeof(len) > 4 && len > INT32_MAX) { \
1142 PUTMARK(SX_LOBJECT); \
1147 int ilen = (int) len; \
1154 #define STORE_SCALAR(pv, len) STORE_PV_LEN(pv, len, SX_SCALAR, SX_LSCALAR)
1157 * Store &PL_sv_undef in arrays without recursing through store(). We
1158 * actually use this to represent nonexistent elements, for historical
1161 #define STORE_SV_UNDEF() \
1164 PUTMARK(SX_SV_UNDEF); \
1168 * Useful retrieve shortcuts...
1172 (cxt->fio ? PerlIO_getc(cxt->fio) \
1173 : (mptr >= mend ? EOF : (int) *mptr++))
1175 #define GETMARK(x) \
1179 else if ((int) (x = PerlIO_getc(cxt->fio)) == EOF) \
1183 #define READ_I32(x) \
1185 ASSERT(sizeof(x) == sizeof(I32), ("reading an I32")); \
1189 else if (PerlIO_read(cxt->fio, oI(&x), \
1190 oS(sizeof(x))) != oS(sizeof(x))) \
1200 else if (PerlIO_read(cxt->fio, oI(&x), \
1201 oS(sizeof(x))) != oS(sizeof(x))) \
1203 if (cxt->netorder) \
1204 x = (int) ntohl(x); \
1207 #define RLEN(x) READ_I32(x)
1214 else if (PerlIO_read(cxt->fio, x, y) != (SSize_t)y) \
1218 #define SAFEREAD(x,y,z) \
1221 MBUF_SAFEREAD(x,y,z); \
1222 else if (PerlIO_read(cxt->fio, x, y) != (SSize_t)y) { \
1228 #define SAFEPVREAD(x,y,z) \
1231 MBUF_SAFEPVREAD(x,y,z); \
1232 else if (PerlIO_read(cxt->fio, x, y) != y) { \
1240 # if defined(HAS_NTOHL)
1241 # define Sntohl(x) ntohl(x)
1242 # elif BYTEORDER == 0x87654321 || BYTEORDER == 0x4321
1243 # define Sntohl(x) (x)
1245 static U32 Sntohl(U32 x) {
1246 return ((x & 0xFF) << 24) + ((x * 0xFF00) << 8)
1247 + ((x & 0xFF0000) >> 8) + ((x & 0xFF000000) >> 24);
1251 # define READ_U64(x) \
1253 ASSERT(sizeof(x) == 8, ("R64LEN reading a U64")); \
1254 if (cxt->netorder) { \
1256 READ((void *)buf, sizeof(buf)); \
1257 (x) = ((UV)Sntohl(buf[0]) << 32) + Sntohl(buf[1]); \
1260 READ(&(x), sizeof(x)); \
1267 * SEEN() is used at retrieve time, to remember where object 'y', bearing a
1268 * given tag 'tagnum', has been retrieved. Next time we see an SX_OBJECT marker,
1269 * we'll therefore know where it has been retrieved and will be able to
1270 * share the same reference, as in the original stored memory image.
1272 * We also need to bless objects ASAP for hooks (which may compute "ref $x"
1273 * on the objects given to STORABLE_thaw and expect that to be defined), and
1274 * also for overloaded objects (for which we might not find the stash if the
1275 * object is not blessed yet--this might occur for overloaded objects that
1276 * refer to themselves indirectly: if we blessed upon return from a sub
1277 * retrieve(), the SX_OBJECT marker we'd found could not have overloading
1278 * restored on it because the underlying object would not be blessed yet!).
1280 * To achieve that, the class name of the last retrieved object is passed down
1281 * recursively, and the first SEEN() call for which the class name is not NULL
1282 * will bless the object.
1284 * i should be true iff sv is immortal (ie PL_sv_yes, PL_sv_no or PL_sv_undef)
1286 * SEEN0() is a short-cut where stash is always NULL.
1288 * The _NN variants dont check for y being null
1290 #define SEEN0_NN(y,i) \
1292 if (av_store(cxt->aseen, cxt->tagnum++, i ? (SV*)(y) \
1293 : SvREFCNT_inc(y)) == 0) \
1295 TRACEME(("aseen(#%d) = 0x%" UVxf " (refcnt=%d)", \
1296 (int)cxt->tagnum-1, \
1297 PTR2UV(y), (int)SvREFCNT(y)-1)); \
1300 #define SEEN0(y,i) \
1307 #define SEEN_NN(y,stash,i) \
1311 BLESS((SV *)(y), (HV *)(stash)); \
1314 #define SEEN(y,stash,i) \
1318 SEEN_NN(y,stash, i); \
1322 * Bless 's' in 'p', via a temporary reference, required by sv_bless().
1323 * "A" magic is added before the sv_bless for overloaded classes, this avoids
1324 * an expensive call to S_reset_amagic in sv_bless.
1326 #define BLESS(s,stash) \
1329 if (cxt->flags & FLAG_BLESS_OK) { \
1330 TRACEME(("blessing 0x%" UVxf " in %s", PTR2UV(s), \
1331 HvNAME_get(stash))); \
1332 ref = newRV_noinc(s); \
1333 if (cxt->in_retrieve_overloaded && Gv_AMG(stash)) { \
1334 cxt->in_retrieve_overloaded = 0; \
1337 (void) sv_bless(ref, stash); \
1338 SvRV_set(ref, NULL); \
1339 SvREFCNT_dec(ref); \
1342 TRACEME(("not blessing 0x%" UVxf " in %s", PTR2UV(s), \
1343 (HvNAME_get(stash)))); \
1347 * sort (used in store_hash) - conditionally use qsort when
1348 * sortsv is not available ( <= 5.6.1 ).
1351 #if (PATCHLEVEL <= 6)
1353 #if defined(USE_ITHREADS)
1355 #define STORE_HASH_SORT \
1357 PerlInterpreter *orig_perl = PERL_GET_CONTEXT; \
1358 SAVESPTR(orig_perl); \
1359 PERL_SET_CONTEXT(aTHX); \
1360 qsort((char *) AvARRAY(av), len, sizeof(SV *), sortcmp);\
1363 #else /* ! USE_ITHREADS */
1365 #define STORE_HASH_SORT \
1366 qsort((char *) AvARRAY(av), len, sizeof(SV *), sortcmp);
1368 #endif /* USE_ITHREADS */
1370 #else /* PATCHLEVEL > 6 */
1372 #define STORE_HASH_SORT \
1373 sortsv(AvARRAY(av), len, Perl_sv_cmp);
1375 #endif /* PATCHLEVEL <= 6 */
1377 static int store(pTHX_ stcxt_t *cxt, SV *sv);
1378 static SV *retrieve(pTHX_ stcxt_t *cxt, const char *cname);
1382 av_pop(cxt->aseen); \
1387 * Dynamic dispatching table for SV store.
1390 static int store_ref(pTHX_ stcxt_t *cxt, SV *sv);
1391 static int store_scalar(pTHX_ stcxt_t *cxt, SV *sv);
1392 static int store_array(pTHX_ stcxt_t *cxt, AV *av);
1393 static int store_hash(pTHX_ stcxt_t *cxt, HV *hv);
1394 static int store_tied(pTHX_ stcxt_t *cxt, SV *sv);
1395 static int store_tied_item(pTHX_ stcxt_t *cxt, SV *sv);
1396 static int store_code(pTHX_ stcxt_t *cxt, CV *cv);
1397 static int store_regexp(pTHX_ stcxt_t *cxt, SV *sv);
1398 static int store_other(pTHX_ stcxt_t *cxt, SV *sv);
1399 static int store_blessed(pTHX_ stcxt_t *cxt, SV *sv, int type, HV *pkg);
1401 typedef int (*sv_store_t)(pTHX_ stcxt_t *cxt, SV *sv);
1403 static const sv_store_t sv_store[] = {
1404 (sv_store_t)store_ref, /* svis_REF */
1405 (sv_store_t)store_scalar, /* svis_SCALAR */
1406 (sv_store_t)store_array, /* svis_ARRAY */
1407 (sv_store_t)store_hash, /* svis_HASH */
1408 (sv_store_t)store_tied, /* svis_TIED */
1409 (sv_store_t)store_tied_item,/* svis_TIED_ITEM */
1410 (sv_store_t)store_code, /* svis_CODE */
1411 (sv_store_t)store_regexp, /* svis_REGEXP */
1412 (sv_store_t)store_other, /* svis_OTHER */
1415 #define SV_STORE(x) (*sv_store[x])
1418 * Dynamic dispatching tables for SV retrieval.
1421 static SV *retrieve_lscalar(pTHX_ stcxt_t *cxt, const char *cname);
1422 static SV *retrieve_lutf8str(pTHX_ stcxt_t *cxt, const char *cname);
1423 static SV *old_retrieve_array(pTHX_ stcxt_t *cxt, const char *cname);
1424 static SV *old_retrieve_hash(pTHX_ stcxt_t *cxt, const char *cname);
1425 static SV *retrieve_ref(pTHX_ stcxt_t *cxt, const char *cname);
1426 static SV *retrieve_undef(pTHX_ stcxt_t *cxt, const char *cname);
1427 static SV *retrieve_integer(pTHX_ stcxt_t *cxt, const char *cname);
1428 static SV *retrieve_double(pTHX_ stcxt_t *cxt, const char *cname);
1429 static SV *retrieve_byte(pTHX_ stcxt_t *cxt, const char *cname);
1430 static SV *retrieve_netint(pTHX_ stcxt_t *cxt, const char *cname);
1431 static SV *retrieve_scalar(pTHX_ stcxt_t *cxt, const char *cname);
1432 static SV *retrieve_utf8str(pTHX_ stcxt_t *cxt, const char *cname);
1433 static SV *retrieve_tied_array(pTHX_ stcxt_t *cxt, const char *cname);
1434 static SV *retrieve_tied_hash(pTHX_ stcxt_t *cxt, const char *cname);
1435 static SV *retrieve_tied_scalar(pTHX_ stcxt_t *cxt, const char *cname);
1436 static SV *retrieve_other(pTHX_ stcxt_t *cxt, const char *cname);
1437 static SV *retrieve_lobject(pTHX_ stcxt_t *cxt, const char *cname);
1438 static SV *retrieve_regexp(pTHX_ stcxt_t *cxt, const char *cname);
1440 /* helpers for U64 lobjects */
1442 static SV *get_lstring(pTHX_ stcxt_t *cxt, UV len, int isutf8, const char *cname);
1444 static SV *get_larray(pTHX_ stcxt_t *cxt, UV len, const char *cname);
1445 static SV *get_lhash(pTHX_ stcxt_t *cxt, UV len, int hash_flags, const char *cname);
1446 static int store_lhash(pTHX_ stcxt_t *cxt, HV *hv, unsigned char hash_flags);
1448 static int store_hentry(pTHX_ stcxt_t *cxt, HV* hv, UV i, HE *he, unsigned char hash_flags);
1450 typedef SV* (*sv_retrieve_t)(pTHX_ stcxt_t *cxt, const char *name);
1452 static const sv_retrieve_t sv_old_retrieve[] = {
1453 0, /* SX_OBJECT -- entry unused dynamically */
1454 (sv_retrieve_t)retrieve_lscalar, /* SX_LSCALAR */
1455 (sv_retrieve_t)old_retrieve_array, /* SX_ARRAY -- for pre-0.6 binaries */
1456 (sv_retrieve_t)old_retrieve_hash, /* SX_HASH -- for pre-0.6 binaries */
1457 (sv_retrieve_t)retrieve_ref, /* SX_REF */
1458 (sv_retrieve_t)retrieve_undef, /* SX_UNDEF */
1459 (sv_retrieve_t)retrieve_integer, /* SX_INTEGER */
1460 (sv_retrieve_t)retrieve_double, /* SX_DOUBLE */
1461 (sv_retrieve_t)retrieve_byte, /* SX_BYTE */
1462 (sv_retrieve_t)retrieve_netint, /* SX_NETINT */
1463 (sv_retrieve_t)retrieve_scalar, /* SX_SCALAR */
1464 (sv_retrieve_t)retrieve_tied_array, /* SX_TIED_ARRAY */
1465 (sv_retrieve_t)retrieve_tied_hash, /* SX_TIED_HASH */
1466 (sv_retrieve_t)retrieve_tied_scalar,/* SX_TIED_SCALAR */
1467 (sv_retrieve_t)retrieve_other, /* SX_SV_UNDEF not supported */
1468 (sv_retrieve_t)retrieve_other, /* SX_SV_YES not supported */
1469 (sv_retrieve_t)retrieve_other, /* SX_SV_NO not supported */
1470 (sv_retrieve_t)retrieve_other, /* SX_BLESS not supported */
1471 (sv_retrieve_t)retrieve_other, /* SX_IX_BLESS not supported */
1472 (sv_retrieve_t)retrieve_other, /* SX_HOOK not supported */
1473 (sv_retrieve_t)retrieve_other, /* SX_OVERLOADED not supported */
1474 (sv_retrieve_t)retrieve_other, /* SX_TIED_KEY not supported */
1475 (sv_retrieve_t)retrieve_other, /* SX_TIED_IDX not supported */
1476 (sv_retrieve_t)retrieve_other, /* SX_UTF8STR not supported */
1477 (sv_retrieve_t)retrieve_other, /* SX_LUTF8STR not supported */
1478 (sv_retrieve_t)retrieve_other, /* SX_FLAG_HASH not supported */
1479 (sv_retrieve_t)retrieve_other, /* SX_CODE not supported */
1480 (sv_retrieve_t)retrieve_other, /* SX_WEAKREF not supported */
1481 (sv_retrieve_t)retrieve_other, /* SX_WEAKOVERLOAD not supported */
1482 (sv_retrieve_t)retrieve_other, /* SX_VSTRING not supported */
1483 (sv_retrieve_t)retrieve_other, /* SX_LVSTRING not supported */
1484 (sv_retrieve_t)retrieve_other, /* SX_SVUNDEF_ELEM not supported */
1485 (sv_retrieve_t)retrieve_other, /* SX_REGEXP */
1486 (sv_retrieve_t)retrieve_other, /* SX_LOBJECT not supported */
1487 (sv_retrieve_t)retrieve_other, /* SX_LAST */
1490 static SV *retrieve_hook_common(pTHX_ stcxt_t *cxt, const char *cname, int large);
1492 static SV *retrieve_array(pTHX_ stcxt_t *cxt, const char *cname);
1493 static SV *retrieve_hash(pTHX_ stcxt_t *cxt, const char *cname);
1494 static SV *retrieve_sv_undef(pTHX_ stcxt_t *cxt, const char *cname);
1495 static SV *retrieve_sv_yes(pTHX_ stcxt_t *cxt, const char *cname);
1496 static SV *retrieve_sv_no(pTHX_ stcxt_t *cxt, const char *cname);
1497 static SV *retrieve_blessed(pTHX_ stcxt_t *cxt, const char *cname);
1498 static SV *retrieve_idx_blessed(pTHX_ stcxt_t *cxt, const char *cname);
1499 static SV *retrieve_hook(pTHX_ stcxt_t *cxt, const char *cname);
1500 static SV *retrieve_overloaded(pTHX_ stcxt_t *cxt, const char *cname);
1501 static SV *retrieve_tied_key(pTHX_ stcxt_t *cxt, const char *cname);
1502 static SV *retrieve_tied_idx(pTHX_ stcxt_t *cxt, const char *cname);
1503 static SV *retrieve_flag_hash(pTHX_ stcxt_t *cxt, const char *cname);
1504 static SV *retrieve_code(pTHX_ stcxt_t *cxt, const char *cname);
1505 static SV *retrieve_weakref(pTHX_ stcxt_t *cxt, const char *cname);
1506 static SV *retrieve_weakoverloaded(pTHX_ stcxt_t *cxt, const char *cname);
1507 static SV *retrieve_vstring(pTHX_ stcxt_t *cxt, const char *cname);
1508 static SV *retrieve_lvstring(pTHX_ stcxt_t *cxt, const char *cname);
1509 static SV *retrieve_svundef_elem(pTHX_ stcxt_t *cxt, const char *cname);
1511 static const sv_retrieve_t sv_retrieve[] = {
1512 0, /* SX_OBJECT -- entry unused dynamically */
1513 (sv_retrieve_t)retrieve_lscalar, /* SX_LSCALAR */
1514 (sv_retrieve_t)retrieve_array, /* SX_ARRAY */
1515 (sv_retrieve_t)retrieve_hash, /* SX_HASH */
1516 (sv_retrieve_t)retrieve_ref, /* SX_REF */
1517 (sv_retrieve_t)retrieve_undef, /* SX_UNDEF */
1518 (sv_retrieve_t)retrieve_integer, /* SX_INTEGER */
1519 (sv_retrieve_t)retrieve_double, /* SX_DOUBLE */
1520 (sv_retrieve_t)retrieve_byte, /* SX_BYTE */
1521 (sv_retrieve_t)retrieve_netint, /* SX_NETINT */
1522 (sv_retrieve_t)retrieve_scalar, /* SX_SCALAR */
1523 (sv_retrieve_t)retrieve_tied_array, /* SX_TIED_ARRAY */
1524 (sv_retrieve_t)retrieve_tied_hash, /* SX_TIED_HASH */
1525 (sv_retrieve_t)retrieve_tied_scalar,/* SX_TIED_SCALAR */
1526 (sv_retrieve_t)retrieve_sv_undef, /* SX_SV_UNDEF */
1527 (sv_retrieve_t)retrieve_sv_yes, /* SX_SV_YES */
1528 (sv_retrieve_t)retrieve_sv_no, /* SX_SV_NO */
1529 (sv_retrieve_t)retrieve_blessed, /* SX_BLESS */
1530 (sv_retrieve_t)retrieve_idx_blessed,/* SX_IX_BLESS */
1531 (sv_retrieve_t)retrieve_hook, /* SX_HOOK */
1532 (sv_retrieve_t)retrieve_overloaded, /* SX_OVERLOAD */
1533 (sv_retrieve_t)retrieve_tied_key, /* SX_TIED_KEY */
1534 (sv_retrieve_t)retrieve_tied_idx, /* SX_TIED_IDX */
1535 (sv_retrieve_t)retrieve_utf8str, /* SX_UTF8STR */
1536 (sv_retrieve_t)retrieve_lutf8str, /* SX_LUTF8STR */
1537 (sv_retrieve_t)retrieve_flag_hash, /* SX_HASH */
1538 (sv_retrieve_t)retrieve_code, /* SX_CODE */
1539 (sv_retrieve_t)retrieve_weakref, /* SX_WEAKREF */
1540 (sv_retrieve_t)retrieve_weakoverloaded,/* SX_WEAKOVERLOAD */
1541 (sv_retrieve_t)retrieve_vstring, /* SX_VSTRING */
1542 (sv_retrieve_t)retrieve_lvstring, /* SX_LVSTRING */
1543 (sv_retrieve_t)retrieve_svundef_elem,/* SX_SVUNDEF_ELEM */
1544 (sv_retrieve_t)retrieve_regexp, /* SX_REGEXP */
1545 (sv_retrieve_t)retrieve_lobject, /* SX_LOBJECT */
1546 (sv_retrieve_t)retrieve_other, /* SX_LAST */
1549 #define RETRIEVE(c,x) ((x) >= SX_LAST ? retrieve_other : *(c)->retrieve_vtbl[x])
1551 static SV *mbuf2sv(pTHX);
1554 *** Context management.
1560 * Called once per "thread" (interpreter) to initialize some global context.
1562 static void init_perinterp(pTHX)
1566 cxt->netorder = 0; /* true if network order used */
1567 cxt->forgive_me = -1; /* whether to be forgiving... */
1568 cxt->accept_future_minor = -1; /* would otherwise occur too late */
1574 * Called at the end of every context cleaning, to perform common reset
1577 static void reset_context(stcxt_t *cxt)
1581 cxt->recur_sv = NULL;
1582 cxt->recur_depth = 0;
1583 cxt->optype &= ~(ST_STORE|ST_RETRIEVE); /* Leave ST_CLONE alone */
1587 * init_store_context
1589 * Initialize a new store context for real recursion.
1591 static void init_store_context(pTHX_
1599 TRACEME(("init_store_context"));
1601 cxt->netorder = network_order;
1602 cxt->forgive_me = -1; /* Fetched from perl if needed */
1603 cxt->deparse = -1; /* Idem */
1604 cxt->eval = NULL; /* Idem */
1605 cxt->canonical = -1; /* Idem */
1606 cxt->tagnum = -1; /* Reset tag numbers */
1607 cxt->classnum = -1; /* Reset class numbers */
1608 cxt->fio = f; /* Where I/O are performed */
1609 cxt->optype = optype; /* A store, or a deep clone */
1610 cxt->entry = 1; /* No recursion yet */
1613 * The 'hseen' table is used to keep track of each SV stored and their
1614 * associated tag numbers is special. It is "abused" because the
1615 * values stored are not real SV, just integers cast to (SV *),
1616 * which explains the freeing below.
1618 * It is also one possible bottleneck to achieve good storing speed,
1619 * so the "shared keys" optimization is turned off (unlikely to be
1620 * of any use here), and the hash table is "pre-extended". Together,
1621 * those optimizations increase the throughput by 12%.
1624 #ifdef USE_PTR_TABLE
1625 cxt->pseen = ptr_table_new();
1628 cxt->hseen = newHV(); /* Table where seen objects are stored */
1629 HvSHAREKEYS_off(cxt->hseen);
1632 * The following does not work well with perl5.004_04, and causes
1633 * a core dump later on, in a completely unrelated spot, which
1634 * makes me think there is a memory corruption going on.
1636 * Calling hv_ksplit(hseen, HBUCKETS) instead of manually hacking
1637 * it below does not make any difference. It seems to work fine
1638 * with perl5.004_68 but given the probable nature of the bug,
1639 * that does not prove anything.
1641 * It's a shame because increasing the amount of buckets raises
1642 * store() throughput by 5%, but until I figure this out, I can't
1643 * allow for this to go into production.
1645 * It is reported fixed in 5.005, hence the #if.
1647 #if PERL_VERSION >= 5
1648 #define HBUCKETS 4096 /* Buckets for %hseen */
1649 #ifndef USE_PTR_TABLE
1650 HvMAX(cxt->hseen) = HBUCKETS - 1; /* keys %hseen = $HBUCKETS; */
1655 * The 'hclass' hash uses the same settings as 'hseen' above, but it is
1656 * used to assign sequential tags (numbers) to class names for blessed
1659 * We turn the shared key optimization on.
1662 cxt->hclass = newHV(); /* Where seen classnames are stored */
1664 #if PERL_VERSION >= 5
1665 HvMAX(cxt->hclass) = HBUCKETS - 1; /* keys %hclass = $HBUCKETS; */
1669 * The 'hook' hash table is used to keep track of the references on
1670 * the STORABLE_freeze hook routines, when found in some class name.
1672 * It is assumed that the inheritance tree will not be changed during
1673 * storing, and that no new method will be dynamically created by the
1677 cxt->hook = newHV(); /* Table where hooks are cached */
1680 * The 'hook_seen' array keeps track of all the SVs returned by
1681 * STORABLE_freeze hooks for us to serialize, so that they are not
1682 * reclaimed until the end of the serialization process. Each SV is
1683 * only stored once, the first time it is seen.
1686 cxt->hook_seen = newAV(); /* Lists SVs returned by STORABLE_freeze */
1688 cxt->max_recur_depth = SvIV(get_sv("Storable::recursion_limit", GV_ADD));
1689 cxt->max_recur_depth_hash = SvIV(get_sv("Storable::recursion_limit_hash", GV_ADD));
1693 * clean_store_context
1695 * Clean store context by
1697 static void clean_store_context(pTHX_ stcxt_t *cxt)
1701 TRACEMED(("clean_store_context"));
1703 ASSERT(cxt->optype & ST_STORE, ("was performing a store()"));
1706 * Insert real values into hashes where we stored faked pointers.
1709 #ifndef USE_PTR_TABLE
1711 hv_iterinit(cxt->hseen);
1712 while ((he = hv_iternext(cxt->hseen))) /* Extra () for -Wall */
1713 HeVAL(he) = &PL_sv_undef;
1718 hv_iterinit(cxt->hclass);
1719 while ((he = hv_iternext(cxt->hclass))) /* Extra () for -Wall */
1720 HeVAL(he) = &PL_sv_undef;
1724 * And now dispose of them...
1726 * The surrounding if() protection has been added because there might be
1727 * some cases where this routine is called more than once, during
1728 * exceptional events. This was reported by Marc Lehmann when Storable
1729 * is executed from mod_perl, and the fix was suggested by him.
1730 * -- RAM, 20/12/2000
1733 #ifdef USE_PTR_TABLE
1735 struct ptr_tbl *pseen = cxt->pseen;
1737 ptr_table_free(pseen);
1739 assert(!cxt->hseen);
1742 HV *hseen = cxt->hseen;
1745 sv_free((SV *) hseen);
1750 HV *hclass = cxt->hclass;
1753 sv_free((SV *) hclass);
1757 HV *hook = cxt->hook;
1760 sv_free((SV *) hook);
1763 if (cxt->hook_seen) {
1764 AV *hook_seen = cxt->hook_seen;
1766 av_undef(hook_seen);
1767 sv_free((SV *) hook_seen);
1770 cxt->forgive_me = -1; /* Fetched from perl if needed */
1771 cxt->deparse = -1; /* Idem */
1773 SvREFCNT_dec(cxt->eval);
1775 cxt->eval = NULL; /* Idem */
1776 cxt->canonical = -1; /* Idem */
1782 * init_retrieve_context
1784 * Initialize a new retrieve context for real recursion.
1786 static void init_retrieve_context(pTHX_
1787 stcxt_t *cxt, int optype, int is_tainted)
1791 TRACEME(("init_retrieve_context"));
1794 * The hook hash table is used to keep track of the references on
1795 * the STORABLE_thaw hook routines, when found in some class name.
1797 * It is assumed that the inheritance tree will not be changed during
1798 * storing, and that no new method will be dynamically created by the
1802 cxt->hook = newHV(); /* Caches STORABLE_thaw */
1804 #ifdef USE_PTR_TABLE
1809 * If retrieving an old binary version, the cxt->retrieve_vtbl variable
1810 * was set to sv_old_retrieve. We'll need a hash table to keep track of
1811 * the correspondence between the tags and the tag number used by the
1812 * new retrieve routines.
1815 cxt->hseen = (((void*)cxt->retrieve_vtbl == (void*)sv_old_retrieve)
1818 cxt->aseen = newAV(); /* Where retrieved objects are kept */
1819 cxt->where_is_undef = UNSET_NTAG_T; /* Special case for PL_sv_undef */
1820 cxt->aclass = newAV(); /* Where seen classnames are kept */
1821 cxt->tagnum = 0; /* Have to count objects... */
1822 cxt->classnum = 0; /* ...and class names as well */
1823 cxt->optype = optype;
1824 cxt->s_tainted = is_tainted;
1825 cxt->entry = 1; /* No recursion yet */
1826 #ifndef HAS_RESTRICTED_HASHES
1827 cxt->derestrict = -1; /* Fetched from perl if needed */
1829 #ifndef HAS_UTF8_ALL
1830 cxt->use_bytes = -1; /* Fetched from perl if needed */
1832 cxt->accept_future_minor = -1;/* Fetched from perl if needed */
1833 cxt->in_retrieve_overloaded = 0;
1835 cxt->max_recur_depth = SvIV(get_sv("Storable::recursion_limit", GV_ADD));
1836 cxt->max_recur_depth_hash = SvIV(get_sv("Storable::recursion_limit_hash", GV_ADD));
1840 * clean_retrieve_context
1842 * Clean retrieve context by
1844 static void clean_retrieve_context(pTHX_ stcxt_t *cxt)
1846 TRACEMED(("clean_retrieve_context"));
1848 ASSERT(cxt->optype & ST_RETRIEVE, ("was performing a retrieve()"));
1851 AV *aseen = cxt->aseen;
1854 sv_free((SV *) aseen);
1856 cxt->where_is_undef = UNSET_NTAG_T;
1859 AV *aclass = cxt->aclass;
1862 sv_free((SV *) aclass);
1866 HV *hook = cxt->hook;
1869 sv_free((SV *) hook);
1873 HV *hseen = cxt->hseen;
1876 sv_free((SV *) hseen); /* optional HV, for backward compat. */
1879 #ifndef HAS_RESTRICTED_HASHES
1880 cxt->derestrict = -1; /* Fetched from perl if needed */
1882 #ifndef HAS_UTF8_ALL
1883 cxt->use_bytes = -1; /* Fetched from perl if needed */
1885 cxt->accept_future_minor = -1; /* Fetched from perl if needed */
1887 cxt->in_retrieve_overloaded = 0;
1894 * A workaround for the CROAK bug: cleanup the last context.
1896 static void clean_context(pTHX_ stcxt_t *cxt)
1898 TRACEMED(("clean_context"));
1900 ASSERT(cxt->s_dirty, ("dirty context"));
1905 ASSERT(!cxt->membuf_ro, ("mbase is not read-only"));
1907 if (cxt->optype & ST_RETRIEVE)
1908 clean_retrieve_context(aTHX_ cxt);
1909 else if (cxt->optype & ST_STORE)
1910 clean_store_context(aTHX_ cxt);
1914 ASSERT(!cxt->s_dirty, ("context is clean"));
1915 ASSERT(cxt->entry == 0, ("context is reset"));
1921 * Allocate a new context and push it on top of the parent one.
1922 * This new context is made globally visible via SET_STCXT().
1924 static stcxt_t *allocate_context(pTHX_ stcxt_t *parent_cxt)
1928 ASSERT(!parent_cxt->s_dirty, ("parent context clean"));
1930 NEW_STORABLE_CXT_OBJ(cxt);
1931 TRACEMED(("allocate_context"));
1933 cxt->prev = parent_cxt->my_sv;
1936 ASSERT(!cxt->s_dirty, ("clean context"));
1944 * Free current context, which cannot be the "root" one.
1945 * Make the context underneath globally visible via SET_STCXT().
1947 static void free_context(pTHX_ stcxt_t *cxt)
1949 stcxt_t *prev = (stcxt_t *)(cxt->prev ? SvPVX(SvRV(cxt->prev)) : 0);
1951 TRACEMED(("free_context"));
1953 ASSERT(!cxt->s_dirty, ("clean context"));
1954 ASSERT(prev, ("not freeing root context"));
1957 SvREFCNT_dec(cxt->my_sv);
1960 ASSERT(cxt, ("context not void"));
1967 /* these two functions are currently only used within asserts */
1972 * Tells whether we're in the middle of a store operation.
1974 static int is_storing(pTHX)
1978 return cxt->entry && (cxt->optype & ST_STORE);
1984 * Tells whether we're in the middle of a retrieve operation.
1986 static int is_retrieving(pTHX)
1990 return cxt->entry && (cxt->optype & ST_RETRIEVE);
1995 * last_op_in_netorder
1997 * Returns whether last operation was made using network order.
1999 * This is typically out-of-band information that might prove useful
2000 * to people wishing to convert native to network order data when used.
2002 static int last_op_in_netorder(pTHX)
2007 return cxt->netorder;
2011 *** Hook lookup and calling routines.
2017 * A wrapper on gv_fetchmethod_autoload() which caches results.
2019 * Returns the routine reference as an SV*, or null if neither the package
2020 * nor its ancestors know about the method.
2022 static SV *pkg_fetchmeth(pTHX_
2029 const char *hvname = HvNAME_get(pkg);
2035 * The following code is the same as the one performed by UNIVERSAL::can
2039 gv = gv_fetchmethod_autoload(pkg, method, FALSE);
2040 if (gv && isGV(gv)) {
2041 sv = newRV_inc((SV*) GvCV(gv));
2042 TRACEME(("%s->%s: 0x%" UVxf, hvname, method, PTR2UV(sv)));
2044 sv = newSVsv(&PL_sv_undef);
2045 TRACEME(("%s->%s: not found", hvname, method));
2049 * Cache the result, ignoring failure: if we can't store the value,
2050 * it just won't be cached.
2053 (void) hv_store(cache, hvname, strlen(hvname), sv, 0);
2055 return SvOK(sv) ? sv : (SV *) 0;
2061 * Force cached value to be undef: hook ignored even if present.
2063 static void pkg_hide(pTHX_
2068 const char *hvname = HvNAME_get(pkg);
2069 PERL_UNUSED_ARG(method);
2070 (void) hv_store(cache,
2071 hvname, strlen(hvname), newSVsv(&PL_sv_undef), 0);
2077 * Discard cached value: a whole fetch loop will be retried at next lookup.
2079 static void pkg_uncache(pTHX_
2084 const char *hvname = HvNAME_get(pkg);
2085 PERL_UNUSED_ARG(method);
2086 (void) hv_delete(cache, hvname, strlen(hvname), G_DISCARD);
2092 * Our own "UNIVERSAL::can", which caches results.
2094 * Returns the routine reference as an SV*, or null if the object does not
2095 * know about the method.
2097 static SV *pkg_can(pTHX_
2104 const char *hvname = HvNAME_get(pkg);
2109 TRACEME(("pkg_can for %s->%s", hvname, method));
2112 * Look into the cache to see whether we already have determined
2113 * where the routine was, if any.
2115 * NOTA BENE: we don't use 'method' at all in our lookup, since we know
2116 * that only one hook (i.e. always the same) is cached in a given cache.
2119 svh = hv_fetch(cache, hvname, strlen(hvname), FALSE);
2123 TRACEME(("cached %s->%s: not found", hvname, method));
2126 TRACEME(("cached %s->%s: 0x%" UVxf,
2127 hvname, method, PTR2UV(sv)));
2132 TRACEME(("not cached yet"));
2133 return pkg_fetchmeth(aTHX_ cache, pkg, method); /* Fetch and cache */
2139 * Call routine as obj->hook(av) in scalar context.
2140 * Propagates the single returned value if not called in void context.
2142 static SV *scalar_call(pTHX_
2156 TRACEME(("scalar_call (cloning=%d)", cloning));
2163 XPUSHs(sv_2mortal(newSViv(cloning))); /* Cloning flag */
2165 SV **ary = AvARRAY(av);
2166 SSize_t cnt = AvFILLp(av) + 1;
2168 XPUSHs(ary[0]); /* Frozen string */
2169 for (i = 1; i < cnt; i++) {
2170 TRACEME(("pushing arg #%d (0x%" UVxf ")...",
2171 (int)i, PTR2UV(ary[i])));
2172 XPUSHs(sv_2mortal(newRV_inc(ary[i])));
2177 TRACEME(("calling..."));
2178 count = call_sv(hook, flags); /* Go back to Perl code */
2179 TRACEME(("count = %d", count));
2185 SvREFCNT_inc(sv); /* We're returning it, must stay alive! */
2198 * Call routine obj->hook(cloning) in list context.
2199 * Returns the list of returned values in an array.
2201 static AV *array_call(pTHX_
2214 TRACEME(("array_call (cloning=%d)", cloning));
2220 XPUSHs(obj); /* Target object */
2221 XPUSHs(sv_2mortal(newSViv(cloning))); /* Cloning flag */
2224 count = call_sv(hook, G_ARRAY); /* Go back to Perl code */
2229 for (i = count - 1; i >= 0; i--) {
2231 av_store(av, i, SvREFCNT_inc(sv));
2241 #if PERL_VERSION < 15
2243 cleanup_recursive_av(pTHX_ AV* av) {
2244 SSize_t i = AvFILLp(av);
2245 SV** arr = AvARRAY(av);
2246 if (SvMAGICAL(av)) return;
2249 #if PERL_VERSION < 14
2252 SvREFCNT_dec(arr[i]);
2259 #ifndef SvREFCNT_IMMORTAL
2261 /* exercise the immortal resurrection code in sv_free2() */
2262 # define SvREFCNT_IMMORTAL 1000
2264 # define SvREFCNT_IMMORTAL ((~(U32)0)/2)
2269 cleanup_recursive_hv(pTHX_ HV* hv) {
2270 SSize_t i = HvTOTALKEYS(hv);
2271 HE** arr = HvARRAY(hv);
2272 if (SvMAGICAL(hv)) return;
2275 SvREFCNT(HeVAL(arr[i])) = SvREFCNT_IMMORTAL;
2276 arr[i] = NULL; /* let it leak. too dangerous to clean it up here */
2280 #if PERL_VERSION < 8
2281 ((XPVHV*)SvANY(hv))->xhv_array = NULL;
2285 HvTOTALKEYS(hv) = 0;
2288 cleanup_recursive_rv(pTHX_ SV* sv) {
2289 if (sv && SvROK(sv))
2290 SvREFCNT_dec(SvRV(sv));
2293 cleanup_recursive_data(pTHX_ SV* sv) {
2294 if (SvTYPE(sv) == SVt_PVAV) {
2295 cleanup_recursive_av(aTHX_ (AV*)sv);
2297 else if (SvTYPE(sv) == SVt_PVHV) {
2298 cleanup_recursive_hv(aTHX_ (HV*)sv);
2301 cleanup_recursive_rv(aTHX_ sv);
2309 * Lookup the class name in the 'hclass' table and either assign it a new ID
2310 * or return the existing one, by filling in 'classnum'.
2312 * Return true if the class was known, false if the ID was just generated.
2314 static int known_class(pTHX_
2316 char *name, /* Class name */
2317 int len, /* Name length */
2321 HV *hclass = cxt->hclass;
2323 TRACEME(("known_class (%s)", name));
2326 * Recall that we don't store pointers in this hash table, but tags.
2327 * Therefore, we need LOW_32BITS() to extract the relevant parts.
2330 svh = hv_fetch(hclass, name, len, FALSE);
2332 *classnum = LOW_32BITS(*svh);
2337 * Unknown classname, we need to record it.
2341 if (!hv_store(hclass, name, len, INT2PTR(SV*, cxt->classnum), 0))
2342 CROAK(("Unable to record new classname"));
2344 *classnum = cxt->classnum;
2349 *** Specific store routines.
2355 * Store a reference.
2356 * Layout is SX_REF <object> or SX_OVERLOAD <object>.
2358 static int store_ref(pTHX_ stcxt_t *cxt, SV *sv)
2362 TRACEME(("store_ref (0x%" UVxf ")", PTR2UV(sv)));
2365 * Follow reference, and check if target is overloaded.
2371 TRACEME(("ref (0x%" UVxf ") is%s weak", PTR2UV(sv),
2372 is_weak ? "" : "n't"));
2377 HV *stash = (HV *) SvSTASH(sv);
2378 if (stash && Gv_AMG(stash)) {
2379 TRACEME(("ref (0x%" UVxf ") is overloaded", PTR2UV(sv)));
2380 PUTMARK(is_weak ? SX_WEAKOVERLOAD : SX_OVERLOAD);
2382 PUTMARK(is_weak ? SX_WEAKREF : SX_REF);
2384 PUTMARK(is_weak ? SX_WEAKREF : SX_REF);
2388 TRACEME((">ref recur_depth %" IVdf ", recur_sv (0x%" UVxf ") max %" IVdf, cxt->recur_depth,
2389 PTR2UV(cxt->recur_sv), cxt->max_recur_depth));
2390 if (RECURSION_TOO_DEEP()) {
2391 #if PERL_VERSION < 15
2392 cleanup_recursive_data(aTHX_ (SV*)sv);
2394 CROAK((MAX_DEPTH_ERROR));
2397 retval = store(aTHX_ cxt, sv);
2398 if (cxt->max_recur_depth != -1 && cxt->recur_depth > 0) {
2399 TRACEME(("<ref recur_depth --%" IVdf, cxt->recur_depth));
2410 * Layout is SX_LSCALAR <length> <data>, SX_SCALAR <length> <data> or SX_UNDEF.
2411 * SX_LUTF8STR and SX_UTF8STR are used for UTF-8 strings.
2412 * The <data> section is omitted if <length> is 0.
2414 * For vstrings, the vstring portion is stored first with
2415 * SX_LVSTRING <length> <data> or SX_VSTRING <length> <data>, followed by
2416 * SX_(L)SCALAR or SX_(L)UTF8STR with the actual PV.
2418 * If integer or double, the layout is SX_INTEGER <data> or SX_DOUBLE <data>.
2419 * Small integers (within [-127, +127]) are stored as SX_BYTE <byte>.
2421 * For huge strings use SX_LOBJECT SX_type SX_U64 <type> <data>
2423 static int store_scalar(pTHX_ stcxt_t *cxt, SV *sv)
2428 U32 flags = SvFLAGS(sv); /* "cc -O" may put it in register */
2430 TRACEME(("store_scalar (0x%" UVxf ")", PTR2UV(sv)));
2433 * For efficiency, break the SV encapsulation by peaking at the flags
2434 * directly without using the Perl macros to avoid dereferencing
2435 * sv->sv_flags each time we wish to check the flags.
2438 if (!(flags & SVf_OK)) { /* !SvOK(sv) */
2439 if (sv == &PL_sv_undef) {
2440 TRACEME(("immortal undef"));
2441 PUTMARK(SX_SV_UNDEF);
2443 TRACEME(("undef at 0x%" UVxf, PTR2UV(sv)));
2450 * Always store the string representation of a scalar if it exists.
2451 * Gisle Aas provided me with this test case, better than a long speach:
2453 * perl -MDevel::Peek -le '$a="abc"; $a+0; Dump($a)'
2454 * SV = PVNV(0x80c8520)
2456 * FLAGS = (NOK,POK,pNOK,pPOK)
2459 * PV = 0x80c83d0 "abc"\0
2463 * Write SX_SCALAR, length, followed by the actual data.
2465 * Otherwise, write an SX_BYTE, SX_INTEGER or an SX_DOUBLE as
2466 * appropriate, followed by the actual (binary) data. A double
2467 * is written as a string if network order, for portability.
2469 * NOTE: instead of using SvNOK(sv), we test for SvNOKp(sv).
2470 * The reason is that when the scalar value is tainted, the SvNOK(sv)
2473 * The test for a read-only scalar with both POK and NOK set is meant
2474 * to quickly detect &PL_sv_yes and &PL_sv_no without having to pay the
2475 * address comparison for each scalar we store.
2478 #define SV_MAYBE_IMMORTAL (SVf_READONLY|SVf_POK|SVf_NOK)
2480 if ((flags & SV_MAYBE_IMMORTAL) == SV_MAYBE_IMMORTAL) {
2481 if (sv == &PL_sv_yes) {
2482 TRACEME(("immortal yes"));
2484 } else if (sv == &PL_sv_no) {
2485 TRACEME(("immortal no"));
2488 pv = SvPV(sv, len); /* We know it's SvPOK */
2489 goto string; /* Share code below */
2491 } else if (flags & SVf_POK) {
2492 /* public string - go direct to string read. */
2493 goto string_readlen;
2495 #if (PATCHLEVEL <= 6)
2496 /* For 5.6 and earlier NV flag trumps IV flag, so only use integer
2497 direct if NV flag is off. */
2498 (flags & (SVf_NOK | SVf_IOK)) == SVf_IOK
2500 /* 5.7 rules are that if IV public flag is set, IV value is as
2501 good, if not better, than NV value. */
2507 * Will come here from below with iv set if double is an integer.
2511 /* Sorry. This isn't in 5.005_56 (IIRC) or earlier. */
2513 /* Need to do this out here, else 0xFFFFFFFF becomes iv of -1
2514 * (for example) and that ends up in the optimised small integer
2517 if ((flags & SVf_IVisUV) && SvUV(sv) > IV_MAX) {
2518 TRACEME(("large unsigned integer as string, value = %" UVuf,
2520 goto string_readlen;
2524 * Optimize small integers into a single byte, otherwise store as
2525 * a real integer (converted into network order if they asked).
2528 if (iv >= -128 && iv <= 127) {
2529 unsigned char siv = (unsigned char) (iv + 128); /* [0,255] */
2532 TRACEME(("small integer stored as %d", (int)siv));
2533 } else if (cxt->netorder) {
2535 TRACEME(("no htonl, fall back to string for integer"));
2536 goto string_readlen;
2544 /* Sorry. This isn't in 5.005_56 (IIRC) or earlier. */
2545 ((flags & SVf_IVisUV) && SvUV(sv) > (UV)0x7FFFFFFF) ||
2547 (iv > (IV)0x7FFFFFFF) || (iv < -(IV)0x80000000)) {
2548 /* Bigger than 32 bits. */
2549 TRACEME(("large network order integer as string, value = %" IVdf, iv));
2550 goto string_readlen;
2554 niv = (I32) htonl((I32) iv);
2555 TRACEME(("using network order"));
2560 PUTMARK(SX_INTEGER);
2561 WRITE(&iv, sizeof(iv));
2564 TRACEME(("ok (integer 0x%" UVxf ", value = %" IVdf ")", PTR2UV(sv), iv));
2565 } else if (flags & SVf_NOK) {
2568 /* if we can't tell if there's padding, clear the whole NV and hope the
2569 compiler leaves the padding alone
2571 Zero(&nv, 1, NV_bytes);
2573 #if (PATCHLEVEL <= 6)
2576 * Watch for number being an integer in disguise.
2578 if (nv.nv == (NV) (iv = I_V(nv.nv))) {
2579 TRACEME(("double %" NVff " is actually integer %" IVdf, nv, iv));
2580 goto integer; /* Share code above */
2585 if (SvIOK_notUV(sv)) {
2587 goto integer; /* Share code above */
2592 if (cxt->netorder) {
2593 TRACEME(("double %" NVff " stored as string", nv.nv));
2594 goto string_readlen; /* Share code below */
2597 Zero(nv.bytes + NVSIZE - NV_PADDING, NV_PADDING, char);
2601 WRITE(&nv, sizeof(nv));
2603 TRACEME(("ok (double 0x%" UVxf ", value = %" NVff ")", PTR2UV(sv), nv.nv));
2605 } else if (flags & (SVp_POK | SVp_NOK | SVp_IOK)) {
2609 UV wlen; /* For 64-bit machines */
2615 * Will come here from above if it was readonly, POK and NOK but
2616 * neither &PL_sv_yes nor &PL_sv_no.
2621 if (SvMAGICAL(sv) && (mg = mg_find(sv, 'V'))) {
2622 /* The macro passes this by address, not value, and a lot of
2623 called code assumes that it's 32 bits without checking. */
2624 const SSize_t len = mg->mg_len;
2625 STORE_PV_LEN((const char *)mg->mg_ptr,
2626 len, SX_VSTRING, SX_LVSTRING);
2632 STORE_UTF8STR(pv, wlen);
2634 STORE_SCALAR(pv, wlen);
2635 TRACEME(("ok (scalar 0x%" UVxf " '%s', length = %" UVuf ")",
2636 PTR2UV(sv), len >= 2048 ? "<string too long>" : SvPVX(sv),
2639 CROAK(("Can't determine type of %s(0x%" UVxf ")",
2640 sv_reftype(sv, FALSE),
2643 return 0; /* Ok, no recursion on scalars */
2651 * Layout is SX_ARRAY <size> followed by each item, in increasing index order.
2652 * Each item is stored as <object>.
2654 static int store_array(pTHX_ stcxt_t *cxt, AV *av)
2657 UV len = av_len(av) + 1;
2660 SV *const recur_sv = cxt->recur_sv;
2662 TRACEME(("store_array (0x%" UVxf ")", PTR2UV(av)));
2665 if (len > 0x7fffffffu) {
2667 * Large array by emitting SX_LOBJECT 1 U64 data
2669 PUTMARK(SX_LOBJECT);
2672 TRACEME(("lobject size = %lu", (unsigned long)len));
2677 * Normal array by emitting SX_ARRAY, followed by the array length.
2682 TRACEME(("size = %d", (int)l));
2685 TRACEME((">array recur_depth %" IVdf ", recur_sv (0x%" UVxf ") max %" IVdf, cxt->recur_depth,
2686 PTR2UV(cxt->recur_sv), cxt->max_recur_depth));
2687 if (recur_sv != (SV*)av) {
2688 if (RECURSION_TOO_DEEP()) {
2689 /* with <= 5.14 it recurses in the cleanup also, needing 2x stack size */
2690 #if PERL_VERSION < 15
2691 cleanup_recursive_data(aTHX_ (SV*)av);
2693 CROAK((MAX_DEPTH_ERROR));
2698 * Now store each item recursively.
2701 for (i = 0; i < len; i++) {
2702 sav = av_fetch(av, i, 0);
2704 TRACEME(("(#%d) nonexistent item", (int)i));
2708 #if PATCHLEVEL >= 19
2709 /* In 5.19.3 and up, &PL_sv_undef can actually be stored in
2710 * an array; it no longer represents nonexistent elements.
2711 * Historically, we have used SX_SV_UNDEF in arrays for
2712 * nonexistent elements, so we use SX_SVUNDEF_ELEM for
2713 * &PL_sv_undef itself. */
2714 if (*sav == &PL_sv_undef) {
2715 TRACEME(("(#%d) undef item", (int)i));
2717 PUTMARK(SX_SVUNDEF_ELEM);
2721 TRACEME(("(#%d) item", (int)i));
2722 if ((ret = store(aTHX_ cxt, *sav))) /* Extra () for -Wall */
2726 if (recur_sv != (SV*)av) {
2727 assert(cxt->max_recur_depth == -1 || cxt->recur_depth > 0);
2728 if (cxt->max_recur_depth != -1 && cxt->recur_depth > 0) {
2729 TRACEME(("<array recur_depth --%" IVdf, cxt->recur_depth));
2733 TRACEME(("ok (array)"));
2739 #if (PATCHLEVEL <= 6)
2745 * Borrowed from perl source file pp_ctl.c, where it is used by pp_sort.
2748 sortcmp(const void *a, const void *b)
2750 #if defined(USE_ITHREADS)
2752 #endif /* USE_ITHREADS */
2753 return sv_cmp(*(SV * const *) a, *(SV * const *) b);
2756 #endif /* PATCHLEVEL <= 6 */
2761 * Store a hash table.
2763 * For a "normal" hash (not restricted, no utf8 keys):
2765 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
2766 * Values are stored as <object>.
2767 * Keys are stored as <length> <data>, the <data> section being omitted
2770 * For a "fancy" hash (restricted or utf8 keys):
2772 * Layout is SX_FLAG_HASH <size> <hash flags> followed by each key/value pair,
2774 * Values are stored as <object>.
2775 * Keys are stored as <flags> <length> <data>, the <data> section being omitted
2777 * Currently the only hash flag is "restricted"
2778 * Key flags are as for hv.h
2780 static int store_hash(pTHX_ stcxt_t *cxt, HV *hv)
2783 UV len = (UV)HvTOTALKEYS(hv);
2788 int flagged_hash = ((SvREADONLY(hv)
2789 #ifdef HAS_HASH_KEY_FLAGS
2793 unsigned char hash_flags = (SvREADONLY(hv) ? SHV_RESTRICTED : 0);
2794 SV * const recur_sv = cxt->recur_sv;
2797 * Signal hash by emitting SX_HASH, followed by the table length.
2798 * Max number of keys per perl version:
2800 * STRLEN 5.14 - 5.24 (size_t: U32/U64)
2801 * SSize_t 5.22c - 5.24c (I32/I64)
2805 if (len > 0x7fffffffu) { /* keys > I32_MAX */
2807 * Large hash: SX_LOBJECT type hashflags? U64 data
2809 * Stupid limitation:
2810 * Note that perl5 can store more than 2G keys, but only iterate
2811 * over 2G max. (cperl can)
2812 * We need to manually iterate over it then, unsorted.
2813 * But until perl itself cannot do that, skip that.
2815 TRACEME(("lobject size = %lu", (unsigned long)len));
2817 PUTMARK(SX_LOBJECT);
2819 PUTMARK(SX_FLAG_HASH);
2820 PUTMARK(hash_flags);
2825 return store_lhash(aTHX_ cxt, hv, hash_flags);
2827 /* <5.12 you could store larger hashes, but cannot iterate over them.
2828 So we reject them, it's a bug. */
2829 CROAK(("Cannot store large objects on a 32bit system"));
2834 TRACEME(("store_hash (0x%" UVxf ") (flags %x)", PTR2UV(hv),
2835 (unsigned int)hash_flags));
2836 PUTMARK(SX_FLAG_HASH);
2837 PUTMARK(hash_flags);
2839 TRACEME(("store_hash (0x%" UVxf ")", PTR2UV(hv)));
2843 TRACEME(("size = %d, used = %d", (int)l, (int)HvUSEDKEYS(hv)));
2846 TRACEME((">hash recur_depth %" IVdf ", recur_sv (0x%" UVxf ") max %" IVdf, cxt->recur_depth,
2847 PTR2UV(cxt->recur_sv), cxt->max_recur_depth_hash));
2848 if (recur_sv != (SV*)hv && cxt->max_recur_depth_hash != -1) {
2851 if (RECURSION_TOO_DEEP_HASH()) {
2852 #if PERL_VERSION < 15
2853 cleanup_recursive_data(aTHX_ (SV*)hv);
2855 CROAK((MAX_DEPTH_ERROR));
2859 * Save possible iteration state via each() on that table.
2861 * Note that perl as of 5.24 *can* store more than 2G keys, but *not*
2863 * Lengths of hash keys are also limited to I32, which is good.
2866 riter = HvRITER_get(hv);
2867 eiter = HvEITER_get(hv);
2871 * Now store each item recursively.
2873 * If canonical is defined to some true value then store each
2874 * key/value pair in sorted order otherwise the order is random.
2875 * Canonical order is irrelevant when a deep clone operation is performed.
2877 * Fetch the value from perl only once per store() operation, and only
2882 !(cxt->optype & ST_CLONE)
2883 && (cxt->canonical == 1
2884 || (cxt->canonical < 0
2885 && (cxt->canonical =
2886 (SvTRUE(get_sv("Storable::canonical", GV_ADD))
2890 * Storing in order, sorted by key.
2891 * Run through the hash, building up an array of keys in a
2892 * mortal array, sort the array and then run through the
2896 av_extend (av, len);
2898 TRACEME(("using canonical order"));
2900 for (i = 0; i < len; i++) {
2901 #ifdef HAS_RESTRICTED_HASHES
2902 HE *he = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS);
2904 HE *he = hv_iternext(hv);
2906 av_store(av, i, hv_iterkeysv(he));
2911 for (i = 0; i < len; i++) {
2912 #ifdef HAS_RESTRICTED_HASHES
2913 int placeholders = (int)HvPLACEHOLDERS_get(hv);
2915 unsigned char flags = 0;
2919 SV *key = av_shift(av);
2920 /* This will fail if key is a placeholder.
2921 Track how many placeholders we have, and error if we
2923 HE *he = hv_fetch_ent(hv, key, 0, 0);
2927 if (!(val = HeVAL(he))) {
2928 /* Internal error, not I/O error */
2932 #ifdef HAS_RESTRICTED_HASHES
2933 /* Should be a placeholder. */
2934 if (placeholders-- < 0) {
2935 /* This should not happen - number of
2936 retrieves should be identical to
2937 number of placeholders. */
2940 /* Value is never needed, and PL_sv_undef is
2941 more space efficient to store. */
2944 ("Flags not 0 but %d", (int)flags));
2945 flags = SHV_K_PLACEHOLDER;
2952 * Store value first.
2955 TRACEME(("(#%d) value 0x%" UVxf, (int)i, PTR2UV(val)));
2957 if ((ret = store(aTHX_ cxt, val))) /* Extra () for -Wall, grr... */
2962 * Keys are written after values to make sure retrieval
2963 * can be optimal in terms of memory usage, where keys are
2964 * read into a fixed unique buffer called kbuf.
2965 * See retrieve_hash() for details.
2968 /* Implementation of restricted hashes isn't nicely
2970 if ((hash_flags & SHV_RESTRICTED)
2971 && SvTRULYREADONLY(val)) {
2972 flags |= SHV_K_LOCKED;
2975 keyval = SvPV(key, keylen_tmp);
2976 keylen = keylen_tmp;
2977 #ifdef HAS_UTF8_HASHES
2978 /* If you build without optimisation on pre 5.6
2979 then nothing spots that SvUTF8(key) is always 0,
2980 so the block isn't optimised away, at which point
2981 the linker dislikes the reference to
2984 const char *keysave = keyval;
2985 bool is_utf8 = TRUE;
2987 /* Just casting the &klen to (STRLEN) won't work
2988 well if STRLEN and I32 are of different widths.
2990 keyval = (char*)bytes_from_utf8((U8*)keyval,
2994 /* If we were able to downgrade here, then than
2995 means that we have a key which only had chars
2996 0-255, but was utf8 encoded. */
2998 if (keyval != keysave) {
2999 keylen = keylen_tmp;
3000 flags |= SHV_K_WASUTF8;
3002 /* keylen_tmp can't have changed, so no need
3003 to assign back to keylen. */
3004 flags |= SHV_K_UTF8;
3011 TRACEME(("(#%d) key '%s' flags %x %u", (int)i, keyval, flags, *keyval));
3013 /* This is a workaround for a bug in 5.8.0
3014 that causes the HEK_WASUTF8 flag to be
3015 set on an HEK without the hash being
3016 marked as having key flags. We just
3017 cross our fingers and drop the flag.
3019 assert (flags == 0 || flags == SHV_K_WASUTF8);
3020 TRACEME(("(#%d) key '%s'", (int)i, keyval));
3024 WRITE(keyval, keylen);
3025 if (flags & SHV_K_WASUTF8)
3030 * Free up the temporary array
3039 * Storing in "random" order (in the order the keys are stored
3040 * within the hash). This is the default and will be faster!
3043 for (i = 0; i < len; i++) {
3044 #ifdef HV_ITERNEXT_WANTPLACEHOLDERS
3045 HE *he = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS);
3047 HE *he = hv_iternext(hv);
3049 SV *val = (he ? hv_iterval(hv, he) : 0);
3052 return 1; /* Internal error, not I/O error */
3054 if ((ret = store_hentry(aTHX_ cxt, hv, i, he, hash_flags)))
3057 /* Implementation of restricted hashes isn't nicely
3059 flags = (((hash_flags & SHV_RESTRICTED)
3060 && SvTRULYREADONLY(val))
3061 ? SHV_K_LOCKED : 0);
3063 if (val == &PL_sv_placeholder) {
3064 flags |= SHV_K_PLACEHOLDER;
3069 * Store value first.
3072 TRACEME(("(#%d) value 0x%" UVxf, (int)i, PTR2UV(val)));
3074 if ((ret = store(aTHX_ cxt, val))) /* Extra () for -Wall */
3078 hek = HeKEY_hek(he);
3080 if (len == HEf_SVKEY) {
3081 /* This is somewhat sick, but the internal APIs are
3082 * such that XS code could put one of these in in
3084 * Maybe we should be capable of storing one if
3087 key_sv = HeKEY_sv(he);
3088 flags |= SHV_K_ISSV;
3090 /* Regular string key. */
3091 #ifdef HAS_HASH_KEY_FLAGS
3093 flags |= SHV_K_UTF8;
3094 if (HEK_WASUTF8(hek))
3095 flags |= SHV_K_WASUTF8;
3101 * Keys are written after values to make sure retrieval
3102 * can be optimal in terms of memory usage, where keys are
3103 * read into a fixed unique buffer called kbuf.
3104 * See retrieve_hash() for details.
3109 TRACEME(("(#%d) key '%s' flags %x", (int)i, key, flags));
3111 /* This is a workaround for a bug in 5.8.0
3112 that causes the HEK_WASUTF8 flag to be
3113 set on an HEK without the hash being
3114 marked as having key flags. We just
3115 cross our fingers and drop the flag.
3117 assert (flags == 0 || flags == SHV_K_WASUTF8);
3118 TRACEME(("(#%d) key '%s'", (int)i, key));
3120 if (flags & SHV_K_ISSV) {
3122 if ((ret = store(aTHX_ cxt, key_sv)))
3133 TRACEME(("ok (hash 0x%" UVxf ")", PTR2UV(hv)));
3136 assert(cxt->max_recur_depth_hash != -1 && cxt->recur_depth > 0);
3137 TRACEME(("<hash recur_depth --%" IVdf , cxt->recur_depth));
3138 if (cxt->max_recur_depth_hash != -1 && recur_sv != (SV*)hv && cxt->recur_depth > 0) {
3141 HvRITER_set(hv, riter); /* Restore hash iterator state */
3142 HvEITER_set(hv, eiter);
3147 static int store_hentry(pTHX_
3148 stcxt_t *cxt, HV* hv, UV i, HE *he, unsigned char hash_flags)
3151 SV* val = hv_iterval(hv, he);
3152 int flagged_hash = ((SvREADONLY(hv)
3153 #ifdef HAS_HASH_KEY_FLAGS
3157 unsigned char flags = (((hash_flags & SHV_RESTRICTED)
3158 && SvTRULYREADONLY(val))
3159 ? SHV_K_LOCKED : 0);
3163 if (val == &PL_sv_placeholder) {
3164 flags |= SHV_K_PLACEHOLDER;
3169 * Store value first.
3172 TRACEME(("(#%d) value 0x%" UVxf, (int)i, PTR2UV(val)));
3175 HEK* hek = HeKEY_hek(he);
3176 I32 len = HEK_LEN(hek);
3180 if ((ret = store(aTHX_ cxt, val)))
3182 if (len == HEf_SVKEY) {
3183 key_sv = HeKEY_sv(he);
3184 flags |= SHV_K_ISSV;
3186 /* Regular string key. */
3187 #ifdef HAS_HASH_KEY_FLAGS
3189 flags |= SHV_K_UTF8;
3190 if (HEK_WASUTF8(hek))
3191 flags |= SHV_K_WASUTF8;
3197 * Keys are written after values to make sure retrieval
3198 * can be optimal in terms of memory usage, where keys are
3199 * read into a fixed unique buffer called kbuf.
3200 * See retrieve_hash() for details.
3205 TRACEME(("(#%d) key '%s' flags %x", (int)i, key, flags));
3207 /* This is a workaround for a bug in 5.8.0
3208 that causes the HEK_WASUTF8 flag to be
3209 set on an HEK without the hash being
3210 marked as having key flags. We just
3211 cross our fingers and drop the flag.
3213 assert (flags == 0 || flags == SHV_K_WASUTF8);
3214 TRACEME(("(#%d) key '%s'", (int)i, key));
3216 if (flags & SHV_K_ISSV) {
3217 if ((ret = store(aTHX_ cxt, key_sv)))
3233 * Store a overlong hash table, with >2G keys, which we cannot iterate
3234 * over with perl5. xhv_eiter is only I32 there. (only cperl can)
3235 * and we also do not want to sort it.
3236 * So we walk the buckets and chains manually.
3238 * type, len and flags are already written.
3241 static int store_lhash(pTHX_ stcxt_t *cxt, HV *hv, unsigned char hash_flags)
3249 UV len = (UV)HvTOTALKEYS(hv);
3251 SV * const recur_sv = cxt->recur_sv;
3253 TRACEME(("store_lhash (0x%" UVxf ") (flags %x)", PTR2UV(hv),
3256 TRACEME(("store_lhash (0x%" UVxf ")", PTR2UV(hv)));
3258 TRACEME(("size = %" UVuf ", used = %" UVuf, len, (UV)HvUSEDKEYS(hv)));
3260 TRACEME(("recur_depth %" IVdf ", recur_sv (0x%" UVxf ")", cxt->recur_depth,
3261 PTR2UV(cxt->recur_sv)));
3262 if (recur_sv != (SV*)hv && cxt->max_recur_depth_hash != -1) {
3265 if (RECURSION_TOO_DEEP_HASH()) {
3266 #if PERL_VERSION < 15
3267 cleanup_recursive_data(aTHX_ (SV*)hv);
3269 CROAK((MAX_DEPTH_ERROR));
3272 array = HvARRAY(hv);
3273 for (i = 0; i <= (Size_t)HvMAX(hv); i++) {
3274 HE* entry = array[i];
3275 if (!entry) continue;
3276 if ((ret = store_hentry(aTHX_ cxt, hv, ix++, entry, hash_flags)))
3278 while ((entry = HeNEXT(entry))) {
3279 if ((ret = store_hentry(aTHX_ cxt, hv, ix++, entry, hash_flags)))
3283 if (recur_sv == (SV*)hv && cxt->max_recur_depth_hash != -1 && cxt->recur_depth > 0) {
3284 TRACEME(("recur_depth --%" IVdf, cxt->recur_depth));
3295 * Store a code reference.
3297 * Layout is SX_CODE <length> followed by a scalar containing the perl
3298 * source code of the code reference.
3300 static int store_code(pTHX_ stcxt_t *cxt, CV *cv)
3302 #if PERL_VERSION < 6
3304 * retrieve_code does not work with perl 5.005 or less
3306 return store_other(aTHX_ cxt, (SV*)cv);
3310 STRLEN count, reallen;
3311 SV *text, *bdeparse;
3313 TRACEME(("store_code (0x%" UVxf ")", PTR2UV(cv)));
3316 cxt->deparse == 0 ||
3317 (cxt->deparse < 0 &&
3319 SvTRUE(get_sv("Storable::Deparse", GV_ADD)) ? 1 : 0))
3321 return store_other(aTHX_ cxt, (SV*)cv);
3325 * Require B::Deparse. At least B::Deparse 0.61 is needed for
3326 * blessed code references.
3328 /* Ownership of both SVs is passed to load_module, which frees them. */
3329 load_module(PERL_LOADMOD_NOIMPORT, newSVpvs("B::Deparse"), newSVnv(0.61));
3336 * create the B::Deparse object
3340 XPUSHs(newSVpvs_flags("B::Deparse", SVs_TEMP));
3342 count = call_method("new", G_SCALAR);
3345 CROAK(("Unexpected return value from B::Deparse::new\n"));
3349 * call the coderef2text method
3353 XPUSHs(bdeparse); /* XXX is this already mortal? */
3354 XPUSHs(sv_2mortal(newRV_inc((SV*)cv)));
3356 count = call_method("coderef2text", G_SCALAR);
3359 CROAK(("Unexpected return value from B::Deparse::coderef2text\n"));
3363 reallen = strlen(SvPV_nolen(text));
3366 * Empty code references or XS functions are deparsed as
3367 * "(prototype) ;" or ";".
3370 if (len == 0 || *(SvPV_nolen(text)+reallen-1) == ';') {
3371 CROAK(("The result of B::Deparse::coderef2text was empty - maybe you're trying to serialize an XS function?\n"));
3375 * Signal code by emitting SX_CODE.
3379 cxt->tagnum++; /* necessary, as SX_CODE is a SEEN() candidate */
3380 TRACEME(("size = %d", (int)len));
3381 TRACEME(("code = %s", SvPV_nolen(text)));
3384 * Now store the source code.
3388 STORE_UTF8STR(SvPV_nolen(text), len);
3390 STORE_SCALAR(SvPV_nolen(text), len);
3395 TRACEME(("ok (code)"));
3401 #if PERL_VERSION < 8
3402 # define PERL_MAGIC_qr 'r' /* precompiled qr// regex */
3403 # define BFD_Svs_SMG_OR_RMG SVs_RMG
3404 #elif ((PERL_VERSION==8) && (PERL_SUBVERSION >= 1) || (PERL_VERSION>8))
3405 # define BFD_Svs_SMG_OR_RMG SVs_SMG
3406 # define MY_PLACEHOLDER PL_sv_placeholder
3408 # define BFD_Svs_SMG_OR_RMG SVs_RMG
3409 # define MY_PLACEHOLDER PL_sv_undef
3412 static int get_regexp(pTHX_ stcxt_t *cxt, SV* sv, SV **re, SV **flags) {
3415 #if PERL_VERSION >= 12
3416 CV *cv = get_cv("re::regexp_pattern", 0);
3418 CV *cv = get_cv("Storable::_regexp_pattern", 0);
3426 rv = sv_2mortal((SV*)newRV_inc(sv));
3430 /* optimize to call the XS directly later */
3431 count = call_sv((SV*)cv, G_ARRAY);
3434 CROAK(("re::regexp_pattern returned only %d results", count));
3436 SvREFCNT_inc(*flags);
3447 static int store_regexp(pTHX_ stcxt_t *cxt, SV *sv) {
3451 const char *flags_pv;
3456 if (!get_regexp(aTHX_ cxt, sv, &re, &flags))
3459 re_pv = SvPV(re, re_len);
3460 flags_pv = SvPV(flags, flags_len);
3462 if (re_len > 0xFF) {
3463 op_flags |= SHR_U32_RE_LEN;
3468 if (op_flags & SHR_U32_RE_LEN) {
3469 U32 re_len32 = re_len;
3474 WRITE(re_pv, re_len);
3476 WRITE(flags_pv, flags_len);
3484 * When storing a tied object (be it a tied scalar, array or hash), we lay out
3485 * a special mark, followed by the underlying tied object. For instance, when
3486 * dealing with a tied hash, we store SX_TIED_HASH <hash object>, where
3487 * <hash object> stands for the serialization of the tied hash.
3489 static int store_tied(pTHX_ stcxt_t *cxt, SV *sv)
3494 int svt = SvTYPE(sv);
3497 TRACEME(("store_tied (0x%" UVxf ")", PTR2UV(sv)));
3500 * We have a small run-time penalty here because we chose to factorise
3501 * all tieds objects into the same routine, and not have a store_tied_hash,
3502 * a store_tied_array, etc...
3504 * Don't use a switch() statement, as most compilers don't optimize that
3505 * well for 2/3 values. An if() else if() cascade is just fine. We put
3506 * tied hashes first, as they are the most likely beasts.
3509 if (svt == SVt_PVHV) {
3510 TRACEME(("tied hash"));
3511 PUTMARK(SX_TIED_HASH); /* Introduces tied hash */
3512 } else if (svt == SVt_PVAV) {
3513 TRACEME(("tied array"));
3514 PUTMARK(SX_TIED_ARRAY); /* Introduces tied array */
3516 TRACEME(("tied scalar"));
3517 PUTMARK(SX_TIED_SCALAR); /* Introduces tied scalar */
3521 if (!(mg = mg_find(sv, mtype)))
3522 CROAK(("No magic '%c' found while storing tied %s", mtype,
3523 (svt == SVt_PVHV) ? "hash" :
3524 (svt == SVt_PVAV) ? "array" : "scalar"));
3527 * The mg->mg_obj found by mg_find() above actually points to the
3528 * underlying tied Perl object implementation. For instance, if the
3529 * original SV was that of a tied array, then mg->mg_obj is an AV.
3531 * Note that we store the Perl object as-is. We don't call its FETCH
3532 * method along the way. At retrieval time, we won't call its STORE
3533 * method either, but the tieing magic will be re-installed. In itself,
3534 * that ensures that the tieing semantics are preserved since further
3535 * accesses on the retrieved object will indeed call the magic methods...
3538 /* [#17040] mg_obj is NULL for scalar self-ties. AMS 20030416 */
3539 obj = mg->mg_obj ? mg->mg_obj : newSV(0);
3540 if ((ret = store(aTHX_ cxt, obj)))
3543 TRACEME(("ok (tied)"));
3551 * Stores a reference to an item within a tied structure:
3553 * . \$h{key}, stores both the (tied %h) object and 'key'.
3554 * . \$a[idx], stores both the (tied @a) object and 'idx'.
3556 * Layout is therefore either:
3557 * SX_TIED_KEY <object> <key>
3558 * SX_TIED_IDX <object> <index>
3560 static int store_tied_item(pTHX_ stcxt_t *cxt, SV *sv)
3565 TRACEME(("store_tied_item (0x%" UVxf ")", PTR2UV(sv)));
3567 if (!(mg = mg_find(sv, 'p')))
3568 CROAK(("No magic 'p' found while storing reference to tied item"));
3571 * We discriminate between \$h{key} and \$a[idx] via mg_ptr.
3575 TRACEME(("store_tied_item: storing a ref to a tied hash item"));
3576 PUTMARK(SX_TIED_KEY);
3577 TRACEME(("store_tied_item: storing OBJ 0x%" UVxf, PTR2UV(mg->mg_obj)));
3579 if ((ret = store(aTHX_ cxt, mg->mg_obj))) /* Extra () for -Wall, grr... */
3582 TRACEME(("store_tied_item: storing PTR 0x%" UVxf, PTR2UV(mg->mg_ptr)));
3584 if ((ret = store(aTHX_ cxt, (SV *) mg->mg_ptr))) /* Idem, for -Wall */
3587 I32 idx = mg->mg_len;
3589 TRACEME(("store_tied_item: storing a ref to a tied array item "));
3590 PUTMARK(SX_TIED_IDX);
3591 TRACEME(("store_tied_item: storing OBJ 0x%" UVxf, PTR2UV(mg->mg_obj)));
3593 if ((ret = store(aTHX_ cxt, mg->mg_obj))) /* Idem, for -Wall */
3596 TRACEME(("store_tied_item: storing IDX %d", (int)idx));
3601 TRACEME(("ok (tied item)"));
3607 * store_hook -- dispatched manually, not via sv_store[]
3609 * The blessed SV is serialized by a hook.
3613 * SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
3615 * where <flags> indicates how long <len>, <len2> and <len3> are, whether
3616 * the trailing part [] is present, the type of object (scalar, array or hash).
3617 * There is also a bit which says how the classname is stored between:
3622 * and when the <index> form is used (classname already seen), the "large
3623 * classname" bit in <flags> indicates how large the <index> is.
3625 * The serialized string returned by the hook is of length <len2> and comes
3626 * next. It is an opaque string for us.
3628 * Those <len3> object IDs which are listed last represent the extra references
3629 * not directly serialized by the hook, but which are linked to the object.
3631 * When recursion is mandated to resolve object-IDs not yet seen, we have
3632 * instead, with <header> being flags with bits set to indicate the object type
3633 * and that recursion was indeed needed:
3635 * SX_HOOK <header> <object> <header> <object> <flags>
3637 * that same header being repeated between serialized objects obtained through
3638 * recursion, until we reach flags indicating no recursion, at which point
3639 * we know we've resynchronized with a single layout, after <flags>.
3641 * When storing a blessed ref to a tied variable, the following format is
3644 * SX_HOOK <flags> <extra> ... [<len3> <object-IDs>] <magic object>
3646 * The first <flags> indication carries an object of type SHT_EXTRA, and the
3647 * real object type is held in the <extra> flag. At the very end of the
3648 * serialization stream, the underlying magic object is serialized, just like
3649 * any other tied variable.
3651 static int store_hook(
3665 IV count; /* really len3 + 1 */
3666 unsigned char flags;
3669 int recursed = 0; /* counts recursion */
3670 int obj_type; /* object type, on 2 bits */
3673 int clone = cxt->optype & ST_CLONE;
3674 char mtype = '\0'; /* for blessed ref to tied structures */
3675 unsigned char eflags = '\0'; /* used when object type is SHT_EXTRA */
3677 int need_large_oids = 0;
3680 TRACEME(("store_hook, classname \"%s\", tagged #%d", HvNAME_get(pkg), (int)cxt->tagnum));
3683 * Determine object type on 2 bits.
3689 obj_type = SHT_SCALAR;
3692 obj_type = SHT_ARRAY;
3695 obj_type = SHT_HASH;
3699 * Produced by a blessed ref to a tied data structure, $o in the
3700 * following Perl code.
3704 * my $o = bless \%h, 'BAR';
3706 * Signal the tie-ing magic by setting the object type as SHT_EXTRA
3707 * (since we have only 2 bits in <flags> to store the type), and an
3708 * <extra> byte flag will be emitted after the FIRST <flags> in the
3709 * stream, carrying what we put in 'eflags'.
3711 obj_type = SHT_EXTRA;
3712 switch (SvTYPE(sv)) {
3714 eflags = (unsigned char) SHT_THASH;
3718 eflags = (unsigned char) SHT_TARRAY;
3722 eflags = (unsigned char) SHT_TSCALAR;
3728 CROAK(("Unexpected object type (%d) in store_hook()", type));
3730 flags = SHF_NEED_RECURSE | obj_type;
3732 classname = HvNAME_get(pkg);
3733 len = strlen(classname);
3736 * To call the hook, we need to fake a call like:
3738 * $object->STORABLE_freeze($cloning);
3740 * but we don't have the $object here. For instance, if $object is
3741 * a blessed array, what we have in 'sv' is the array, and we can't
3742 * call a method on those.
3744 * Therefore, we need to create a temporary reference to the object and
3745 * make the call on that reference.
3748 TRACEME(("about to call STORABLE_freeze on class %s", classname));
3750 ref = newRV_inc(sv); /* Temporary reference */
3751 av = array_call(aTHX_ ref, hook, clone); /* @a = $object->STORABLE_freeze($c) */
3752 SvREFCNT_dec(ref); /* Reclaim temporary reference */
3754 count = AvFILLp(av) + 1;
3755 TRACEME(("store_hook, array holds %" IVdf " items", count));
3758 * If they return an empty list, it means they wish to ignore the
3759 * hook for this class (and not just this instance -- that's for them
3760 * to handle if they so wish).
3762 * Simply disable the cached entry for the hook (it won't be recomputed
3763 * since it's present in the cache) and recurse to store_blessed().
3767 /* free empty list returned by the hook */
3772 * They must not change their mind in the middle of a serialization.
3775 if (hv_fetch(cxt->hclass, classname, len, FALSE))
3776 CROAK(("Too late to ignore hooks for %s class \"%s\"",
3777 (cxt->optype & ST_CLONE) ? "cloning" : "storing",
3780 pkg_hide(aTHX_ cxt->hook, pkg, "STORABLE_freeze");
3782 ASSERT(!pkg_can(aTHX_ cxt->hook, pkg, "STORABLE_freeze"),
3783 ("hook invisible"));
3784 TRACEME(("ignoring STORABLE_freeze in class \"%s\"", classname));
3786 return store_blessed(aTHX_ cxt, sv, type, pkg);
3790 * Get frozen string.
3794 pv = SvPV(ary[0], len2);
3795 /* We can't use pkg_can here because it only caches one method per
3798 GV* gv = gv_fetchmethod_autoload(pkg, "STORABLE_attach", FALSE);
3799 if (gv && isGV(gv)) {
3801 CROAK(("Freeze cannot return references if %s class is using STORABLE_attach", classname));
3807 if (count > I32_MAX) {
3808 CROAK(("Too many references returned by STORABLE_freeze()"));
3813 * If they returned more than one item, we need to serialize some
3814 * extra references if not already done.
3816 * Loop over the array, starting at position #1, and for each item,
3817 * ensure it is a reference, serialize it if not already done, and
3818 * replace the entry with the tag ID of the corresponding serialized
3821 * We CHEAT by not calling av_fetch() and read directly within the
3825 for (i = 1; i < count; i++) {
3826 #ifdef USE_PTR_TABLE
3834 AV *av_hook = cxt->hook_seen;
3837 CROAK(("Item #%d returned by STORABLE_freeze "
3838 "for %s is not a reference", (int)i, classname));
3839 xsv = SvRV(rsv); /* Follow ref to know what to look for */
3842 * Look in hseen and see if we have a tag already.
3843 * Serialize entry if not done already, and get its tag.
3846 #ifdef USE_PTR_TABLE
3847 /* Fakery needed because ptr_table_fetch returns zero for a
3848 failure, whereas the existing code assumes that it can
3849 safely store a tag zero. So for ptr_tables we store tag+1
3851 if ((fake_tag = (char *)ptr_table_fetch(cxt->pseen, xsv)))
3852 goto sv_seen; /* Avoid moving code too far to the right */
3854 if ((svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE)))
3855 goto sv_seen; /* Avoid moving code too far to the right */
3858 TRACEME(("listed object %d at 0x%" UVxf " is unknown", i-1,
3862 * We need to recurse to store that object and get it to be known
3863 * so that we can resolve the list of object-IDs at retrieve time.
3865 * The first time we do this, we need to emit the proper header
3866 * indicating that we recursed, and what the type of object is (the
3867 * object we're storing via a user-hook). Indeed, during retrieval,
3868 * we'll have to create the object before recursing to retrieve the
3869 * others, in case those would point back at that object.
3872 /* [SX_HOOK] <flags> [<extra>] <object>*/
3875 if (len2 > INT32_MAX)
3876 PUTMARK(SX_LOBJECT);
3880 if (obj_type == SHT_EXTRA)
3885 if ((ret = store(aTHX_ cxt, xsv))) /* Given by hook for us to store */
3888 #ifdef USE_PTR_TABLE
3889 fake_tag = (char *)ptr_table_fetch(cxt->pseen, xsv);
3891 CROAK(("Could not serialize item #%d from hook in %s",
3892 (int)i, classname));
3894 svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE);
3896 CROAK(("Could not serialize item #%d from hook in %s",
3897 (int)i, classname));
3900 * It was the first time we serialized 'xsv'.
3902 * Keep this SV alive until the end of the serialization: if we
3903 * disposed of it right now by decrementing its refcount, and it was
3904 * a temporary value, some next temporary value allocated during
3905 * another STORABLE_freeze might take its place, and we'd wrongly
3906 * assume that new SV was already serialized, based on its presence
3909 * Therefore, push it away in cxt->hook_seen.
3912 av_store(av_hook, AvFILLp(av_hook)+1, SvREFCNT_inc(xsv));
3916 * Dispose of the REF they returned. If we saved the 'xsv' away
3917 * in the array of returned SVs, that will not cause the underlying
3918 * referenced SV to be reclaimed.
3921 ASSERT(SvREFCNT(xsv) > 1, ("SV will survive disposal of its REF"));
3922 SvREFCNT_dec(rsv); /* Dispose of reference */
3925 * Replace entry with its tag (not a real SV, so no refcnt increment)
3928 #ifdef USE_PTR_TABLE
3929 tag = (SV *)--fake_tag;
3934 TRACEME(("listed object %d at 0x%" UVxf " is tag #%" UVuf,
3935 i-1, PTR2UV(xsv), PTR2UV(tag)));
3937 if ((U32)PTR2TAG(tag) != PTR2TAG(tag))
3938 need_large_oids = 1;
3943 * Allocate a class ID if not already done.
3945 * This needs to be done after the recursion above, since at retrieval
3946 * time, we'll see the inner objects first. Many thanks to
3947 * Salvador Ortiz Garcia <sog@msg.com.mx> who spot that bug and
3948 * proposed the right fix. -- RAM, 15/09/2000
3952 if (!known_class(aTHX_ cxt, classname, len, &classnum)) {
3953 TRACEME(("first time we see class %s, ID = %d", classname, (int)classnum));
3954 classnum = -1; /* Mark: we must store classname */
3956 TRACEME(("already seen class %s, ID = %d", classname, (int)classnum));
3960 * Compute leading flags.
3964 if (((classnum == -1) ? len : classnum) > LG_SCALAR)
3965 flags |= SHF_LARGE_CLASSLEN;
3967 flags |= SHF_IDX_CLASSNAME;
3968 if (len2 > LG_SCALAR)
3969 flags |= SHF_LARGE_STRLEN;
3971 flags |= SHF_HAS_LIST;
3972 if (count > (LG_SCALAR + 1))
3973 flags |= SHF_LARGE_LISTLEN;
3975 if (need_large_oids)
3976 flags |= SHF_LARGE_LISTLEN;
3980 * We're ready to emit either serialized form:
3982 * SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
3983 * SX_HOOK <flags> <index> <len2> <str> [<len3> <object-IDs>]
3985 * If we recursed, the SX_HOOK has already been emitted.
3988 TRACEME(("SX_HOOK (recursed=%d) flags=0x%x "
3989 "class=%" IVdf " len=%" IVdf " len2=%" IVdf " len3=%" IVdf,
3990 recursed, flags, (IV)classnum, (IV)len, (IV)len2, count-1));
3992 /* SX_HOOK <flags> [<extra>] */
3995 if (len2 > INT32_MAX)
3996 PUTMARK(SX_LOBJECT);
4000 if (obj_type == SHT_EXTRA)
4005 /* <len> <classname> or <index> */
4006 if (flags & SHF_IDX_CLASSNAME) {
4007 if (flags & SHF_LARGE_CLASSLEN)
4010 unsigned char cnum = (unsigned char) classnum;
4014 if (flags & SHF_LARGE_CLASSLEN)
4017 unsigned char clen = (unsigned char) len;
4020 WRITE(classname, len); /* Final \0 is omitted */
4023 /* <len2> <frozen-str> */
4025 if (len2 > INT32_MAX) {
4030 if (flags & SHF_LARGE_STRLEN) {
4031 U32 wlen2 = len2; /* STRLEN might be 8 bytes */
4032 WLEN(wlen2); /* Must write an I32 for 64-bit machines */
4034 unsigned char clen = (unsigned char) len2;
4038 WRITE(pv, (SSize_t)len2); /* Final \0 is omitted */
4040 /* [<len3> <object-IDs>] */
4041 if (flags & SHF_HAS_LIST) {
4042 int len3 = count - 1;
4043 if (flags & SHF_LARGE_LISTLEN) {
4045 int tlen3 = need_large_oids ? -len3 : len3;
4052 unsigned char clen = (unsigned char) len3;
4057 * NOTA BENE, for 64-bit machines: the ary[i] below does not yield a
4058 * real pointer, rather a tag number, well under the 32-bit limit.
4059 * Which is wrong... if we have more than 2**32 SVs we can get ids over
4063 for (i = 1; i < count; i++) {
4065 if (need_large_oids) {
4066 ntag_t tag = PTR2TAG(ary[i]);
4068 TRACEME(("object %d, tag #%" UVuf, i-1, (UV)tag));
4073 I32 tagval = htonl(LOW_32BITS(ary[i]));
4075 TRACEME(("object %d, tag #%d", i-1, ntohl(tagval)));
4081 * Free the array. We need extra care for indices after 0, since they
4082 * don't hold real SVs but integers cast.
4086 AvFILLp(av) = 0; /* Cheat, nothing after 0 interests us */
4091 * If object was tied, need to insert serialization of the magic object.
4094 if (obj_type == SHT_EXTRA) {
4097 if (!(mg = mg_find(sv, mtype))) {
4098 int svt = SvTYPE(sv);
4099 CROAK(("No magic '%c' found while storing ref to tied %s with hook",
4100 mtype, (svt == SVt_PVHV) ? "hash" :
4101 (svt == SVt_PVAV) ? "array" : "scalar"));
4104 TRACEME(("handling the magic object 0x%" UVxf " part of 0x%" UVxf,
4105 PTR2UV(mg->mg_obj), PTR2UV(sv)));
4110 if ((ret = store(aTHX_ cxt, mg->mg_obj)))
4118 * store_blessed -- dispatched manually, not via sv_store[]
4120 * Check whether there is a STORABLE_xxx hook defined in the class or in one
4121 * of its ancestors. If there is, then redispatch to store_hook();
4123 * Otherwise, the blessed SV is stored using the following layout:
4125 * SX_BLESS <flag> <len> <classname> <object>
4127 * where <flag> indicates whether <len> is stored on 0 or 4 bytes, depending
4128 * on the high-order bit in flag: if 1, then length follows on 4 bytes.
4129 * Otherwise, the low order bits give the length, thereby giving a compact
4130 * representation for class names less than 127 chars long.
4132 * Each <classname> seen is remembered and indexed, so that the next time
4133 * an object in the blessed in the same <classname> is stored, the following
4136 * SX_IX_BLESS <flag> <index> <object>
4138 * where <index> is the classname index, stored on 0 or 4 bytes depending
4139 * on the high-order bit in flag (same encoding as above for <len>).
4141 static int store_blessed(
4153 TRACEME(("store_blessed, type %d, class \"%s\"", type, HvNAME_get(pkg)));
4156 * Look for a hook for this blessed SV and redirect to store_hook()
4160 hook = pkg_can(aTHX_ cxt->hook, pkg, "STORABLE_freeze");
4162 return store_hook(aTHX_ cxt, sv, type, pkg, hook);
4165 * This is a blessed SV without any serialization hook.
4168 classname = HvNAME_get(pkg);
4169 len = strlen(classname);
4171 TRACEME(("blessed 0x%" UVxf " in %s, no hook: tagged #%d",
4172 PTR2UV(sv), classname, (int)cxt->tagnum));
4175 * Determine whether it is the first time we see that class name (in which
4176 * case it will be stored in the SX_BLESS form), or whether we already
4177 * saw that class name before (in which case the SX_IX_BLESS form will be
4181 if (known_class(aTHX_ cxt, classname, len, &classnum)) {
4182 TRACEME(("already seen class %s, ID = %d", classname, (int)classnum));
4183 PUTMARK(SX_IX_BLESS);
4184 if (classnum <= LG_BLESS) {
4185 unsigned char cnum = (unsigned char) classnum;
4188 unsigned char flag = (unsigned char) 0x80;
4193 TRACEME(("first time we see class %s, ID = %d", classname,
4196 if (len <= LG_BLESS) {
4197 unsigned char clen = (unsigned char) len;
4200 unsigned char flag = (unsigned char) 0x80;
4202 WLEN(len); /* Don't BER-encode, this should be rare */
4204 WRITE(classname, len); /* Final \0 is omitted */
4208 * Now emit the <object> part.
4211 return SV_STORE(type)(aTHX_ cxt, sv);
4217 * We don't know how to store the item we reached, so return an error condition.
4218 * (it's probably a GLOB, some CODE reference, etc...)
4220 * If they defined the 'forgive_me' variable at the Perl level to some
4221 * true value, then don't croak, just warn, and store a placeholder string
4224 static int store_other(pTHX_ stcxt_t *cxt, SV *sv)
4229 TRACEME(("store_other"));
4232 * Fetch the value from perl only once per store() operation.
4236 cxt->forgive_me == 0 ||
4237 (cxt->forgive_me < 0 &&
4238 !(cxt->forgive_me = SvTRUE
4239 (get_sv("Storable::forgive_me", GV_ADD)) ? 1 : 0))
4241 CROAK(("Can't store %s items", sv_reftype(sv, FALSE)));
4243 warn("Can't store item %s(0x%" UVxf ")",
4244 sv_reftype(sv, FALSE), PTR2UV(sv));
4247 * Store placeholder string as a scalar instead...
4250 (void) sprintf(buf, "You lost %s(0x%" UVxf ")%c", sv_reftype(sv, FALSE),
4251 PTR2UV(sv), (char) 0);
4255 STORE_SCALAR(buf, len);
4256 TRACEME(("ok (dummy \"%s\", length = %" IVdf ")", buf, (IV) len));
4262 *** Store driving routines
4268 * WARNING: partially duplicates Perl's sv_reftype for speed.
4270 * Returns the type of the SV, identified by an integer. That integer
4271 * may then be used to index the dynamic routine dispatch table.
4273 static int sv_type(pTHX_ SV *sv)
4275 switch (SvTYPE(sv)) {
4277 #if PERL_VERSION <= 10
4282 * No need to check for ROK, that can't be set here since there
4283 * is no field capable of hodling the xrv_rv reference.
4287 #if PERL_VERSION <= 10
4295 * Starting from SVt_PV, it is possible to have the ROK flag
4296 * set, the pointer to the other SV being either stored in
4297 * the xrv_rv (in the case of a pure SVt_RV), or as the
4298 * xpv_pv field of an SVt_PV and its heirs.
4300 * However, those SV cannot be magical or they would be an
4301 * SVt_PVMG at least.
4303 return SvROK(sv) ? svis_REF : svis_SCALAR;
4305 #if PERL_VERSION <= 10
4306 if ((SvFLAGS(sv) & (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
4307 == (SVs_OBJECT|BFD_Svs_SMG_OR_RMG)
4308 && mg_find(sv, PERL_MAGIC_qr)) {
4312 case SVt_PVLV: /* Workaround for perl5.004_04 "LVALUE" bug */
4313 if ((SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG)) ==
4314 (SVs_GMG|SVs_SMG|SVs_RMG) &&
4316 return svis_TIED_ITEM;
4318 #if PERL_VERSION < 9
4321 if ((SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG)) ==
4322 (SVs_GMG|SVs_SMG|SVs_RMG) &&
4325 return SvROK(sv) ? svis_REF : svis_SCALAR;
4327 if (SvRMAGICAL(sv) && (mg_find(sv, 'P')))
4331 if (SvRMAGICAL(sv) && (mg_find(sv, 'P')))
4336 #if PERL_VERSION > 8
4337 /* case SVt_INVLIST: */
4339 #if PERL_VERSION > 10
4353 * Recursively store objects pointed to by the sv to the specified file.
4355 * Layout is <content> or SX_OBJECT <tagnum> if we reach an already stored
4356 * object (one for which storage has started -- it may not be over if we have
4357 * a self-referenced structure). This data set forms a stored <object>.
4359 static int store(pTHX_ stcxt_t *cxt, SV *sv)
4364 #ifdef USE_PTR_TABLE
4365 struct ptr_tbl *pseen = cxt->pseen;
4367 HV *hseen = cxt->hseen;
4370 TRACEME(("store (0x%" UVxf ")", PTR2UV(sv)));
4373 * If object has already been stored, do not duplicate data.
4374 * Simply emit the SX_OBJECT marker followed by its tag data.
4375 * The tag is always written in network order.
4377 * NOTA BENE, for 64-bit machines: the "*svh" below does not yield a
4378 * real pointer, rather a tag number (watch the insertion code below).
4379 * That means it probably safe to assume it is well under the 32-bit
4380 * limit, and makes the truncation safe.
4381 * -- RAM, 14/09/1999
4384 #ifdef USE_PTR_TABLE
4385 svh = (SV **)ptr_table_fetch(pseen, sv);
4387 svh = hv_fetch(hseen, (char *) &sv, sizeof(sv), FALSE);
4391 if (sv == &PL_sv_undef) {
4392 /* We have seen PL_sv_undef before, but fake it as
4395 Not the simplest solution to making restricted
4396 hashes work on 5.8.0, but it does mean that
4397 repeated references to the one true undef will
4398 take up less space in the output file.
4400 /* Need to jump past the next hv_store, because on the
4401 second store of undef the old hash value will be
4402 SvREFCNT_dec()ed, and as Storable cheats horribly
4403 by storing non-SVs in the hash a SEGV will ensure.
4404 Need to increase the tag number so that the
4405 receiver has no idea what games we're up to. This
4406 special casing doesn't affect hooks that store
4407 undef, as the hook routine does its own lookup into
4408 hseen. Also this means that any references back
4409 to PL_sv_undef (from the pathological case of hooks
4410 storing references to it) will find the seen hash
4411 entry for the first time, as if we didn't have this
4412 hackery here. (That hseen lookup works even on 5.8.0
4413 because it's a key of &PL_sv_undef and a value
4414 which is a tag number, not a value which is
4418 goto undef_special_case;
4421 #ifdef USE_PTR_TABLE
4422 tagval = PTR2TAG(((char *)svh)-1);
4424 tagval = PTR2TAG(*svh);
4428 /* older versions of Storable streat the tag as a signed value
4429 used in an array lookup, corrupting the data structure.
4430 Ensure only a newer Storable will be able to parse this tag id
4431 if it's over the 2G mark.
4433 if (tagval > I32_MAX) {
4435 TRACEME(("object 0x%" UVxf " seen as #%" UVuf, PTR2UV(sv),
4438 PUTMARK(SX_LOBJECT);
4448 ltagval = htonl((I32)tagval);
4450 TRACEME(("object 0x%" UVxf " seen as #%d", PTR2UV(sv),
4460 * Allocate a new tag and associate it with the address of the sv being
4461 * stored, before recursing...
4463 * In order to avoid creating new SvIVs to hold the tagnum we just
4464 * cast the tagnum to an SV pointer and store that in the hash. This
4465 * means that we must clean up the hash manually afterwards, but gives
4466 * us a 15% throughput increase.
4471 #ifdef USE_PTR_TABLE
4472 ptr_table_store(pseen, sv, INT2PTR(SV*, 1 + cxt->tagnum));
4474 if (!hv_store(hseen,
4475 (char *) &sv, sizeof(sv), INT2PTR(SV*, cxt->tagnum), 0))
4480 * Store 'sv' and everything beneath it, using appropriate routine.
4481 * Abort immediately if we get a non-zero status back.
4484 type = sv_type(aTHX_ sv);
4487 TRACEME(("storing 0x%" UVxf " tag #%d, type %d...",
4488 PTR2UV(sv), (int)cxt->tagnum, (int)type));
4491 HV *pkg = SvSTASH(sv);
4492 ret = store_blessed(aTHX_ cxt, sv, type, pkg);
4494 ret = SV_STORE(type)(aTHX_ cxt, sv);
4496 TRACEME(("%s (stored 0x%" UVxf ", refcnt=%d, %s)",
4497 ret ? "FAILED" : "ok", PTR2UV(sv),
4498 (int)SvREFCNT(sv), sv_reftype(sv, FALSE)));
4506 * Write magic number and system information into the file.
4507 * Layout is <magic> <network> [<len> <byteorder> <sizeof int> <sizeof long>
4508 * <sizeof ptr>] where <len> is the length of the byteorder hexa string.
4509 * All size and lengths are written as single characters here.
4511 * Note that no byte ordering info is emitted when <network> is true, since
4512 * integers will be emitted in network order in that case.
4514 static int magic_write(pTHX_ stcxt_t *cxt)
4517 * Starting with 0.6, the "use_network_order" byte flag is also used to
4518 * indicate the version number of the binary image, encoded in the upper
4519 * bits. The bit 0 is always used to indicate network order.
4522 * Starting with 0.7, a full byte is dedicated to the minor version of
4523 * the binary format, which is incremented only when new markers are
4524 * introduced, for instance, but when backward compatibility is preserved.
4527 /* Make these at compile time. The WRITE() macro is sufficiently complex
4528 that it saves about 200 bytes doing it this way and only using it
4530 static const unsigned char network_file_header[] = {
4532 (STORABLE_BIN_MAJOR << 1) | 1,
4533 STORABLE_BIN_WRITE_MINOR
4535 static const unsigned char file_header[] = {
4537 (STORABLE_BIN_MAJOR << 1) | 0,
4538 STORABLE_BIN_WRITE_MINOR,
4539 /* sizeof the array includes the 0 byte at the end: */
4540 (char) sizeof (byteorderstr) - 1,
4542 (unsigned char) sizeof(int),
4543 (unsigned char) sizeof(long),
4544 (unsigned char) sizeof(char *),
4545 (unsigned char) sizeof(NV)
4547 #ifdef USE_56_INTERWORK_KLUDGE
4548 static const unsigned char file_header_56[] = {
4550 (STORABLE_BIN_MAJOR << 1) | 0,
4551 STORABLE_BIN_WRITE_MINOR,
4552 /* sizeof the array includes the 0 byte at the end: */
4553 (char) sizeof (byteorderstr_56) - 1,
4555 (unsigned char) sizeof(int),
4556 (unsigned char) sizeof(long),
4557 (unsigned char) sizeof(char *),
4558 (unsigned char) sizeof(NV)
4561 const unsigned char *header;
4564 TRACEME(("magic_write on fd=%d", cxt->fio ? PerlIO_fileno(cxt->fio) : -1));
4566 if (cxt->netorder) {
4567 header = network_file_header;
4568 length = sizeof (network_file_header);
4570 #ifdef USE_56_INTERWORK_KLUDGE
4571 if (SvTRUE(get_sv("Storable::interwork_56_64bit", GV_ADD))) {
4572 header = file_header_56;
4573 length = sizeof (file_header_56);
4577 header = file_header;
4578 length = sizeof (file_header);
4583 /* sizeof the array includes the 0 byte at the end. */
4584 header += sizeof (magicstr) - 1;
4585 length -= sizeof (magicstr) - 1;
4588 WRITE( (unsigned char*) header, length);
4590 if (!cxt->netorder) {
4591 TRACEME(("ok (magic_write byteorder = 0x%lx [%d], I%d L%d P%d D%d)",
4592 (unsigned long) BYTEORDER, (int) sizeof (byteorderstr) - 1,
4593 (int) sizeof(int), (int) sizeof(long),
4594 (int) sizeof(char *), (int) sizeof(NV)));
4602 * Common code for store operations.
4604 * When memory store is requested (f = NULL) and a non null SV* is given in
4605 * 'res', it is filled with a new SV created out of the memory buffer.
4607 * It is required to provide a non-null 'res' when the operation type is not
4608 * dclone() and store() is performed to memory.
4610 static int do_store(pTHX_
4620 ASSERT(!(f == 0 && !(optype & ST_CLONE)) || res,
4621 ("must supply result SV pointer for real recursion to memory"));
4623 TRACEMED(("do_store (optype=%d, netorder=%d)",
4624 optype, network_order));
4629 * Workaround for CROAK leak: if they enter with a "dirty" context,
4630 * free up memory for them now.
4635 clean_context(aTHX_ cxt);
4638 * Now that STORABLE_xxx hooks exist, it is possible that they try to
4639 * re-enter store() via the hooks. We need to stack contexts.
4643 cxt = allocate_context(aTHX_ cxt);
4649 ASSERT(cxt->entry == 1, ("starting new recursion"));
4650 ASSERT(!cxt->s_dirty, ("clean context"));
4653 * Ensure sv is actually a reference. From perl, we called something
4655 * pstore(aTHX_ FILE, \@array);
4656 * so we must get the scalar value behind that reference.
4660 CROAK(("Not a reference"));
4661 sv = SvRV(sv); /* So follow it to know what to store */
4664 * If we're going to store to memory, reset the buffer.
4671 * Prepare context and emit headers.
4674 init_store_context(aTHX_ cxt, f, optype, network_order);
4676 if (-1 == magic_write(aTHX_ cxt)) /* Emit magic and ILP info */
4677 return 0; /* Error */
4680 * Recursively store object...
4683 ASSERT(is_storing(aTHX), ("within store operation"));
4685 status = store(aTHX_ cxt, sv); /* Just do it! */
4688 * If they asked for a memory store and they provided an SV pointer,
4689 * make an SV string out of the buffer and fill their pointer.
4691 * When asking for ST_REAL, it's MANDATORY for the caller to provide
4692 * an SV, since context cleanup might free the buffer if we did recurse.
4693 * (unless caller is dclone(), which is aware of that).
4696 if (!cxt->fio && res)
4697 *res = mbuf2sv(aTHX);
4699 TRACEME(("do_store returns %d", status));
4704 * The "root" context is never freed, since it is meant to be always
4705 * handy for the common case where no recursion occurs at all (i.e.
4706 * we enter store() outside of any Storable code and leave it, period).
4707 * We know it's the "root" context because there's nothing stacked
4712 * When deep cloning, we don't free the context: doing so would force
4713 * us to copy the data in the memory buffer. Sicne we know we're
4714 * about to enter do_retrieve...
4717 clean_store_context(aTHX_ cxt);
4718 if (cxt->prev && !(cxt->optype & ST_CLONE))
4719 free_context(aTHX_ cxt);
4731 * Build a new SV out of the content of the internal memory buffer.
4733 static SV *mbuf2sv(pTHX)
4738 return newSVpv(mbase, MBUF_SIZE());
4742 *** Specific retrieve callbacks.
4748 * Return an error via croak, since it is not possible that we get here
4749 * under normal conditions, when facing a file produced via pstore().
4751 static SV *retrieve_other(pTHX_ stcxt_t *cxt, const char *cname)
4753 PERL_UNUSED_ARG(cname);
4755 cxt->ver_major != STORABLE_BIN_MAJOR &&
4756 cxt->ver_minor != STORABLE_BIN_MINOR
4758 CROAK(("Corrupted storable %s (binary v%d.%d), current is v%d.%d",
4759 cxt->fio ? "file" : "string",
4760 cxt->ver_major, cxt->ver_minor,
4761 STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR));
4763 CROAK(("Corrupted storable %s (binary v%d.%d)",
4764 cxt->fio ? "file" : "string",
4765 cxt->ver_major, cxt->ver_minor));
4768 return (SV *) 0; /* Just in case */
4772 * retrieve_idx_blessed
4774 * Layout is SX_IX_BLESS <index> <object> with SX_IX_BLESS already read.
4775 * <index> can be coded on either 1 or 5 bytes.
4777 static SV *retrieve_idx_blessed(pTHX_ stcxt_t *cxt, const char *cname)
4780 const char *classname;
4784 PERL_UNUSED_ARG(cname);
4785 TRACEME(("retrieve_idx_blessed (#%d)", (int)cxt->tagnum));
4786 ASSERT(!cname, ("no bless-into class given here, got %s", cname));
4788 GETMARK(idx); /* Index coded on a single char? */
4793 * Fetch classname in 'aclass'
4796 sva = av_fetch(cxt->aclass, idx, FALSE);
4798 CROAK(("Class name #%" IVdf " should have been seen already",
4801 classname = SvPVX(*sva); /* We know it's a PV, by construction */
4803 TRACEME(("class ID %d => %s", (int)idx, classname));
4806 * Retrieve object and bless it.
4809 sv = retrieve(aTHX_ cxt, classname); /* First SV which is SEEN
4818 * Layout is SX_BLESS <len> <classname> <object> with SX_BLESS already read.
4819 * <len> can be coded on either 1 or 5 bytes.
4821 static SV *retrieve_blessed(pTHX_ stcxt_t *cxt, const char *cname)
4825 char buf[LG_BLESS + 1]; /* Avoid malloc() if possible */
4826 char *classname = buf;
4827 char *malloced_classname = NULL;
4829 PERL_UNUSED_ARG(cname);
4830 TRACEME(("retrieve_blessed (#%d)", (int)cxt->tagnum));
4831 ASSERT(!cname, ("no bless-into class given here, got %s", cname));
4834 * Decode class name length and read that name.
4836 * Short classnames have two advantages: their length is stored on one
4837 * single byte, and the string can be read on the stack.
4840 GETMARK(len); /* Length coded on a single char? */
4843 TRACEME(("** allocating %ld bytes for class name", (long)len+1));
4845 CROAK(("Corrupted classname length %lu", (long)len));
4846 PL_nomemok = TRUE; /* handle error by ourselves */
4847 New(10003, classname, len+1, char);
4850 CROAK(("Out of memory with len %ld", (long)len));
4852 malloced_classname = classname;
4854 SAFEPVREAD(classname, (I32)len, malloced_classname);
4855 classname[len] = '\0'; /* Mark string end */
4858 * It's a new classname, otherwise it would have been an SX_IX_BLESS.
4861 TRACEME(("new class name \"%s\" will bear ID = %d", classname,
4862 (int)cxt->classnum));
4864 if (!av_store(cxt->aclass, cxt->classnum++, newSVpvn(classname, len))) {
4865 Safefree(malloced_classname);
4870 * Retrieve object and bless it.
4873 sv = retrieve(aTHX_ cxt, classname); /* First SV which is SEEN will be blessed */
4874 if (malloced_classname)
4875 Safefree(malloced_classname);
4883 * Layout: SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
4884 * with leading mark already read, as usual.
4886 * When recursion was involved during serialization of the object, there
4887 * is an unknown amount of serialized objects after the SX_HOOK mark. Until
4888 * we reach a <flags> marker with the recursion bit cleared.
4890 * If the first <flags> byte contains a type of SHT_EXTRA, then the real type
4891 * is held in the <extra> byte, and if the object is tied, the serialized
4892 * magic object comes at the very end:
4894 * SX_HOOK <flags> <extra> ... [<len3> <object-IDs>] <magic object>
4896 * This means the STORABLE_thaw hook will NOT get a tied variable during its
4897 * processing (since we won't have seen the magic object by the time the hook
4898 * is called). See comments below for why it was done that way.
4900 static SV *retrieve_hook_common(pTHX_ stcxt_t *cxt, const char *cname, int large)
4903 char buf[LG_BLESS + 1]; /* Avoid malloc() if possible */
4904 char *classname = buf;
4916 int clone = cxt->optype & ST_CLONE;
4918 unsigned int extra_type = 0;
4920 int has_large_oids = 0;
4923 PERL_UNUSED_ARG(cname);
4924 TRACEME(("retrieve_hook (#%d)", (int)cxt->tagnum));
4925 ASSERT(!cname, ("no bless-into class given here, got %s", cname));
4929 PERL_UNUSED_ARG(large);
4933 * Read flags, which tell us about the type, and whether we need
4940 * Create the (empty) object, and mark it as seen.
4942 * This must be done now, because tags are incremented, and during
4943 * serialization, the object tag was affected before recursion could
4947 obj_type = flags & SHF_TYPE_MASK;
4953 sv = (SV *) newAV();
4956 sv = (SV *) newHV();
4960 * Read <extra> flag to know the type of the object.
4961 * Record associated magic type for later.
4963 GETMARK(extra_type);
4964 switch (extra_type) {
4970 sv = (SV *) newAV();
4974 sv = (SV *) newHV();
4978 return retrieve_other(aTHX_ cxt, 0);/* Let it croak */
4982 return retrieve_other(aTHX_ cxt, 0); /* Let it croak */
4984 SEEN0_NN(sv, 0); /* Don't bless yet */
4987 * Whilst flags tell us to recurse, do so.
4989 * We don't need to remember the addresses returned by retrieval, because
4990 * all the references will be obtained through indirection via the object
4991 * tags in the object-ID list.
4993 * We need to decrement the reference count for these objects
4994 * because, if the user doesn't save a reference to them in the hook,
4995 * they must be freed when this context is cleaned.
4998 while (flags & SHF_NEED_RECURSE) {
4999 TRACEME(("retrieve_hook recursing..."));
5000 rv = retrieve(aTHX_ cxt, 0);
5004 TRACEME(("retrieve_hook back with rv=0x%" UVxf,
5009 if (flags & SHF_IDX_CLASSNAME) {
5014 * Fetch index from 'aclass'
5017 if (flags & SHF_LARGE_CLASSLEN)
5022 sva = av_fetch(cxt->aclass, idx, FALSE);
5024 CROAK(("Class name #%" IVdf " should have been seen already",
5027 classname = SvPVX(*sva); /* We know it's a PV, by construction */
5028 TRACEME(("class ID %d => %s", (int)idx, classname));
5032 * Decode class name length and read that name.
5034 * NOTA BENE: even if the length is stored on one byte, we don't read
5035 * on the stack. Just like retrieve_blessed(), we limit the name to
5036 * LG_BLESS bytes. This is an arbitrary decision.
5038 char *malloced_classname = NULL;
5040 if (flags & SHF_LARGE_CLASSLEN)
5045 TRACEME(("** allocating %ld bytes for class name", (long)len+1));
5046 if (len > I32_MAX) /* security */
5047 CROAK(("Corrupted classname length %lu", (long)len));
5048 else if (len > LG_BLESS) { /* security: signed len */
5049 PL_nomemok = TRUE; /* handle error by ourselves */
5050 New(10003, classname, len+1, char);
5053 CROAK(("Out of memory with len %u", (unsigned)len+1));
5054 malloced_classname = classname;
5057 SAFEPVREAD(classname, (I32)len, malloced_classname);
5058 classname[len] = '\0'; /* Mark string end */
5061 * Record new classname.
5064 if (!av_store(cxt->aclass, cxt->classnum++,
5065 newSVpvn(classname, len))) {
5066 Safefree(malloced_classname);
5071 TRACEME(("class name: %s", classname));
5074 * Decode user-frozen string length and read it in an SV.
5076 * For efficiency reasons, we read data directly into the SV buffer.
5077 * To understand that code, read retrieve_scalar()
5086 if (flags & SHF_LARGE_STRLEN) {
5094 frozen = NEWSV(10002, len2 ? len2 : 1);
5096 SAFEREAD(SvPVX(frozen), len2, frozen);
5098 SvCUR_set(frozen, len2);
5099 *SvEND(frozen) = '\0';
5100 (void) SvPOK_only(frozen); /* Validates string pointer */
5101 if (cxt->s_tainted) /* Is input source tainted? */
5104 TRACEME(("frozen string: %d bytes", (int)len2));
5107 * Decode object-ID list length, if present.
5110 if (flags & SHF_HAS_LIST) {
5111 if (flags & SHF_LARGE_LISTLEN) {
5118 CROAK(("Large object ids in hook data not supported on 32-bit platforms"));
5127 av_extend(av, len3 + 1); /* Leave room for [0] */
5128 AvFILLp(av) = len3; /* About to be filled anyway */
5132 TRACEME(("has %d object IDs to link", (int)len3));
5135 * Read object-ID list into array.
5136 * Because we pre-extended it, we can cheat and fill it manually.
5138 * We read object tags and we can convert them into SV* on the fly
5139 * because we know all the references listed in there (as tags)
5140 * have been already serialized, hence we have a valid correspondence
5141 * between each of those tags and the recreated SV.
5145 SV **ary = AvARRAY(av);
5147 for (i = 1; i <= len3; i++) { /* We leave [0] alone */
5153 if (has_large_oids) {
5166 svh = av_fetch(cxt->aseen, tag, FALSE);
5168 if (tag == cxt->where_is_undef) {
5169 /* av_fetch uses PL_sv_undef internally, hence this
5170 somewhat gruesome hack. */
5174 CROAK(("Object #%" IVdf
5175 " should have been retrieved already",
5180 ary[i] = SvREFCNT_inc(xsv);
5185 * Look up the STORABLE_attach hook
5186 * If blessing is disabled, just return what we've got.
5188 if (!(cxt->flags & FLAG_BLESS_OK)) {
5189 TRACEME(("skipping bless because flags is %d", cxt->flags));
5194 * Bless the object and look up the STORABLE_thaw hook.
5196 stash = gv_stashpv(classname, GV_ADD);
5198 /* Handle attach case; again can't use pkg_can because it only
5199 * caches one method */
5200 attach = gv_fetchmethod_autoload(stash, "STORABLE_attach", FALSE);
5201 if (attach && isGV(attach)) {
5203 SV* attach_hook = newRV_inc((SV*) GvCV(attach));
5206 CROAK(("STORABLE_attach called with unexpected references"));
5210 AvARRAY(av)[0] = SvREFCNT_inc(frozen);
5211 rv = newSVpv(classname, 0);
5212 attached = scalar_call(aTHX_ rv, attach_hook, clone, av, G_SCALAR);
5213 /* Free memory after a call */
5215 SvREFCNT_dec(frozen);
5218 SvREFCNT_dec(attach_hook);
5221 sv_derived_from(attached, classname)
5224 /* refcnt of unneeded sv is 2 at this point
5225 (one from newHV, second from SEEN call) */
5228 /* we need to free RV but preserve value that RV point to */
5229 sv = SvRV(attached);
5231 SvRV_set(attached, NULL);
5232 SvREFCNT_dec(attached);
5233 if (!(flags & SHF_IDX_CLASSNAME) && classname != buf)
5234 Safefree(classname);
5237 CROAK(("STORABLE_attach did not return a %s object", classname));
5241 * Bless the object and look up the STORABLE_thaw hook.
5246 hook = pkg_can(aTHX_ cxt->hook, stash, "STORABLE_thaw");
5249 * Hook not found. Maybe they did not require the module where this
5250 * hook is defined yet?
5252 * If the load below succeeds, we'll be able to find the hook.
5253 * Still, it only works reliably when each class is defined in a
5257 TRACEME(("No STORABLE_thaw defined for objects of class %s", classname));
5258 TRACEME(("Going to load module '%s'", classname));
5259 load_module(PERL_LOADMOD_NOIMPORT, newSVpv(classname, 0), Nullsv);
5262 * We cache results of pkg_can, so we need to uncache before attempting
5266 pkg_uncache(aTHX_ cxt->hook, SvSTASH(sv), "STORABLE_thaw");
5267 hook = pkg_can(aTHX_ cxt->hook, SvSTASH(sv), "STORABLE_thaw");
5270 CROAK(("No STORABLE_thaw defined for objects of class %s "
5271 "(even after a \"require %s;\")", classname, classname));
5275 * If we don't have an 'av' yet, prepare one.
5276 * Then insert the frozen string as item [0].
5284 AvARRAY(av)[0] = SvREFCNT_inc(frozen);
5289 * $object->STORABLE_thaw($cloning, $frozen, @refs);
5291 * where $object is our blessed (empty) object, $cloning is a boolean
5292 * telling whether we're running a deep clone, $frozen is the frozen
5293 * string the user gave us in his serializing hook, and @refs, which may
5294 * be empty, is the list of extra references he returned along for us
5297 * In effect, the hook is an alternate creation routine for the class,
5298 * the object itself being already created by the runtime.
5301 TRACEME(("calling STORABLE_thaw on %s at 0x%" UVxf " (%" IVdf " args)",
5302 classname, PTR2UV(sv), (IV) AvFILLp(av) + 1));
5305 (void) scalar_call(aTHX_ rv, hook, clone, av, G_SCALAR|G_DISCARD);
5312 SvREFCNT_dec(frozen);
5315 if (!(flags & SHF_IDX_CLASSNAME) && classname != buf)
5316 Safefree(classname);
5319 * If we had an <extra> type, then the object was not as simple, and
5320 * we need to restore extra magic now.
5326 TRACEME(("retrieving magic object for 0x%" UVxf "...", PTR2UV(sv)));
5328 rv = retrieve(aTHX_ cxt, 0); /* Retrieve <magic object> */
5330 TRACEME(("restoring the magic object 0x%" UVxf " part of 0x%" UVxf,
5331 PTR2UV(rv), PTR2UV(sv)));
5333 switch (extra_type) {
5335 sv_upgrade(sv, SVt_PVMG);
5338 sv_upgrade(sv, SVt_PVAV);
5339 AvREAL_off((AV *)sv);
5342 sv_upgrade(sv, SVt_PVHV);
5345 CROAK(("Forgot to deal with extra type %d", extra_type));
5350 * Adding the magic only now, well after the STORABLE_thaw hook was called
5351 * means the hook cannot know it deals with an object whose variable is
5352 * tied. But this is happening when retrieving $o in the following case:
5356 * my $o = bless \%h, 'BAR';
5358 * The 'BAR' class is NOT the one where %h is tied into. Therefore, as
5359 * far as the 'BAR' class is concerned, the fact that %h is not a REAL
5360 * hash but a tied one should not matter at all, and remain transparent.
5361 * This means the magic must be restored by Storable AFTER the hook is
5364 * That looks very reasonable to me, but then I've come up with this
5365 * after a bug report from David Nesting, who was trying to store such
5366 * an object and caused Storable to fail. And unfortunately, it was
5367 * also the easiest way to retrofit support for blessed ref to tied objects
5368 * into the existing design. -- RAM, 17/02/2001
5371 sv_magic(sv, rv, mtype, (char *)NULL, 0);
5372 SvREFCNT_dec(rv); /* Undo refcnt inc from sv_magic() */
5377 static SV *retrieve_hook(pTHX_ stcxt_t *cxt, const char *cname) {
5378 return retrieve_hook_common(aTHX_ cxt, cname, FALSE);
5384 * Retrieve reference to some other scalar.
5385 * Layout is SX_REF <object>, with SX_REF already read.
5387 static SV *retrieve_ref(pTHX_ stcxt_t *cxt, const char *cname)
5393 TRACEME(("retrieve_ref (#%d)", (int)cxt->tagnum));
5396 * We need to create the SV that holds the reference to the yet-to-retrieve
5397 * object now, so that we may record the address in the seen table.
5398 * Otherwise, if the object to retrieve references us, we won't be able
5399 * to resolve the SX_OBJECT we'll see at that point! Hence we cannot
5400 * do the retrieve first and use rv = newRV(sv) since it will be too late
5401 * for SEEN() recording.
5404 rv = NEWSV(10002, 0);
5406 stash = gv_stashpv(cname, GV_ADD);
5409 SEEN_NN(rv, stash, 0); /* Will return if rv is null */
5410 sv = retrieve(aTHX_ cxt, 0);/* Retrieve <object> */
5412 return (SV *) 0; /* Failed */
5415 * WARNING: breaks RV encapsulation.
5417 * Now for the tricky part. We have to upgrade our existing SV, so that
5418 * it is now an RV on sv... Again, we cheat by duplicating the code
5419 * held in newSVrv(), since we already got our SV from retrieve().
5423 * SvRV(rv) = SvREFCNT_inc(sv);
5425 * here because the reference count we got from retrieve() above is
5426 * already correct: if the object was retrieved from the file, then
5427 * its reference count is one. Otherwise, if it was retrieved via
5428 * an SX_OBJECT indication, a ref count increment was done.
5432 /* No need to do anything, as rv will already be PVMG. */
5433 assert (SvTYPE(rv) == SVt_RV || SvTYPE(rv) >= SVt_PV);
5435 sv_upgrade(rv, SVt_RV);
5438 SvRV_set(rv, sv); /* $rv = \$sv */
5440 /*if (cxt->entry && ++cxt->ref_cnt > MAX_REF_CNT) {
5441 CROAK(("Max. recursion depth with nested refs exceeded"));
5444 TRACEME(("ok (retrieve_ref at 0x%" UVxf ")", PTR2UV(rv)));
5452 * Retrieve weak reference to some other scalar.
5453 * Layout is SX_WEAKREF <object>, with SX_WEAKREF already read.
5455 static SV *retrieve_weakref(pTHX_ stcxt_t *cxt, const char *cname)
5459 TRACEME(("retrieve_weakref (#%d)", (int)cxt->tagnum));
5461 sv = retrieve_ref(aTHX_ cxt, cname);
5473 * retrieve_overloaded
5475 * Retrieve reference to some other scalar with overloading.
5476 * Layout is SX_OVERLOAD <object>, with SX_OVERLOAD already read.
5478 static SV *retrieve_overloaded(pTHX_ stcxt_t *cxt, const char *cname)
5484 TRACEME(("retrieve_overloaded (#%d)", (int)cxt->tagnum));
5487 * Same code as retrieve_ref(), duplicated to avoid extra call.
5490 rv = NEWSV(10002, 0);
5491 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
5492 SEEN_NN(rv, stash, 0); /* Will return if rv is null */
5493 cxt->in_retrieve_overloaded = 1; /* so sv_bless doesn't call S_reset_amagic */
5494 sv = retrieve(aTHX_ cxt, 0); /* Retrieve <object> */
5495 cxt->in_retrieve_overloaded = 0;
5497 return (SV *) 0; /* Failed */
5500 * WARNING: breaks RV encapsulation.
5503 SvUPGRADE(rv, SVt_RV);
5504 SvRV_set(rv, sv); /* $rv = \$sv */
5508 * Restore overloading magic.
5511 stash = SvTYPE(sv) ? (HV *) SvSTASH (sv) : 0;
5513 CROAK(("Cannot restore overloading on %s(0x%" UVxf
5514 ") (package <unknown>)",
5515 sv_reftype(sv, FALSE),
5518 if (!Gv_AMG(stash)) {
5519 const char *package = HvNAME_get(stash);
5520 TRACEME(("No overloading defined for package %s", package));
5521 TRACEME(("Going to load module '%s'", package));
5522 load_module(PERL_LOADMOD_NOIMPORT, newSVpv(package, 0), Nullsv);
5523 if (!Gv_AMG(stash)) {
5524 CROAK(("Cannot restore overloading on %s(0x%" UVxf
5525 ") (package %s) (even after a \"require %s;\")",
5526 sv_reftype(sv, FALSE),
5534 TRACEME(("ok (retrieve_overloaded at 0x%" UVxf ")", PTR2UV(rv)));
5540 * retrieve_weakoverloaded
5542 * Retrieve weak overloaded reference to some other scalar.
5543 * Layout is SX_WEAKOVERLOADED <object>, with SX_WEAKOVERLOADED already read.
5545 static SV *retrieve_weakoverloaded(pTHX_ stcxt_t *cxt, const char *cname)
5549 TRACEME(("retrieve_weakoverloaded (#%d)", (int)cxt->tagnum));
5551 sv = retrieve_overloaded(aTHX_ cxt, cname);
5563 * retrieve_tied_array
5565 * Retrieve tied array
5566 * Layout is SX_TIED_ARRAY <object>, with SX_TIED_ARRAY already read.
5568 static SV *retrieve_tied_array(pTHX_ stcxt_t *cxt, const char *cname)
5574 TRACEME(("retrieve_tied_array (#%d)", (int)cxt->tagnum));
5576 if (!(cxt->flags & FLAG_TIE_OK)) {
5577 CROAK(("Tying is disabled."));
5580 tv = NEWSV(10002, 0);
5581 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
5582 SEEN_NN(tv, stash, 0); /* Will return if tv is null */
5583 sv = retrieve(aTHX_ cxt, 0); /* Retrieve <object> */
5585 return (SV *) 0; /* Failed */
5587 sv_upgrade(tv, SVt_PVAV);
5588 sv_magic(tv, sv, 'P', (char *)NULL, 0);
5589 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
5591 TRACEME(("ok (retrieve_tied_array at 0x%" UVxf ")", PTR2UV(tv)));
5597 * retrieve_tied_hash
5599 * Retrieve tied hash
5600 * Layout is SX_TIED_HASH <object>, with SX_TIED_HASH already read.
5602 static SV *retrieve_tied_hash(pTHX_ stcxt_t *cxt, const char *cname)
5608 TRACEME(("retrieve_tied_hash (#%d)", (int)cxt->tagnum));
5610 if (!(cxt->flags & FLAG_TIE_OK)) {
5611 CROAK(("Tying is disabled."));
5614 tv = NEWSV(10002, 0);
5615 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
5616 SEEN_NN(tv, stash, 0); /* Will return if tv is null */
5617 sv = retrieve(aTHX_ cxt, 0); /* Retrieve <object> */
5619 return (SV *) 0; /* Failed */
5621 sv_upgrade(tv, SVt_PVHV);
5622 sv_magic(tv, sv, 'P', (char *)NULL, 0);
5623 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
5625 TRACEME(("ok (retrieve_tied_hash at 0x%" UVxf ")", PTR2UV(tv)));
5631 * retrieve_tied_scalar
5633 * Retrieve tied scalar
5634 * Layout is SX_TIED_SCALAR <object>, with SX_TIED_SCALAR already read.
5636 static SV *retrieve_tied_scalar(pTHX_ stcxt_t *cxt, const char *cname)
5639 SV *sv, *obj = NULL;
5642 TRACEME(("retrieve_tied_scalar (#%d)", (int)cxt->tagnum));
5644 if (!(cxt->flags & FLAG_TIE_OK)) {
5645 CROAK(("Tying is disabled."));
5648 tv = NEWSV(10002, 0);
5649 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
5650 SEEN_NN(tv, stash, 0); /* Will return if rv is null */
5651 sv = retrieve(aTHX_ cxt, 0); /* Retrieve <object> */
5653 return (SV *) 0; /* Failed */
5655 else if (SvTYPE(sv) != SVt_NULL) {
5659 sv_upgrade(tv, SVt_PVMG);
5660 sv_magic(tv, obj, 'q', (char *)NULL, 0);
5663 /* Undo refcnt inc from sv_magic() */
5667 TRACEME(("ok (retrieve_tied_scalar at 0x%" UVxf ")", PTR2UV(tv)));
5675 * Retrieve reference to value in a tied hash.
5676 * Layout is SX_TIED_KEY <object> <key>, with SX_TIED_KEY already read.
5678 static SV *retrieve_tied_key(pTHX_ stcxt_t *cxt, const char *cname)
5685 TRACEME(("retrieve_tied_key (#%d)", (int)cxt->tagnum));
5687 if (!(cxt->flags & FLAG_TIE_OK)) {
5688 CROAK(("Tying is disabled."));
5691 tv = NEWSV(10002, 0);
5692 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
5693 SEEN_NN(tv, stash, 0); /* Will return if tv is null */
5694 sv = retrieve(aTHX_ cxt, 0); /* Retrieve <object> */
5696 return (SV *) 0; /* Failed */
5698 key = retrieve(aTHX_ cxt, 0); /* Retrieve <key> */
5700 return (SV *) 0; /* Failed */
5702 sv_upgrade(tv, SVt_PVMG);
5703 sv_magic(tv, sv, 'p', (char *)key, HEf_SVKEY);
5704 SvREFCNT_dec(key); /* Undo refcnt inc from sv_magic() */
5705 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
5713 * Retrieve reference to value in a tied array.
5714 * Layout is SX_TIED_IDX <object> <idx>, with SX_TIED_IDX already read.
5716 static SV *retrieve_tied_idx(pTHX_ stcxt_t *cxt, const char *cname)
5723 TRACEME(("retrieve_tied_idx (#%d)", (int)cxt->tagnum));
5725 if (!(cxt->flags & FLAG_TIE_OK)) {
5726 CROAK(("Tying is disabled."));
5729 tv = NEWSV(10002, 0);
5730 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
5731 SEEN_NN(tv, stash, 0); /* Will return if tv is null */
5732 sv = retrieve(aTHX_ cxt, 0); /* Retrieve <object> */
5734 return (SV *) 0; /* Failed */
5736 RLEN(idx); /* Retrieve <idx> */
5738 sv_upgrade(tv, SVt_PVMG);
5739 sv_magic(tv, sv, 'p', (char *)NULL, idx);
5740 SvREFCNT_dec(sv); /* Undo refcnt inc from sv_magic() */
5748 * Helper to read a string
5750 static SV *get_lstring(pTHX_ stcxt_t *cxt, UV len, int isutf8, const char *cname)
5755 TRACEME(("get_lstring (#%d), len = %" UVuf, (int)cxt->tagnum, len));
5758 * Allocate an empty scalar of the suitable length.
5761 sv = NEWSV(10002, len);
5762 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
5763 SEEN_NN(sv, stash, 0); /* Associate this new scalar with tag "tagnum" */
5771 * WARNING: duplicates parts of sv_setpv and breaks SV data encapsulation.
5773 * Now, for efficiency reasons, read data directly inside the SV buffer,
5774 * and perform the SV final settings directly by duplicating the final
5775 * work done by sv_setpv. Since we're going to allocate lots of scalars
5776 * this way, it's worth the hassle and risk.
5779 SAFEREAD(SvPVX(sv), len, sv);
5780 SvCUR_set(sv, len); /* Record C string length */
5781 *SvEND(sv) = '\0'; /* Ensure it's null terminated anyway */
5782 (void) SvPOK_only(sv); /* Validate string pointer */
5783 if (cxt->s_tainted) /* Is input source tainted? */
5784 SvTAINT(sv); /* External data cannot be trusted */
5786 /* Check for CVE-215-1592 */
5787 if (cname && len == 13 && strEQc(cname, "CGITempFile")
5788 && strEQc(SvPVX(sv), "mt-config.cgi")) {
5789 #if defined(USE_CPERL) && defined(WARN_SECURITY)
5790 Perl_warn_security(aTHX_
5791 "Movable-Type CVE-2015-1592 Storable metasploit attack");
5794 "SECURITY: Movable-Type CVE-2015-1592 Storable metasploit attack");
5799 TRACEME(("large utf8 string len %" UVuf " '%s'", len,
5800 len >= 2048 ? "<string too long>" : SvPVX(sv)));
5801 #ifdef HAS_UTF8_SCALARS
5804 if (cxt->use_bytes < 0)
5806 = (SvTRUE(get_sv("Storable::drop_utf8", GV_ADD))
5808 if (cxt->use_bytes == 0)
5812 TRACEME(("large string len %" UVuf " '%s'", len,
5813 len >= 2048 ? "<string too long>" : SvPVX(sv)));
5815 TRACEME(("ok (get_lstring at 0x%" UVxf ")", PTR2UV(sv)));
5823 * Retrieve defined long (string) scalar.
5825 * Layout is SX_LSCALAR <length> <data>, with SX_LSCALAR already read.
5826 * The scalar is "long" in that <length> is larger than LG_SCALAR so it
5827 * was not stored on a single byte, but in 4 bytes. For strings longer than
5828 * 4 byte (>2GB) see retrieve_lobject.
5830 static SV *retrieve_lscalar(pTHX_ stcxt_t *cxt, const char *cname)
5834 return get_lstring(aTHX_ cxt, len, 0, cname);
5840 * Retrieve defined short (string) scalar.
5842 * Layout is SX_SCALAR <length> <data>, with SX_SCALAR already read.
5843 * The scalar is "short" so <length> is single byte. If it is 0, there
5844 * is no <data> section.
5846 static SV *retrieve_scalar(pTHX_ stcxt_t *cxt, const char *cname)
5853 TRACEME(("retrieve_scalar (#%d), len = %d", (int)cxt->tagnum, len));
5854 return get_lstring(aTHX_ cxt, (UV)len, 0, cname);
5860 * Like retrieve_scalar(), but tag result as utf8.
5861 * If we're retrieving UTF8 data in a non-UTF8 perl, croaks.
5863 static SV *retrieve_utf8str(pTHX_ stcxt_t *cxt, const char *cname)
5868 TRACEME(("retrieve_utf8str"));
5870 return get_lstring(aTHX_ cxt, (UV)len, 1, cname);
5876 * Like retrieve_lscalar(), but tag result as utf8.
5877 * If we're retrieving UTF8 data in a non-UTF8 perl, croaks.
5879 static SV *retrieve_lutf8str(pTHX_ stcxt_t *cxt, const char *cname)
5883 TRACEME(("retrieve_lutf8str"));
5886 return get_lstring(aTHX_ cxt, (UV)len, 1, cname);
5892 * Retrieve a vstring, and then retrieve the stringy scalar following it,
5893 * attaching the vstring to the scalar via magic.
5894 * If we're retrieving a vstring in a perl without vstring magic, croaks.
5896 * The vstring layout mirrors an SX_SCALAR string:
5897 * SX_VSTRING <length> <data> with SX_VSTRING already read.
5899 static SV *retrieve_vstring(pTHX_ stcxt_t *cxt, const char *cname)
5907 TRACEME(("retrieve_vstring (#%d), len = %d", (int)cxt->tagnum, len));
5910 sv = retrieve(aTHX_ cxt, cname);
5912 return (SV *) 0; /* Failed */
5913 sv_magic(sv,NULL,PERL_MAGIC_vstring,s,len);
5914 /* 5.10.0 and earlier seem to need this */
5917 TRACEME(("ok (retrieve_vstring at 0x%" UVxf ")", PTR2UV(sv)));
5928 * Like retrieve_vstring, but for longer vstrings.
5930 static SV *retrieve_lvstring(pTHX_ stcxt_t *cxt, const char *cname)
5938 TRACEME(("retrieve_lvstring (#%d), len = %" IVdf,
5939 (int)cxt->tagnum, (IV)len));
5941 New(10003, s, len+1, char);
5942 SAFEPVREAD(s, len, s);
5944 sv = retrieve(aTHX_ cxt, cname);
5947 return (SV *) 0; /* Failed */
5949 sv_magic(sv,NULL,PERL_MAGIC_vstring,s,len);
5950 /* 5.10.0 and earlier seem to need this */
5955 TRACEME(("ok (retrieve_lvstring at 0x%" UVxf ")", PTR2UV(sv)));
5966 * Retrieve defined integer.
5967 * Layout is SX_INTEGER <data>, whith SX_INTEGER already read.
5969 static SV *retrieve_integer(pTHX_ stcxt_t *cxt, const char *cname)
5975 TRACEME(("retrieve_integer (#%d)", (int)cxt->tagnum));
5977 READ(&iv, sizeof(iv));
5979 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
5980 SEEN_NN(sv, stash, 0); /* Associate this new scalar with tag "tagnum" */
5982 TRACEME(("integer %" IVdf, iv));
5983 TRACEME(("ok (retrieve_integer at 0x%" UVxf ")", PTR2UV(sv)));
5991 * Retrieve overlong scalar, array or hash.
5992 * Layout is SX_LOBJECT type U64_len ...
5994 static SV *retrieve_lobject(pTHX_ stcxt_t *cxt, const char *cname)
6003 TRACEME(("retrieve_lobject (#%d)", (int)cxt->tagnum));
6006 TRACEME(("object type %d", type));
6009 if (type == SX_FLAG_HASH) {
6010 /* we write the flags immediately after the op. I could have
6011 changed the writer, but this may allow someone to recover
6012 data they're already frozen, though such a very large hash
6015 GETMARK(hash_flags);
6017 else if (type == SX_HOOK) {
6018 return retrieve_hook_common(aTHX_ cxt, cname, TRUE);
6022 TRACEME(("wlen %" UVuf, len));
6026 /* not a large object, just a large index */
6027 SV **svh = av_fetch(cxt->aseen, len, FALSE);
6029 CROAK(("Object #%" UVuf " should have been retrieved already",
6032 TRACEME(("had retrieved #%" UVuf " at 0x%" UVxf, len, PTR2UV(sv)));
6037 sv = get_lstring(aTHX_ cxt, len, 0, cname);
6040 sv = get_lstring(aTHX_ cxt, len, 1, cname);
6043 sv = get_larray(aTHX_ cxt, len, cname);
6045 /* <5.12 you could store larger hashes, but cannot iterate over them.
6046 So we reject them, it's a bug. */
6048 sv = get_lhash(aTHX_ cxt, len, hash_flags, cname);
6051 sv = get_lhash(aTHX_ cxt, len, 0, cname);
6054 CROAK(("Unexpected type %d in retrieve_lobject\n", type));
6057 TRACEME(("ok (retrieve_lobject at 0x%" UVxf ")", PTR2UV(sv)));
6060 PERL_UNUSED_ARG(cname);
6062 /* previously this (brokenly) checked the length value and only failed if
6063 the length was over 4G.
6064 Since this op should only occur with objects over 4GB (or 2GB) we can just
6067 CROAK(("Invalid large object op for this 32bit system"));
6074 * Retrieve defined integer in network order.
6075 * Layout is SX_NETINT <data>, whith SX_NETINT already read.
6077 static SV *retrieve_netint(pTHX_ stcxt_t *cxt, const char *cname)
6083 TRACEME(("retrieve_netint (#%d)", (int)cxt->tagnum));
6087 sv = newSViv((int) ntohl(iv));
6088 TRACEME(("network integer %d", (int) ntohl(iv)));
6091 TRACEME(("network integer (as-is) %d", iv));
6093 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6094 SEEN_NN(sv, stash, 0); /* Associate this new scalar with tag "tagnum" */
6096 TRACEME(("ok (retrieve_netint at 0x%" UVxf ")", PTR2UV(sv)));
6104 * Retrieve defined double.
6105 * Layout is SX_DOUBLE <data>, whith SX_DOUBLE already read.
6107 static SV *retrieve_double(pTHX_ stcxt_t *cxt, const char *cname)
6113 TRACEME(("retrieve_double (#%d)", (int)cxt->tagnum));
6115 READ(&nv, sizeof(nv));
6117 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6118 SEEN_NN(sv, stash, 0); /* Associate this new scalar with tag "tagnum" */
6120 TRACEME(("double %" NVff, nv));
6121 TRACEME(("ok (retrieve_double at 0x%" UVxf ")", PTR2UV(sv)));
6129 * Retrieve defined byte (small integer within the [-128, +127] range).
6130 * Layout is SX_BYTE <data>, whith SX_BYTE already read.
6132 static SV *retrieve_byte(pTHX_ stcxt_t *cxt, const char *cname)
6138 /* MSVC 2017 doesn't handle the AIX workaround well */
6141 signed char tmp; /* Workaround for AIX cc bug --H.Merijn Brand */
6144 TRACEME(("retrieve_byte (#%d)", (int)cxt->tagnum));
6147 TRACEME(("small integer read as %d", (unsigned char) siv));
6148 tmp = (unsigned char) siv - 128;
6150 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6151 SEEN_NN(sv, stash, 0); /* Associate this new scalar with tag "tagnum" */
6153 TRACEME(("byte %d", tmp));
6154 TRACEME(("ok (retrieve_byte at 0x%" UVxf ")", PTR2UV(sv)));
6162 * Return the undefined value.
6164 static SV *retrieve_undef(pTHX_ stcxt_t *cxt, const char *cname)
6169 TRACEME(("retrieve_undef"));
6172 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6173 SEEN_NN(sv, stash, 0);
6181 * Return the immortal undefined value.
6183 static SV *retrieve_sv_undef(pTHX_ stcxt_t *cxt, const char *cname)
6185 SV *sv = &PL_sv_undef;
6188 TRACEME(("retrieve_sv_undef"));
6190 /* Special case PL_sv_undef, as av_fetch uses it internally to mark
6191 deleted elements, and will return NULL (fetch failed) whenever it
6193 if (cxt->where_is_undef == UNSET_NTAG_T) {
6194 cxt->where_is_undef = cxt->tagnum;
6196 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6197 SEEN_NN(sv, stash, 1);
6204 * Return the immortal yes value.
6206 static SV *retrieve_sv_yes(pTHX_ stcxt_t *cxt, const char *cname)
6208 SV *sv = &PL_sv_yes;
6211 TRACEME(("retrieve_sv_yes"));
6213 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6214 SEEN_NN(sv, stash, 1);
6221 * Return the immortal no value.
6223 static SV *retrieve_sv_no(pTHX_ stcxt_t *cxt, const char *cname)
6228 TRACEME(("retrieve_sv_no"));
6230 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6231 SEEN_NN(sv, stash, 1);
6236 * retrieve_svundef_elem
6238 * Return &PL_sv_placeholder, representing &PL_sv_undef in an array. This
6239 * is a bit of a hack, but we already use SX_SV_UNDEF to mean a nonexistent
6240 * element, for historical reasons.
6242 static SV *retrieve_svundef_elem(pTHX_ stcxt_t *cxt, const char *cname)
6244 TRACEME(("retrieve_svundef_elem"));
6246 /* SEEN reads the contents of its SV argument, which we are not
6247 supposed to do with &PL_sv_placeholder. */
6248 SEEN_NN(&PL_sv_undef, cname, 1);
6250 return &PL_sv_placeholder;
6256 * Retrieve a whole array.
6257 * Layout is SX_ARRAY <size> followed by each item, in increasing index order.
6258 * Each item is stored as <object>.
6260 * When we come here, SX_ARRAY has been read already.
6262 static SV *retrieve_array(pTHX_ stcxt_t *cxt, const char *cname)
6268 bool seen_null = FALSE;
6270 TRACEME(("retrieve_array (#%d)", (int)cxt->tagnum));
6273 * Read length, and allocate array, then pre-extend it.
6277 TRACEME(("size = %d", (int)len));
6279 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6280 SEEN_NN(av, stash, 0); /* Will return if array not allocated nicely */
6284 return (SV *) av; /* No data follow if array is empty */
6287 * Now get each item in turn...
6290 for (i = 0; i < len; i++) {
6291 TRACEME(("(#%d) item", (int)i));
6292 sv = retrieve(aTHX_ cxt, 0); /* Retrieve item */
6295 if (sv == &PL_sv_undef) {
6299 if (sv == &PL_sv_placeholder)
6301 if (av_store(av, i, sv) == 0)
6304 if (seen_null) av_fill(av, len-1);
6306 TRACEME(("ok (retrieve_array at 0x%" UVxf ")", PTR2UV(av)));
6313 /* internal method with len already read */
6315 static SV *get_larray(pTHX_ stcxt_t *cxt, UV len, const char *cname)
6321 bool seen_null = FALSE;
6323 TRACEME(("get_larray (#%d) %lu", (int)cxt->tagnum, (unsigned long)len));
6326 * allocate array, then pre-extend it.
6330 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6331 SEEN_NN(av, stash, 0); /* Will return if array not allocated nicely */
6336 * Now get each item in turn...
6339 for (i = 0; i < len; i++) {
6340 TRACEME(("(#%d) item", (int)i));
6341 sv = retrieve(aTHX_ cxt, 0); /* Retrieve item */
6344 if (sv == &PL_sv_undef) {
6348 if (sv == &PL_sv_placeholder)
6350 if (av_store(av, i, sv) == 0)
6353 if (seen_null) av_fill(av, len-1);
6355 TRACEME(("ok (get_larray at 0x%" UVxf ")", PTR2UV(av)));
6363 * Retrieve a overlong hash table.
6364 * <len> is already read. What follows is each key/value pair, in random order.
6365 * Keys are stored as <length> <data>, the <data> section being omitted
6367 * Values are stored as <object>.
6370 static SV *get_lhash(pTHX_ stcxt_t *cxt, UV len, int hash_flags, const char *cname)
6378 TRACEME(("get_lhash (#%d)", (int)cxt->tagnum));
6380 #ifdef HAS_RESTRICTED_HASHES
6381 PERL_UNUSED_ARG(hash_flags);
6383 if (hash_flags & SHV_RESTRICTED) {
6384 if (cxt->derestrict < 0)
6385 cxt->derestrict = (SvTRUE
6386 (get_sv("Storable::downgrade_restricted", GV_ADD))
6388 if (cxt->derestrict == 0)
6389 RESTRICTED_HASH_CROAK();
6393 TRACEME(("size = %lu", (unsigned long)len));
6395 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6396 SEEN_NN(hv, stash, 0); /* Will return if table not allocated properly */
6398 return (SV *) hv; /* No data follow if table empty */
6399 TRACEME(("split %lu", (unsigned long)len+1));
6400 hv_ksplit(hv, len+1); /* pre-extend hash to save multiple splits */
6403 * Now get each key/value pair in turn...
6406 for (i = 0; i < len; i++) {
6411 TRACEME(("(#%d) value", (int)i));
6412 sv = retrieve(aTHX_ cxt, 0);
6418 * Since we're reading into kbuf, we must ensure we're not
6419 * recursing between the read and the hv_store() where it's used.
6420 * Hence the key comes after the value.
6423 RLEN(size); /* Get key size */
6424 KBUFCHK((STRLEN)size); /* Grow hash key read pool if needed */
6427 kbuf[size] = '\0'; /* Mark string end, just in case */
6428 TRACEME(("(#%d) key '%s'", (int)i, kbuf));
6431 * Enter key/value pair into hash table.
6434 if (hv_store(hv, kbuf, (U32) size, sv, 0) == 0)
6438 TRACEME(("ok (get_lhash at 0x%" UVxf ")", PTR2UV(hv)));
6446 * Retrieve a whole hash table.
6447 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
6448 * Keys are stored as <length> <data>, the <data> section being omitted
6450 * Values are stored as <object>.
6452 * When we come here, SX_HASH has been read already.
6454 static SV *retrieve_hash(pTHX_ stcxt_t *cxt, const char *cname)
6463 TRACEME(("retrieve_hash (#%d)", (int)cxt->tagnum));
6466 * Read length, allocate table.
6470 TRACEME(("size = %d", (int)len));
6472 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6473 SEEN_NN(hv, stash, 0); /* Will return if table not allocated properly */
6475 return (SV *) hv; /* No data follow if table empty */
6476 TRACEME(("split %d", (int)len+1));
6477 hv_ksplit(hv, len+1); /* pre-extend hash to save multiple splits */
6480 * Now get each key/value pair in turn...
6483 for (i = 0; i < len; i++) {
6488 TRACEME(("(#%d) value", (int)i));
6489 sv = retrieve(aTHX_ cxt, 0);
6495 * Since we're reading into kbuf, we must ensure we're not
6496 * recursing between the read and the hv_store() where it's used.
6497 * Hence the key comes after the value.
6500 RLEN(size); /* Get key size */
6501 KBUFCHK((STRLEN)size); /* Grow hash key read pool if needed */
6504 kbuf[size] = '\0'; /* Mark string end, just in case */
6505 TRACEME(("(#%d) key '%s'", (int)i, kbuf));
6508 * Enter key/value pair into hash table.
6511 if (hv_store(hv, kbuf, (U32) size, sv, 0) == 0)
6515 TRACEME(("ok (retrieve_hash at 0x%" UVxf ")", PTR2UV(hv)));
6523 * Retrieve a whole hash table.
6524 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
6525 * Keys are stored as <length> <data>, the <data> section being omitted
6527 * Values are stored as <object>.
6529 * When we come here, SX_HASH has been read already.
6531 static SV *retrieve_flag_hash(pTHX_ stcxt_t *cxt, const char *cname)
6542 GETMARK(hash_flags);
6543 TRACEME(("retrieve_flag_hash (#%d)", (int)cxt->tagnum));
6545 * Read length, allocate table.
6548 #ifndef HAS_RESTRICTED_HASHES
6549 if (hash_flags & SHV_RESTRICTED) {
6550 if (cxt->derestrict < 0)
6551 cxt->derestrict = (SvTRUE
6552 (get_sv("Storable::downgrade_restricted", GV_ADD))
6554 if (cxt->derestrict == 0)
6555 RESTRICTED_HASH_CROAK();
6560 TRACEME(("size = %d, flags = %d", (int)len, hash_flags));
6562 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6563 SEEN_NN(hv, stash, 0); /* Will return if table not allocated properly */
6565 return (SV *) hv; /* No data follow if table empty */
6566 TRACEME(("split %d", (int)len+1));
6567 hv_ksplit(hv, len+1); /* pre-extend hash to save multiple splits */
6570 * Now get each key/value pair in turn...
6573 for (i = 0; i < len; i++) {
6575 int store_flags = 0;
6580 TRACEME(("(#%d) value", (int)i));
6581 sv = retrieve(aTHX_ cxt, 0);
6586 #ifdef HAS_RESTRICTED_HASHES
6587 if ((hash_flags & SHV_RESTRICTED) && (flags & SHV_K_LOCKED))
6591 if (flags & SHV_K_ISSV) {
6592 /* XXX you can't set a placeholder with an SV key.
6593 Then again, you can't get an SV key.
6594 Without messing around beyond what the API is supposed to do.
6597 TRACEME(("(#%d) keysv, flags=%d", (int)i, flags));
6598 keysv = retrieve(aTHX_ cxt, 0);
6602 if (!hv_store_ent(hv, keysv, sv, 0))
6607 * Since we're reading into kbuf, we must ensure we're not
6608 * recursing between the read and the hv_store() where it's used.
6609 * Hence the key comes after the value.
6612 if (flags & SHV_K_PLACEHOLDER) {
6614 sv = &PL_sv_placeholder;
6615 store_flags |= HVhek_PLACEHOLD;
6617 if (flags & SHV_K_UTF8) {
6618 #ifdef HAS_UTF8_HASHES
6619 store_flags |= HVhek_UTF8;
6621 if (cxt->use_bytes < 0)
6623 = (SvTRUE(get_sv("Storable::drop_utf8", GV_ADD))
6625 if (cxt->use_bytes == 0)
6629 #ifdef HAS_UTF8_HASHES
6630 if (flags & SHV_K_WASUTF8)
6631 store_flags |= HVhek_WASUTF8;
6634 RLEN(size); /* Get key size */
6635 KBUFCHK((STRLEN)size);/* Grow hash key read pool if needed */
6638 kbuf[size] = '\0'; /* Mark string end, just in case */
6639 TRACEME(("(#%d) key '%s' flags %X store_flags %X", (int)i, kbuf,
6640 flags, store_flags));
6643 * Enter key/value pair into hash table.
6646 #ifdef HAS_RESTRICTED_HASHES
6647 if (hv_store_flags(hv, kbuf, size, sv, 0, store_flags) == 0)
6650 if (!(store_flags & HVhek_PLACEHOLD))
6651 if (hv_store(hv, kbuf, size, sv, 0) == 0)
6656 #ifdef HAS_RESTRICTED_HASHES
6657 if (hash_flags & SHV_RESTRICTED)
6661 TRACEME(("ok (retrieve_hash at 0x%" UVxf ")", PTR2UV(hv)));
6669 * Return a code reference.
6671 static SV *retrieve_code(pTHX_ stcxt_t *cxt, const char *cname)
6673 #if PERL_VERSION < 6
6674 CROAK(("retrieve_code does not work with perl 5.005 or less\n"));
6680 SV *sv, *text, *sub, *errsv;
6683 TRACEME(("retrieve_code (#%d)", (int)cxt->tagnum));
6686 * Insert dummy SV in the aseen array so that we don't screw
6687 * up the tag numbers. We would just make the internal
6688 * scalar an untagged item in the stream, but
6689 * retrieve_scalar() calls SEEN(). So we just increase the
6692 tagnum = cxt->tagnum;
6694 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6695 SEEN_NN(sv, stash, 0);
6698 * Retrieve the source of the code reference
6699 * as a small or large scalar
6705 text = retrieve_scalar(aTHX_ cxt, cname);
6708 text = retrieve_lscalar(aTHX_ cxt, cname);
6711 text = retrieve_utf8str(aTHX_ cxt, cname);
6714 text = retrieve_lutf8str(aTHX_ cxt, cname);
6717 CROAK(("Unexpected type %d in retrieve_code\n", (int)type));
6721 CROAK(("Unable to retrieve code\n"));
6725 * prepend "sub " to the source
6728 sub = newSVpvs("sub ");
6731 sv_catpv(sub, SvPV_nolen(text)); /* XXX no sv_catsv! */
6735 * evaluate the source to a code reference and use the CV value
6738 if (cxt->eval == NULL) {
6739 cxt->eval = get_sv("Storable::Eval", GV_ADD);
6740 SvREFCNT_inc(cxt->eval);
6742 if (!SvTRUE(cxt->eval)) {
6743 if (cxt->forgive_me == 0 ||
6744 (cxt->forgive_me < 0 &&
6745 !(cxt->forgive_me = SvTRUE
6746 (get_sv("Storable::forgive_me", GV_ADD)) ? 1 : 0))
6748 CROAK(("Can't eval, please set $Storable::Eval to a true value"));
6751 /* fix up the dummy entry... */
6752 av_store(cxt->aseen, tagnum, SvREFCNT_inc(sv));
6760 errsv = get_sv("@", GV_ADD);
6761 SvPVCLEAR(errsv); /* clear $@ */
6762 if (SvROK(cxt->eval) && SvTYPE(SvRV(cxt->eval)) == SVt_PVCV) {
6764 XPUSHs(sv_2mortal(newSVsv(sub)));
6766 count = call_sv(cxt->eval, G_SCALAR);
6768 CROAK(("Unexpected return value from $Storable::Eval callback\n"));
6770 eval_sv(sub, G_SCALAR);
6776 if (SvTRUE(errsv)) {
6777 CROAK(("code %s caused an error: %s",
6778 SvPV_nolen(sub), SvPV_nolen(errsv)));
6781 if (cv && SvROK(cv) && SvTYPE(SvRV(cv)) == SVt_PVCV) {
6784 CROAK(("code %s did not evaluate to a subroutine reference\n",
6788 SvREFCNT_inc(sv); /* XXX seems to be necessary */
6793 /* fix up the dummy entry... */
6794 av_store(cxt->aseen, tagnum, SvREFCNT_inc(sv));
6800 static SV *retrieve_regexp(pTHX_ stcxt_t *cxt, const char *cname) {
6801 #if PERL_VERSION >= 8
6817 if (op_flags & SHR_U32_RE_LEN) {
6823 re = sv_2mortal(NEWSV(10002, re_len ? re_len : 1));
6824 READ(SvPVX(re), re_len);
6825 SvCUR_set(re, re_len);
6830 flags = sv_2mortal(NEWSV(10002, flags_len ? flags_len : 1));
6831 READ(SvPVX(flags), flags_len);
6832 SvCUR_set(flags, flags_len);
6833 *SvEND(flags) = '\0';
6843 count = call_pv("Storable::_make_re", G_SCALAR);
6848 CROAK(("Bad count %d calling _make_re", count));
6855 CROAK(("_make_re didn't return a reference"));
6859 stash = cname ? gv_stashpv(cname, GV_ADD) : 0;
6860 SEEN_NN(sv, stash, 0);
6867 CROAK(("retrieve_regexp does not work with 5.6 or earlier"));
6872 * old_retrieve_array
6874 * Retrieve a whole array in pre-0.6 binary format.
6876 * Layout is SX_ARRAY <size> followed by each item, in increasing index order.
6877 * Each item is stored as SX_ITEM <object> or SX_IT_UNDEF for "holes".
6879 * When we come here, SX_ARRAY has been read already.
6881 static SV *old_retrieve_array(pTHX_ stcxt_t *cxt, const char *cname)
6889 PERL_UNUSED_ARG(cname);
6890 TRACEME(("old_retrieve_array (#%d)", (int)cxt->tagnum));
6893 * Read length, and allocate array, then pre-extend it.
6897 TRACEME(("size = %d", (int)len));
6899 SEEN0_NN(av, 0); /* Will return if array not allocated nicely */
6903 return (SV *) av; /* No data follow if array is empty */
6906 * Now get each item in turn...
6909 for (i = 0; i < len; i++) {
6911 if (c == SX_IT_UNDEF) {
6912 TRACEME(("(#%d) undef item", (int)i));
6913 continue; /* av_extend() already filled us with undef */
6916 (void) retrieve_other(aTHX_ cxt, 0);/* Will croak out */
6917 TRACEME(("(#%d) item", (int)i));
6918 sv = retrieve(aTHX_ cxt, 0); /* Retrieve item */
6921 if (av_store(av, i, sv) == 0)
6925 TRACEME(("ok (old_retrieve_array at 0x%" UVxf ")", PTR2UV(av)));
6933 * Retrieve a whole hash table in pre-0.6 binary format.
6935 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
6936 * Keys are stored as SX_KEY <length> <data>, the <data> section being omitted
6938 * Values are stored as SX_VALUE <object> or SX_VL_UNDEF for "holes".
6940 * When we come here, SX_HASH has been read already.
6942 static SV *old_retrieve_hash(pTHX_ stcxt_t *cxt, const char *cname)
6950 SV *sv_h_undef = (SV *) 0; /* hv_store() bug */
6952 PERL_UNUSED_ARG(cname);
6953 TRACEME(("old_retrieve_hash (#%d)", (int)cxt->tagnum));
6956 * Read length, allocate table.
6960 TRACEME(("size = %d", (int)len));
6962 SEEN0_NN(hv, 0); /* Will return if table not allocated properly */
6964 return (SV *) hv; /* No data follow if table empty */
6965 TRACEME(("split %d", (int)len+1));
6966 hv_ksplit(hv, len+1); /* pre-extend hash to save multiple splits */
6969 * Now get each key/value pair in turn...
6972 for (i = 0; i < len; i++) {
6978 if (c == SX_VL_UNDEF) {
6979 TRACEME(("(#%d) undef value", (int)i));
6981 * Due to a bug in hv_store(), it's not possible to pass
6982 * &PL_sv_undef to hv_store() as a value, otherwise the
6983 * associated key will not be creatable any more. -- RAM, 14/01/97
6986 sv_h_undef = newSVsv(&PL_sv_undef);
6987 sv = SvREFCNT_inc(sv_h_undef);
6988 } else if (c == SX_VALUE) {
6989 TRACEME(("(#%d) value", (int)i));
6990 sv = retrieve(aTHX_ cxt, 0);
6994 (void) retrieve_other(aTHX_ cxt, 0); /* Will croak out */
6998 * Since we're reading into kbuf, we must ensure we're not
6999 * recursing between the read and the hv_store() where it's used.
7000 * Hence the key comes after the value.
7005 (void) retrieve_other(aTHX_ cxt, 0); /* Will croak out */
7006 RLEN(size); /* Get key size */
7007 KBUFCHK((STRLEN)size); /* Grow hash key read pool if needed */
7010 kbuf[size] = '\0'; /* Mark string end, just in case */
7011 TRACEME(("(#%d) key '%s'", (int)i, kbuf));
7014 * Enter key/value pair into hash table.
7017 if (hv_store(hv, kbuf, (U32) size, sv, 0) == 0)
7021 TRACEME(("ok (retrieve_hash at 0x%" UVxf ")", PTR2UV(hv)));
7027 *** Retrieval engine.
7033 * Make sure the stored data we're trying to retrieve has been produced
7034 * on an ILP compatible system with the same byteorder. It croaks out in
7035 * case an error is detected. [ILP = integer-long-pointer sizes]
7036 * Returns null if error is detected, &PL_sv_undef otherwise.
7038 * Note that there's no byte ordering info emitted when network order was
7039 * used at store time.
7041 static SV *magic_check(pTHX_ stcxt_t *cxt)
7043 /* The worst case for a malicious header would be old magic (which is
7044 longer), major, minor, byteorder length byte of 255, 255 bytes of
7045 garbage, sizeof int, long, pointer, NV.
7046 So the worse of that we can read is 255 bytes of garbage plus 4.
7047 Err, I am assuming 8 bit bytes here. Please file a bug report if you're
7048 compiling perl on a system with chars that are larger than 8 bits.
7049 (Even Crays aren't *that* perverse).
7051 unsigned char buf[4 + 255];
7052 unsigned char *current;
7055 int use_network_order;
7059 int version_minor = 0;
7061 TRACEME(("magic_check"));
7064 * The "magic number" is only for files, not when freezing in memory.
7068 /* This includes the '\0' at the end. I want to read the extra byte,
7069 which is usually going to be the major version number. */
7070 STRLEN len = sizeof(magicstr);
7073 READ(buf, (SSize_t)(len)); /* Not null-terminated */
7075 /* Point at the byte after the byte we read. */
7076 current = buf + --len; /* Do the -- outside of macros. */
7078 if (memNE(buf, magicstr, len)) {
7080 * Try to read more bytes to check for the old magic number, which
7084 TRACEME(("trying for old magic number"));
7086 old_len = sizeof(old_magicstr) - 1;
7087 READ(current + 1, (SSize_t)(old_len - len));
7089 if (memNE(buf, old_magicstr, old_len))
7090 CROAK(("File is not a perl storable"));
7092 current = buf + old_len;
7094 use_network_order = *current;
7096 GETMARK(use_network_order);
7100 * Starting with 0.6, the "use_network_order" byte flag is also used to
7101 * indicate the version number of the binary, and therefore governs the
7102 * setting of sv_retrieve_vtbl. See magic_write().
7104 if (old_magic && use_network_order > 1) {
7105 /* 0.1 dump - use_network_order is really byte order length */
7109 version_major = use_network_order >> 1;
7111 cxt->retrieve_vtbl = (SV*(**)(pTHX_ stcxt_t *cxt, const char *cname)) (version_major > 0 ? sv_retrieve : sv_old_retrieve);
7113 TRACEME(("magic_check: netorder = 0x%x", use_network_order));
7117 * Starting with 0.7 (binary major 2), a full byte is dedicated to the
7118 * minor version of the protocol. See magic_write().
7121 if (version_major > 1)
7122 GETMARK(version_minor);
7124 cxt->ver_major = version_major;
7125 cxt->ver_minor = version_minor;
7127 TRACEME(("binary image version is %d.%d", version_major, version_minor));
7130 * Inter-operability sanity check: we can't retrieve something stored
7131 * using a format more recent than ours, because we have no way to
7132 * know what has changed, and letting retrieval go would mean a probable
7133 * failure reporting a "corrupted" storable file.
7137 version_major > STORABLE_BIN_MAJOR ||
7138 (version_major == STORABLE_BIN_MAJOR &&
7139 version_minor > STORABLE_BIN_MINOR)
7142 TRACEME(("but I am version is %d.%d", STORABLE_BIN_MAJOR,
7143 STORABLE_BIN_MINOR));
7145 if (version_major == STORABLE_BIN_MAJOR) {
7146 TRACEME(("cxt->accept_future_minor is %d",
7147 cxt->accept_future_minor));
7148 if (cxt->accept_future_minor < 0)
7149 cxt->accept_future_minor
7150 = (SvTRUE(get_sv("Storable::accept_future_minor",
7153 if (cxt->accept_future_minor == 1)
7154 croak_now = 0; /* Don't croak yet. */
7157 CROAK(("Storable binary image v%d.%d more recent than I am (v%d.%d)",
7158 version_major, version_minor,
7159 STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR));
7164 * If they stored using network order, there's no byte ordering
7165 * information to check.
7168 if ((cxt->netorder = (use_network_order & 0x1))) /* Extra () for -Wall */
7169 return &PL_sv_undef; /* No byte ordering info */
7171 /* In C truth is 1, falsehood is 0. Very convenient. */
7172 use_NV_size = version_major >= 2 && version_minor >= 2;
7174 if (version_major >= 0) {
7178 c = use_network_order;
7180 length = c + 3 + use_NV_size;
7181 READ(buf, length); /* Not null-terminated */
7183 TRACEME(("byte order '%.*s' %d", c, buf, c));
7185 #ifdef USE_56_INTERWORK_KLUDGE
7186 /* No point in caching this in the context as we only need it once per
7187 retrieve, and we need to recheck it each read. */
7188 if (SvTRUE(get_sv("Storable::interwork_56_64bit", GV_ADD))) {
7189 if ((c != (sizeof (byteorderstr_56) - 1))
7190 || memNE(buf, byteorderstr_56, c))
7191 CROAK(("Byte order is not compatible"));
7195 if ((c != (sizeof (byteorderstr) - 1))
7196 || memNE(buf, byteorderstr, c))
7197 CROAK(("Byte order is not compatible"));
7203 if ((int) *current++ != sizeof(int))
7204 CROAK(("Integer size is not compatible"));
7207 if ((int) *current++ != sizeof(long))
7208 CROAK(("Long integer size is not compatible"));
7210 /* sizeof(char *) */
7211 if ((int) *current != sizeof(char *))
7212 CROAK(("Pointer size is not compatible"));
7216 if ((int) *++current != sizeof(NV))
7217 CROAK(("Double size is not compatible"));
7220 return &PL_sv_undef; /* OK */
7226 * Recursively retrieve objects from the specified file and return their
7227 * root SV (which may be an AV or an HV for what we care).
7228 * Returns null if there is a problem.
7230 static SV *retrieve(pTHX_ stcxt_t *cxt, const char *cname)
7236 TRACEME(("retrieve"));
7239 * Grab address tag which identifies the object if we are retrieving
7240 * an older format. Since the new binary format counts objects and no
7241 * longer explicitly tags them, we must keep track of the correspondence
7244 * The following section will disappear one day when the old format is
7245 * no longer supported, hence the final "goto" in the "if" block.
7248 if (cxt->hseen) { /* Retrieving old binary */
7250 if (cxt->netorder) {
7252 READ(&nettag, sizeof(I32)); /* Ordered sequence of I32 */
7253 tag = (stag_t) nettag;
7255 READ(&tag, sizeof(stag_t)); /* Original address of the SV */
7258 if (type == SX_OBJECT) {
7260 svh = hv_fetch(cxt->hseen, (char *) &tag, sizeof(tag), FALSE);
7262 CROAK(("Old tag 0x%" UVxf " should have been mapped already",
7264 tagn = SvIV(*svh); /* Mapped tag number computed earlier below */
7267 * The following code is common with the SX_OBJECT case below.
7270 svh = av_fetch(cxt->aseen, tagn, FALSE);
7272 CROAK(("Object #%" IVdf " should have been retrieved already",
7275 TRACEME(("has retrieved #%d at 0x%" UVxf, (int)tagn, PTR2UV(sv)));
7276 SvREFCNT_inc(sv); /* One more reference to this same sv */
7277 return sv; /* The SV pointer where object was retrieved */
7281 * Map new object, but don't increase tagnum. This will be done
7282 * by each of the retrieve_* functions when they call SEEN().
7284 * The mapping associates the "tag" initially present with a unique
7285 * tag number. See test for SX_OBJECT above to see how this is perused.
7288 if (!hv_store(cxt->hseen, (char *) &tag, sizeof(tag),
7289 newSViv(cxt->tagnum), 0))
7296 * Regular post-0.6 binary format.
7301 TRACEME(("retrieve type = %d", type));
7304 * Are we dealing with an object we should have already retrieved?
7307 if (type == SX_OBJECT) {
7312 /* A 32-bit system can't have over 2**31 objects anyway */
7314 CROAK(("Object #%" IVdf " out of range", (IV)tag));
7316 /* Older versions of Storable on with 64-bit support on 64-bit
7317 systems can produce values above the 2G boundary (or wrapped above
7318 the 4G boundary, which we can't do much about), treat those as
7320 This same commit stores tag ids over the 2G boundary as long tags
7321 since older Storables will mis-handle them as short tags.
7323 svh = av_fetch(cxt->aseen, (U32)tag, FALSE);
7325 CROAK(("Object #%" IVdf " should have been retrieved already",
7328 TRACEME(("had retrieved #%d at 0x%" UVxf, (int)tag, PTR2UV(sv)));
7329 SvREFCNT_inc(sv); /* One more reference to this same sv */
7330 return sv; /* The SV pointer where object was retrieved */
7331 } else if (type >= SX_LAST && cxt->ver_minor > STORABLE_BIN_MINOR) {
7332 if (cxt->accept_future_minor < 0)
7333 cxt->accept_future_minor
7334 = (SvTRUE(get_sv("Storable::accept_future_minor",
7337 if (cxt->accept_future_minor == 1) {
7338 CROAK(("Storable binary image v%d.%d contains data of type %d. "
7339 "This Storable is v%d.%d and can only handle data types up to %d",
7340 cxt->ver_major, cxt->ver_minor, type,
7341 STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR, SX_LAST - 1));
7345 first_time: /* Will disappear when support for old format is dropped */
7348 * Okay, first time through for this one.
7351 sv = RETRIEVE(cxt, type)(aTHX_ cxt, cname);
7353 return (SV *) 0; /* Failed */
7356 * Old binary formats (pre-0.7).
7358 * Final notifications, ended by SX_STORED may now follow.
7359 * Currently, the only pertinent notification to apply on the
7360 * freshly retrieved object is either:
7361 * SX_CLASS <char-len> <classname> for short classnames.
7362 * SX_LG_CLASS <int-len> <classname> for larger one (rare!).
7363 * Class name is then read into the key buffer pool used by
7364 * hash table key retrieval.
7367 if (cxt->ver_major < 2) {
7368 while ((type = GETCHAR()) != SX_STORED) {
7373 GETMARK(len); /* Length coded on a single char */
7375 case SX_LG_CLASS: /* Length coded on a regular integer */
7380 return (SV *) 0; /* Failed */
7382 KBUFCHK((STRLEN)len); /* Grow buffer as necessary */
7385 kbuf[len] = '\0'; /* Mark string end */
7386 stash = gv_stashpvn(kbuf, len, GV_ADD);
7391 TRACEME(("ok (retrieved 0x%" UVxf ", refcnt=%d, %s)", PTR2UV(sv),
7392 (int)SvREFCNT(sv) - 1, sv_reftype(sv, FALSE)));
7400 * Retrieve data held in file and return the root object.
7401 * Common routine for pretrieve and mretrieve.
7403 static SV *do_retrieve(
7412 int is_tainted; /* Is input source tainted? */
7413 int pre_06_fmt = 0; /* True with pre Storable 0.6 formats */
7415 TRACEMED(("do_retrieve (optype = 0x%x, flags=0x%x)",
7416 (unsigned)optype, (unsigned)flags));
7418 optype |= ST_RETRIEVE;
7422 * Sanity assertions for retrieve dispatch tables.
7425 ASSERT(sizeof(sv_old_retrieve) == sizeof(sv_retrieve),
7426 ("old and new retrieve dispatch table have same size"));
7427 ASSERT(sv_old_retrieve[(int)SX_LAST] == retrieve_other,
7428 ("SX_LAST entry correctly initialized in old dispatch table"));
7429 ASSERT(sv_retrieve[(int)SX_LAST] == retrieve_other,
7430 ("SX_LAST entry correctly initialized in new dispatch table"));
7433 * Workaround for CROAK leak: if they enter with a "dirty" context,
7434 * free up memory for them now.
7439 clean_context(aTHX_ cxt);
7442 * Now that STORABLE_xxx hooks exist, it is possible that they try to
7443 * re-enter retrieve() via the hooks.
7447 cxt = allocate_context(aTHX_ cxt);
7454 ASSERT(cxt->entry == 1, ("starting new recursion"));
7455 ASSERT(!cxt->s_dirty, ("clean context"));
7460 * Data is loaded into the memory buffer when f is NULL, unless 'in' is
7461 * also NULL, in which case we're expecting the data to already lie
7462 * in the buffer (dclone case).
7465 KBUFINIT(); /* Allocate hash key reading pool once */
7471 const char *orig = SvPV(in, length);
7473 /* This is quite deliberate. I want the UTF8 routines
7474 to encounter the '\0' which perl adds at the end
7475 of all scalars, so that any new string also has
7478 STRLEN klen_tmp = length + 1;
7479 bool is_utf8 = TRUE;
7481 /* Just casting the &klen to (STRLEN) won't work
7482 well if STRLEN and I32 are of different widths.
7484 asbytes = (char*)bytes_from_utf8((U8*)orig,
7488 CROAK(("Frozen string corrupt - contains characters outside 0-255"));
7490 if (asbytes != orig) {
7491 /* String has been converted.
7492 There is no need to keep any reference to
7494 in = sv_newmortal();
7495 /* We donate the SV the malloc()ed string
7496 bytes_from_utf8 returned us. */
7497 SvUPGRADE(in, SVt_PV);
7499 SvPV_set(in, asbytes);
7500 SvLEN_set(in, klen_tmp);
7501 SvCUR_set(in, klen_tmp - 1);
7505 MBUF_SAVE_AND_LOAD(in);
7509 * Magic number verifications.
7511 * This needs to be done before calling init_retrieve_context()
7512 * since the format indication in the file are necessary to conduct
7513 * some of the initializations.
7516 cxt->fio = f; /* Where I/O are performed */
7518 if (!magic_check(aTHX_ cxt))
7519 CROAK(("Magic number checking on storable %s failed",
7520 cxt->fio ? "file" : "string"));
7522 TRACEME(("data stored in %s format",
7523 cxt->netorder ? "net order" : "native"));
7526 * Check whether input source is tainted, so that we don't wrongly
7527 * taint perfectly good values...
7529 * We assume file input is always tainted. If both 'f' and 'in' are
7530 * NULL, then we come from dclone, and tainted is already filled in
7531 * the context. That's a kludge, but the whole dclone() thing is
7532 * already quite a kludge anyway! -- RAM, 15/09/2000.
7535 is_tainted = f ? 1 : (in ? SvTAINTED(in) : cxt->s_tainted);
7536 TRACEME(("input source is %s", is_tainted ? "tainted" : "trusted"));
7537 init_retrieve_context(aTHX_ cxt, optype, is_tainted);
7539 ASSERT(is_retrieving(aTHX), ("within retrieve operation"));
7541 sv = retrieve(aTHX_ cxt, 0); /* Recursively retrieve object, get root SV */
7550 pre_06_fmt = cxt->hseen != NULL; /* Before we clean context */
7553 * The "root" context is never freed.
7556 clean_retrieve_context(aTHX_ cxt);
7557 if (cxt->prev) /* This context was stacked */
7558 free_context(aTHX_ cxt); /* It was not the "root" context */
7561 * Prepare returned value.
7565 TRACEMED(("retrieve ERROR"));
7566 #if (PATCHLEVEL <= 4)
7567 /* perl 5.00405 seems to screw up at this point with an
7568 'attempt to modify a read only value' error reported in the
7569 eval { $self = pretrieve(*FILE) } in _retrieve.
7570 I can't see what the cause of this error is, but I suspect a
7571 bug in 5.004, as it seems to be capable of issuing spurious
7572 errors or core dumping with matches on $@. I'm not going to
7573 spend time on what could be a fruitless search for the cause,
7574 so here's a bodge. If you're running 5.004 and don't like
7575 this inefficiency, either upgrade to a newer perl, or you are
7576 welcome to find the problem and send in a patch.
7580 return &PL_sv_undef; /* Something went wrong, return undef */
7584 TRACEMED(("retrieve got %s(0x%" UVxf ")",
7585 sv_reftype(sv, FALSE), PTR2UV(sv)));
7588 * Backward compatibility with Storable-0.5@9 (which we know we
7589 * are retrieving if hseen is non-null): don't create an extra RV
7590 * for objects since we special-cased it at store time.
7592 * Build a reference to the SV returned by pretrieve even if it is
7593 * already one and not a scalar, for consistency reasons.
7596 if (pre_06_fmt) { /* Was not handling overloading by then */
7598 TRACEMED(("fixing for old formats -- pre 0.6"));
7599 if (sv_type(aTHX_ sv) == svis_REF && (rv = SvRV(sv)) && SvOBJECT(rv)) {
7600 TRACEME(("ended do_retrieve() with an object -- pre 0.6"));
7606 * If reference is overloaded, restore behaviour.
7608 * NB: minor glitch here: normally, overloaded refs are stored specially
7609 * so that we can croak when behaviour cannot be re-installed, and also
7610 * avoid testing for overloading magic at each reference retrieval.
7612 * Unfortunately, the root reference is implicitly stored, so we must
7613 * check for possible overloading now. Furthermore, if we don't restore
7614 * overloading, we cannot croak as if the original ref was, because we
7615 * have no way to determine whether it was an overloaded ref or not in
7618 * It's a pity that overloading magic is attached to the rv, and not to
7619 * the underlying sv as blessing is.
7623 HV *stash = (HV *) SvSTASH(sv);
7624 SV *rv = newRV_noinc(sv);
7625 if (stash && Gv_AMG(stash)) {
7627 TRACEMED(("restored overloading on root reference"));
7629 TRACEMED(("ended do_retrieve() with an object"));
7633 TRACEMED(("regular do_retrieve() end"));
7635 return newRV_noinc(sv);
7641 * Retrieve data held in file and return the root object, undef on error.
7643 static SV *pretrieve(pTHX_ PerlIO *f, IV flag)
7645 TRACEMED(("pretrieve"));
7646 return do_retrieve(aTHX_ f, Nullsv, 0, (int)flag);
7652 * Retrieve data held in scalar and return the root object, undef on error.
7654 static SV *mretrieve(pTHX_ SV *sv, IV flag)
7656 TRACEMED(("mretrieve"));
7657 return do_retrieve(aTHX_ (PerlIO*) 0, sv, 0, (int)flag);
7667 * Deep clone: returns a fresh copy of the original referenced SV tree.
7669 * This is achieved by storing the object in memory and restoring from
7670 * there. Not that efficient, but it should be faster than doing it from
7673 static SV *dclone(pTHX_ SV *sv)
7677 stcxt_t *real_context;
7680 TRACEMED(("dclone"));
7683 * Workaround for CROAK leak: if they enter with a "dirty" context,
7684 * free up memory for them now.
7689 clean_context(aTHX_ cxt);
7692 * Tied elements seem to need special handling.
7695 if ((SvTYPE(sv) == SVt_PVLV
7696 #if PERL_VERSION < 8
7697 || SvTYPE(sv) == SVt_PVMG
7699 ) && (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG)) ==
7700 (SVs_GMG|SVs_SMG|SVs_RMG) &&
7706 * do_store() optimizes for dclone by not freeing its context, should
7707 * we need to allocate one because we're deep cloning from a hook.
7710 if (!do_store(aTHX_ (PerlIO*) 0, sv, ST_CLONE, FALSE, (SV**) 0))
7711 return &PL_sv_undef; /* Error during store */
7714 * Because of the above optimization, we have to refresh the context,
7715 * since a new one could have been allocated and stacked by do_store().
7718 { dSTCXT; real_context = cxt; } /* Sub-block needed for macro */
7719 cxt = real_context; /* And we need this temporary... */
7722 * Now, 'cxt' may refer to a new context.
7726 ASSERT(!cxt->s_dirty, ("clean context"));
7727 ASSERT(!cxt->entry, ("entry will not cause new context allocation"));
7730 TRACEME(("dclone stored %ld bytes", (long)size));
7734 * Since we're passing do_retrieve() both a NULL file and sv, we need
7735 * to pre-compute the taintedness of the input by setting cxt->tainted
7736 * to whatever state our own input string was. -- RAM, 15/09/2000
7738 * do_retrieve() will free non-root context.
7741 cxt->s_tainted = SvTAINTED(sv);
7742 out = do_retrieve(aTHX_ (PerlIO*) 0, Nullsv, ST_CLONE, FLAG_BLESS_OK | FLAG_TIE_OK);
7744 TRACEMED(("dclone returns 0x%" UVxf, PTR2UV(out)));
7754 * The Perl IO GV object distinguishes between input and output for sockets
7755 * but not for plain files. To allow Storable to transparently work on
7756 * plain files and sockets transparently, we have to ask xsubpp to fetch the
7757 * right object for us. Hence the OutputStream and InputStream declarations.
7759 * Before perl 5.004_05, those entries in the standard typemap are not
7760 * defined in perl include files, so we do that here.
7763 #ifndef OutputStream
7764 #define OutputStream PerlIO *
7765 #define InputStream PerlIO *
7766 #endif /* !OutputStream */
7769 storable_free(pTHX_ SV *sv, MAGIC* mg) {
7770 stcxt_t *cxt = (stcxt_t *)SvPVX(sv);
7772 PERL_UNUSED_ARG(mg);
7773 #ifdef USE_PTR_TABLE
7775 ptr_table_free(cxt->pseen);
7779 if (!cxt->membuf_ro && mbase)
7781 if (cxt->membuf_ro && (cxt->msaved).arena)
7782 Safefree((cxt->msaved).arena);
7786 MODULE = Storable PACKAGE = Storable
7792 HV *stash = gv_stashpvn("Storable", 8, GV_ADD);
7793 newCONSTSUB(stash, "BIN_MAJOR", newSViv(STORABLE_BIN_MAJOR));
7794 newCONSTSUB(stash, "BIN_MINOR", newSViv(STORABLE_BIN_MINOR));
7795 newCONSTSUB(stash, "BIN_WRITE_MINOR", newSViv(STORABLE_BIN_WRITE_MINOR));
7797 init_perinterp(aTHX);
7798 gv_fetchpv("Storable::drop_utf8", GV_ADDMULTI, SVt_PV);
7800 /* Only disable the used only once warning if we are in debugging mode. */
7801 gv_fetchpv("Storable::DEBUGME", GV_ADDMULTI, SVt_PV);
7803 #ifdef USE_56_INTERWORK_KLUDGE
7804 gv_fetchpv("Storable::interwork_56_64bit", GV_ADDMULTI, SVt_PV);
7811 init_perinterp(aTHX);
7815 # Store the transitive data closure of given object to disk.
7816 # Returns undef on error, a true value otherwise.
7820 # Same as pstore(), but network order is used for integers and doubles are
7821 # emitted as strings.
7830 RETVAL = do_store(aTHX_ f, obj, 0, ix, (SV **)0) ? &PL_sv_yes : &PL_sv_undef;
7831 /* do_store() can reallocate the stack, so need a sequence point to ensure
7832 that ST(0) knows about it. Hence using two statements. */
7838 # Store the transitive data closure of given object to memory.
7839 # Returns undef on error, a scalar value containing the data otherwise.
7843 # Same as mstore(), but network order is used for integers and doubles are
7844 # emitted as strings.
7852 RETVAL = &PL_sv_undef;
7853 if (!do_store(aTHX_ (PerlIO*) 0, obj, 0, ix, &RETVAL))
7854 RETVAL = &PL_sv_undef;
7859 pretrieve(f, flag = 6)
7863 RETVAL = pretrieve(aTHX_ f, flag);
7868 mretrieve(sv, flag = 6)
7872 RETVAL = mretrieve(aTHX_ sv, flag);
7880 RETVAL = dclone(aTHX_ sv);
7885 last_op_in_netorder()
7887 is_storing = ST_STORE
7888 is_retrieving = ST_RETRIEVE
7895 result = cxt->entry && (cxt->optype & ix) ? TRUE : FALSE;
7897 result = !!last_op_in_netorder(aTHX);
7899 ST(0) = boolSV(result);
7905 RETVAL = SvIV(get_sv("Storable::recursion_limit", GV_ADD));
7912 RETVAL = SvIV(get_sv("Storable::recursion_limit_hash", GV_ADD));