This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate perlio:
[perl5.git] / ext / Storable / Storable.xs
CommitLineData
7a6a85bf
RG
1/*
2 * Store and retrieve mechanism.
3 */
4
5/*
6e0ac6f5 6 * $Id: Storable.xs,v 1.0.1.10 2001/08/28 21:52:14 ram Exp $
7a6a85bf
RG
7 *
8 * Copyright (c) 1995-2000, Raphael Manfredi
9 *
9e21b3d0
JH
10 * You may redistribute only under the same terms as Perl 5, as specified
11 * in the README file that comes with the distribution.
7a6a85bf 12 *
7a6a85bf
RG
13 */
14
15#include <EXTERN.h>
16#include <perl.h>
e993d95c 17#include <patchlevel.h> /* Perl's one, needed since 5.6 */
7a6a85bf
RG
18#include <XSUB.h>
19
5dff01bc 20#ifndef NETWARE
e8189732 21#if 0
9e21b3d0
JH
22#define DEBUGME /* Debug mode, turns assertions on as well */
23#define DASSERT /* Assertion mode */
24#endif
5dff01bc
JH
25#else /* NETWARE */
26#if 0 /* On NetWare USE_PERLIO is not used */
27#define DEBUGME /* Debug mode, turns assertions on as well */
28#define DASSERT /* Assertion mode */
29#endif
30#endif
7a6a85bf
RG
31
32/*
33 * Pre PerlIO time when none of USE_PERLIO and PERLIO_IS_STDIO is defined
34 * Provide them with the necessary defines so they can build with pre-5.004.
35 */
36#ifndef USE_PERLIO
37#ifndef PERLIO_IS_STDIO
38#define PerlIO FILE
39#define PerlIO_getc(x) getc(x)
40#define PerlIO_putc(f,x) putc(x,f)
41#define PerlIO_read(x,y,z) fread(y,1,z,x)
42#define PerlIO_write(x,y,z) fwrite(y,1,z,x)
43#define PerlIO_stdoutf printf
44#endif /* PERLIO_IS_STDIO */
45#endif /* USE_PERLIO */
46
47/*
48 * Earlier versions of perl might be used, we can't assume they have the latest!
49 */
f0ffaed8
JH
50
51#ifndef PERL_VERSION /* For perls < 5.6 */
e993d95c 52#define PERL_VERSION PATCHLEVEL
7a6a85bf
RG
53#ifndef newRV_noinc
54#define newRV_noinc(sv) ((Sv = newRV(sv)), --SvREFCNT(SvRV(Sv)), Sv)
55#endif
e993d95c 56#if (PATCHLEVEL <= 4) /* Older perls (<= 5.004) lack PL_ namespace */
7a6a85bf
RG
57#define PL_sv_yes sv_yes
58#define PL_sv_no sv_no
59#define PL_sv_undef sv_undef
e993d95c 60#if (SUBVERSION <= 4) /* 5.004_04 has been reported to lack newSVpvn */
dd19458b 61#define newSVpvn newSVpv
7a6a85bf 62#endif
e993d95c 63#endif /* PATCHLEVEL <= 4 */
7a6a85bf
RG
64#ifndef HvSHAREKEYS_off
65#define HvSHAREKEYS_off(hv) /* Ignore */
66#endif
f0ffaed8
JH
67#ifndef AvFILLp /* Older perls (<=5.003) lack AvFILLp */
68#define AvFILLp AvFILL
69#endif
70typedef double NV; /* Older perls lack the NV type */
cc964657
JH
71#define IVdf "ld" /* Various printf formats for Perl types */
72#define UVuf "lu"
73#define UVof "lo"
74#define UVxf "lx"
75#define INT2PTR(t,v) (t)(IV)(v)
76#define PTR2UV(v) (unsigned long)(v)
f0ffaed8 77#endif /* PERL_VERSION -- perls < 5.6 */
7a6a85bf 78
cc964657 79#ifndef NVef /* The following were not part of perl 5.6 */
9e21b3d0
JH
80#if defined(USE_LONG_DOUBLE) && \
81 defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
82#define NVef PERL_PRIeldbl
83#define NVff PERL_PRIfldbl
84#define NVgf PERL_PRIgldbl
85#else
cc964657
JH
86#define NVef "e"
87#define NVff "f"
88#define NVgf "g"
89#endif
90#endif
91
7a6a85bf 92#ifdef DEBUGME
8be2b38b
JH
93
94#ifndef DASSERT
95#define DASSERT
96#endif
97
90826881
JH
98/*
99 * TRACEME() will only output things when the $Storable::DEBUGME is true.
100 */
101
111e03c1
RG
102#define TRACEME(x) \
103 STMT_START { \
90826881 104 if (SvTRUE(perl_get_sv("Storable::DEBUGME", TRUE))) \
111e03c1
RG
105 { PerlIO_stdoutf x; PerlIO_stdoutf("\n"); } \
106 } STMT_END
7a6a85bf
RG
107#else
108#define TRACEME(x)
8be2b38b 109#endif /* DEBUGME */
7a6a85bf
RG
110
111#ifdef DASSERT
111e03c1
RG
112#define ASSERT(x,y) \
113 STMT_START { \
7a6a85bf
RG
114 if (!(x)) { \
115 PerlIO_stdoutf("ASSERT FAILED (\"%s\", line %d): ", \
116 __FILE__, __LINE__); \
117 PerlIO_stdoutf y; PerlIO_stdoutf("\n"); \
118 } \
111e03c1 119 } STMT_END
7a6a85bf
RG
120#else
121#define ASSERT(x,y)
122#endif
123
124/*
125 * Type markers.
126 */
127
128#define C(x) ((char) (x)) /* For markers with dynamic retrieval handling */
129
130#define SX_OBJECT C(0) /* Already stored object */
dd19458b 131#define SX_LSCALAR C(1) /* Scalar (large binary) follows (length, data) */
7a6a85bf
RG
132#define SX_ARRAY C(2) /* Array forthcominng (size, item list) */
133#define SX_HASH C(3) /* Hash forthcoming (size, key/value pair list) */
134#define SX_REF C(4) /* Reference to object forthcoming */
135#define SX_UNDEF C(5) /* Undefined scalar */
136#define SX_INTEGER C(6) /* Integer forthcoming */
137#define SX_DOUBLE C(7) /* Double forthcoming */
138#define SX_BYTE C(8) /* (signed) byte forthcoming */
139#define SX_NETINT C(9) /* Integer in network order forthcoming */
dd19458b 140#define SX_SCALAR C(10) /* Scalar (binary, small) follows (length, data) */
f062ea6c
PN
141#define SX_TIED_ARRAY C(11) /* Tied array forthcoming */
142#define SX_TIED_HASH C(12) /* Tied hash forthcoming */
143#define SX_TIED_SCALAR C(13) /* Tied scalar forthcoming */
7a6a85bf
RG
144#define SX_SV_UNDEF C(14) /* Perl's immortal PL_sv_undef */
145#define SX_SV_YES C(15) /* Perl's immortal PL_sv_yes */
146#define SX_SV_NO C(16) /* Perl's immortal PL_sv_no */
147#define SX_BLESS C(17) /* Object is blessed */
148#define SX_IX_BLESS C(18) /* Object is blessed, classname given by index */
149#define SX_HOOK C(19) /* Stored via hook, user-defined */
150#define SX_OVERLOAD C(20) /* Overloaded reference */
f062ea6c
PN
151#define SX_TIED_KEY C(21) /* Tied magic key forthcoming */
152#define SX_TIED_IDX C(22) /* Tied magic index forthcoming */
153#define SX_UTF8STR C(23) /* UTF-8 string forthcoming (small) */
154#define SX_LUTF8STR C(24) /* UTF-8 string forthcoming (large) */
155#define SX_FLAG_HASH C(25) /* Hash with flags forthcoming (size, flags, key/flags/value triplet list) */
e16e2ff8 156#define SX_ERROR C(26) /* Error */
7a6a85bf
RG
157
158/*
159 * Those are only used to retrieve "old" pre-0.6 binary images.
160 */
161#define SX_ITEM 'i' /* An array item introducer */
162#define SX_IT_UNDEF 'I' /* Undefined array item */
d1be9408
JF
163#define SX_KEY 'k' /* A hash key introducer */
164#define SX_VALUE 'v' /* A hash value introducer */
7a6a85bf
RG
165#define SX_VL_UNDEF 'V' /* Undefined hash value */
166
167/*
168 * Those are only used to retrieve "old" pre-0.7 binary images
169 */
170
171#define SX_CLASS 'b' /* Object is blessed, class name length <255 */
f062ea6c 172#define SX_LG_CLASS 'B' /* Object is blessed, class name length >255 */
7a6a85bf
RG
173#define SX_STORED 'X' /* End of object */
174
175/*
176 * Limits between short/long length representation.
177 */
178
179#define LG_SCALAR 255 /* Large scalar length limit */
180#define LG_BLESS 127 /* Large classname bless limit */
181
182/*
183 * Operation types
184 */
185
186#define ST_STORE 0x1 /* Store operation */
187#define ST_RETRIEVE 0x2 /* Retrieval operation */
188#define ST_CLONE 0x4 /* Deep cloning operation */
189
190/*
191 * The following structure is used for hash table key retrieval. Since, when
192 * retrieving objects, we'll be facing blessed hash references, it's best
193 * to pre-allocate that buffer once and resize it as the need arises, never
194 * freeing it (keys will be saved away someplace else anyway, so even large
195 * keys are not enough a motivation to reclaim that space).
196 *
197 * This structure is also used for memory store/retrieve operations which
198 * happen in a fixed place before being malloc'ed elsewhere if persistency
199 * is required. Hence the aptr pointer.
200 */
201struct extendable {
202 char *arena; /* Will hold hash key strings, resized as needed */
203 STRLEN asiz; /* Size of aforementionned buffer */
204 char *aptr; /* Arena pointer, for in-place read/write ops */
205 char *aend; /* First invalid address */
206};
207
208/*
209 * At store time:
d1be9408 210 * A hash table records the objects which have already been stored.
7a6a85bf
RG
211 * Those are referred to as SX_OBJECT in the file, and their "tag" (i.e.
212 * an arbitrary sequence number) is used to identify them.
213 *
214 * At retrieve time:
215 * An array table records the objects which have already been retrieved,
216 * as seen by the tag determind by counting the objects themselves. The
217 * reference to that retrieved object is kept in the table, and is returned
218 * when an SX_OBJECT is found bearing that same tag.
219 *
220 * The same processing is used to record "classname" for blessed objects:
221 * indexing by a hash at store time, and via an array at retrieve time.
222 */
223
224typedef unsigned long stag_t; /* Used by pre-0.6 binary format */
225
226/*
227 * The following "thread-safe" related defines were contributed by
228 * Murray Nesbitt <murray@activestate.com> and integrated by RAM, who
229 * only renamed things a little bit to ensure consistency with surrounding
230 * code. -- RAM, 14/09/1999
231 *
232 * The original patch suffered from the fact that the stcxt_t structure
233 * was global. Murray tried to minimize the impact on the code as much as
234 * possible.
235 *
236 * Starting with 0.7, Storable can be re-entrant, via the STORABLE_xxx hooks
237 * on objects. Therefore, the notion of context needs to be generalized,
238 * threading or not.
239 */
240
241#define MY_VERSION "Storable(" XS_VERSION ")"
242
530b72ba
NC
243
244/*
245 * Conditional UTF8 support.
246 *
247 */
248#ifdef SvUTF8_on
249#define STORE_UTF8STR(pv, len) STORE_PV_LEN(pv, len, SX_UTF8STR, SX_LUTF8STR)
250#define HAS_UTF8_SCALARS
251#ifdef HeKUTF8
252#define HAS_UTF8_HASHES
253#define HAS_UTF8_ALL
254#else
255/* 5.6 perl has utf8 scalars but not hashes */
256#endif
257#else
258#define SvUTF8(sv) 0
259#define STORE_UTF8STR(pv, len) CROAK(("panic: storing UTF8 in non-UTF8 perl"))
260#endif
261#ifndef HAS_UTF8_ALL
262#define UTF8_CROAK() CROAK(("Cannot retrieve UTF8 data in non-UTF8 perl"))
263#endif
264
265#ifdef HvPLACEHOLDERS
266#define HAS_RESTRICTED_HASHES
267#else
268#define HVhek_PLACEHOLD 0x200
269#define RESTRICTED_HASH_CROAK() CROAK(("Cannot retrieve restricted hash"))
270#endif
271
272#ifdef HvHASKFLAGS
273#define HAS_HASH_KEY_FLAGS
274#endif
275
dd19458b
JH
276/*
277 * Fields s_tainted and s_dirty are prefixed with s_ because Perl's include
278 * files remap tainted and dirty when threading is enabled. That's bad for
279 * perl to remap such common words. -- RAM, 29/09/00
280 */
281
7a6a85bf
RG
282typedef struct stcxt {
283 int entry; /* flags recursion */
284 int optype; /* type of traversal operation */
e993d95c
JH
285 HV *hseen; /* which objects have been seen, store time */
286 AV *hook_seen; /* which SVs were returned by STORABLE_freeze() */
287 AV *aseen; /* which objects have been seen, retrieve time */
288 HV *hclass; /* which classnames have been seen, store time */
289 AV *aclass; /* which classnames have been seen, retrieve time */
290 HV *hook; /* cache for hook methods per class name */
291 IV tagnum; /* incremented at store time for each seen object */
292 IV classnum; /* incremented at store time for each seen classname */
293 int netorder; /* true if network order used */
294 int s_tainted; /* true if input source is tainted, at retrieve time */
295 int forgive_me; /* whether to be forgiving... */
296 int canonical; /* whether to store hashes sorted by key */
530b72ba
NC
297#ifndef HAS_RESTRICTED_HASHES
298 int derestrict; /* whether to downgrade restrcted hashes */
299#endif
300#ifndef HAS_UTF8_ALL
301 int use_bytes; /* whether to bytes-ify utf8 */
302#endif
e8189732 303 int accept_future_minor; /* croak immediately on future minor versions? */
dd19458b 304 int s_dirty; /* context is dirty due to CROAK() -- can be cleaned */
e993d95c
JH
305 int membuf_ro; /* true means membuf is read-only and msaved is rw */
306 struct extendable keybuf; /* for hash key retrieval */
307 struct extendable membuf; /* for memory store/retrieve operations */
308 struct extendable msaved; /* where potentially valid mbuf is saved */
7a6a85bf
RG
309 PerlIO *fio; /* where I/O are performed, NULL for memory */
310 int ver_major; /* major of version for retrieved object */
311 int ver_minor; /* minor of version for retrieved object */
312 SV *(**retrieve_vtbl)(); /* retrieve dispatch table */
111e03c1
RG
313 SV *prev; /* contexts chained backwards in real recursion */
314 SV *my_sv; /* the blessed scalar who's SvPVX() I am */
7a6a85bf
RG
315} stcxt_t;
316
111e03c1
RG
317#define NEW_STORABLE_CXT_OBJ(cxt) \
318 STMT_START { \
319 SV *self = newSV(sizeof(stcxt_t) - 1); \
320 SV *my_sv = newRV_noinc(self); \
321 sv_bless(my_sv, gv_stashpv("Storable::Cxt", TRUE)); \
322 cxt = (stcxt_t *)SvPVX(self); \
323 Zero(cxt, 1, stcxt_t); \
324 cxt->my_sv = my_sv; \
325 } STMT_END
326
7a6a85bf
RG
327#if defined(MULTIPLICITY) || defined(PERL_OBJECT) || defined(PERL_CAPI)
328
e993d95c 329#if (PATCHLEVEL <= 4) && (SUBVERSION < 68)
7a6a85bf
RG
330#define dSTCXT_SV \
331 SV *perinterp_sv = perl_get_sv(MY_VERSION, FALSE)
332#else /* >= perl5.004_68 */
333#define dSTCXT_SV \
334 SV *perinterp_sv = *hv_fetch(PL_modglobal, \
335 MY_VERSION, sizeof(MY_VERSION)-1, TRUE)
336#endif /* < perl5.004_68 */
337
338#define dSTCXT_PTR(T,name) \
111e03c1 339 T name = ((perinterp_sv && SvIOK(perinterp_sv) && SvIVX(perinterp_sv) \
436c6dd3 340 ? (T)SvPVX(SvRV(INT2PTR(SV*,SvIVX(perinterp_sv)))) : (T) 0))
7a6a85bf
RG
341#define dSTCXT \
342 dSTCXT_SV; \
343 dSTCXT_PTR(stcxt_t *, cxt)
344
111e03c1
RG
345#define INIT_STCXT \
346 dSTCXT; \
347 NEW_STORABLE_CXT_OBJ(cxt); \
348 sv_setiv(perinterp_sv, PTR2IV(cxt->my_sv))
7a6a85bf 349
111e03c1
RG
350#define SET_STCXT(x) \
351 STMT_START { \
7a6a85bf 352 dSTCXT_SV; \
111e03c1
RG
353 sv_setiv(perinterp_sv, PTR2IV(x->my_sv)); \
354 } STMT_END
7a6a85bf
RG
355
356#else /* !MULTIPLICITY && !PERL_OBJECT && !PERL_CAPI */
357
358static stcxt_t Context;
359static stcxt_t *Context_ptr = &Context;
360#define dSTCXT stcxt_t *cxt = Context_ptr
111e03c1
RG
361#define INIT_STCXT \
362 dSTCXT; \
363 NEW_STORABLE_CXT_OBJ(cxt)
364
365#define SET_STCXT(x) Context_ptr = x
7a6a85bf
RG
366
367#endif /* MULTIPLICITY || PERL_OBJECT || PERL_CAPI */
368
369/*
370 * KNOWN BUG:
371 * Croaking implies a memory leak, since we don't use setjmp/longjmp
372 * to catch the exit and free memory used during store or retrieve
373 * operations. This is not too difficult to fix, but I need to understand
374 * how Perl does it, and croaking is exceptional anyway, so I lack the
375 * motivation to do it.
376 *
377 * The current workaround is to mark the context as dirty when croaking,
378 * so that data structures can be freed whenever we renter Storable code
379 * (but only *then*: it's a workaround, not a fix).
380 *
381 * This is also imperfect, because we don't really know how far they trapped
382 * the croak(), and when we were recursing, we won't be able to clean anything
383 * but the topmost context stacked.
384 */
385
111e03c1 386#define CROAK(x) STMT_START { cxt->s_dirty = 1; croak x; } STMT_END
7a6a85bf
RG
387
388/*
389 * End of "thread-safe" related definitions.
390 */
391
392/*
9e21b3d0
JH
393 * LOW_32BITS
394 *
395 * Keep only the low 32 bits of a pointer (used for tags, which are not
396 * really pointers).
397 */
398
399#if PTRSIZE <= 4
400#define LOW_32BITS(x) ((I32) (x))
401#else
402#define LOW_32BITS(x) ((I32) ((unsigned long) (x) & 0xffffffffUL))
403#endif
404
405/*
406 * oI, oS, oC
407 *
408 * Hack for Crays, where sizeof(I32) == 8, and which are big-endians.
409 * Used in the WLEN and RLEN macros.
410 */
411
412#if INTSIZE > 4
413#define oI(x) ((I32 *) ((char *) (x) + 4))
414#define oS(x) ((x) - 4)
415#define oC(x) (x = 0)
416#define CRAY_HACK
417#else
418#define oI(x) (x)
419#define oS(x) (x)
420#define oC(x)
421#endif
422
423/*
7a6a85bf
RG
424 * key buffer handling
425 */
426#define kbuf (cxt->keybuf).arena
427#define ksiz (cxt->keybuf).asiz
111e03c1
RG
428#define KBUFINIT() \
429 STMT_START { \
7a6a85bf
RG
430 if (!kbuf) { \
431 TRACEME(("** allocating kbuf of 128 bytes")); \
432 New(10003, kbuf, 128, char); \
433 ksiz = 128; \
434 } \
111e03c1
RG
435 } STMT_END
436#define KBUFCHK(x) \
437 STMT_START { \
7a6a85bf 438 if (x >= ksiz) { \
e993d95c 439 TRACEME(("** extending kbuf to %d bytes (had %d)", x+1, ksiz)); \
7a6a85bf
RG
440 Renew(kbuf, x+1, char); \
441 ksiz = x+1; \
442 } \
111e03c1 443 } STMT_END
7a6a85bf
RG
444
445/*
446 * memory buffer handling
447 */
448#define mbase (cxt->membuf).arena
449#define msiz (cxt->membuf).asiz
450#define mptr (cxt->membuf).aptr
451#define mend (cxt->membuf).aend
452
453#define MGROW (1 << 13)
454#define MMASK (MGROW - 1)
455
456#define round_mgrow(x) \
457 ((unsigned long) (((unsigned long) (x) + MMASK) & ~MMASK))
458#define trunc_int(x) \
459 ((unsigned long) ((unsigned long) (x) & ~(sizeof(int)-1)))
460#define int_aligned(x) \
461 ((unsigned long) (x) == trunc_int(x))
462
111e03c1
RG
463#define MBUF_INIT(x) \
464 STMT_START { \
7a6a85bf
RG
465 if (!mbase) { \
466 TRACEME(("** allocating mbase of %d bytes", MGROW)); \
467 New(10003, mbase, MGROW, char); \
468 msiz = MGROW; \
469 } \
470 mptr = mbase; \
471 if (x) \
472 mend = mbase + x; \
473 else \
474 mend = mbase + msiz; \
111e03c1 475 } STMT_END
7a6a85bf
RG
476
477#define MBUF_TRUNC(x) mptr = mbase + x
478#define MBUF_SIZE() (mptr - mbase)
479
480/*
e993d95c
JH
481 * MBUF_SAVE_AND_LOAD
482 * MBUF_RESTORE
483 *
484 * Those macros are used in do_retrieve() to save the current memory
485 * buffer into cxt->msaved, before MBUF_LOAD() can be used to retrieve
486 * data from a string.
487 */
111e03c1
RG
488#define MBUF_SAVE_AND_LOAD(in) \
489 STMT_START { \
e993d95c
JH
490 ASSERT(!cxt->membuf_ro, ("mbase not already saved")); \
491 cxt->membuf_ro = 1; \
492 TRACEME(("saving mbuf")); \
493 StructCopy(&cxt->membuf, &cxt->msaved, struct extendable); \
494 MBUF_LOAD(in); \
111e03c1 495 } STMT_END
e993d95c 496
111e03c1
RG
497#define MBUF_RESTORE() \
498 STMT_START { \
e993d95c
JH
499 ASSERT(cxt->membuf_ro, ("mbase is read-only")); \
500 cxt->membuf_ro = 0; \
501 TRACEME(("restoring mbuf")); \
502 StructCopy(&cxt->msaved, &cxt->membuf, struct extendable); \
111e03c1 503 } STMT_END
e993d95c
JH
504
505/*
7a6a85bf
RG
506 * Use SvPOKp(), because SvPOK() fails on tainted scalars.
507 * See store_scalar() for other usage of this workaround.
508 */
111e03c1
RG
509#define MBUF_LOAD(v) \
510 STMT_START { \
e993d95c 511 ASSERT(cxt->membuf_ro, ("mbase is read-only")); \
7a6a85bf
RG
512 if (!SvPOKp(v)) \
513 CROAK(("Not a scalar string")); \
514 mptr = mbase = SvPV(v, msiz); \
515 mend = mbase + msiz; \
111e03c1 516 } STMT_END
7a6a85bf 517
111e03c1
RG
518#define MBUF_XTEND(x) \
519 STMT_START { \
7a6a85bf
RG
520 int nsz = (int) round_mgrow((x)+msiz); \
521 int offset = mptr - mbase; \
e993d95c
JH
522 ASSERT(!cxt->membuf_ro, ("mbase is not read-only")); \
523 TRACEME(("** extending mbase from %d to %d bytes (wants %d new)", \
524 msiz, nsz, (x))); \
7a6a85bf
RG
525 Renew(mbase, nsz, char); \
526 msiz = nsz; \
527 mptr = mbase + offset; \
528 mend = mbase + nsz; \
111e03c1 529 } STMT_END
7a6a85bf 530
111e03c1
RG
531#define MBUF_CHK(x) \
532 STMT_START { \
7a6a85bf
RG
533 if ((mptr + (x)) > mend) \
534 MBUF_XTEND(x); \
111e03c1 535 } STMT_END
7a6a85bf 536
111e03c1
RG
537#define MBUF_GETC(x) \
538 STMT_START { \
7a6a85bf
RG
539 if (mptr < mend) \
540 x = (int) (unsigned char) *mptr++; \
541 else \
542 return (SV *) 0; \
111e03c1 543 } STMT_END
7a6a85bf 544
9e21b3d0 545#ifdef CRAY_HACK
111e03c1
RG
546#define MBUF_GETINT(x) \
547 STMT_START { \
9e21b3d0
JH
548 oC(x); \
549 if ((mptr + 4) <= mend) { \
550 memcpy(oI(&x), mptr, 4); \
551 mptr += 4; \
552 } else \
553 return (SV *) 0; \
111e03c1 554 } STMT_END
9e21b3d0 555#else
111e03c1
RG
556#define MBUF_GETINT(x) \
557 STMT_START { \
7a6a85bf
RG
558 if ((mptr + sizeof(int)) <= mend) { \
559 if (int_aligned(mptr)) \
560 x = *(int *) mptr; \
561 else \
562 memcpy(&x, mptr, sizeof(int)); \
563 mptr += sizeof(int); \
564 } else \
565 return (SV *) 0; \
111e03c1 566 } STMT_END
9e21b3d0 567#endif
7a6a85bf 568
111e03c1
RG
569#define MBUF_READ(x,s) \
570 STMT_START { \
7a6a85bf
RG
571 if ((mptr + (s)) <= mend) { \
572 memcpy(x, mptr, s); \
573 mptr += s; \
574 } else \
575 return (SV *) 0; \
111e03c1 576 } STMT_END
7a6a85bf 577
111e03c1
RG
578#define MBUF_SAFEREAD(x,s,z) \
579 STMT_START { \
7a6a85bf
RG
580 if ((mptr + (s)) <= mend) { \
581 memcpy(x, mptr, s); \
582 mptr += s; \
583 } else { \
584 sv_free(z); \
585 return (SV *) 0; \
586 } \
111e03c1 587 } STMT_END
7a6a85bf 588
111e03c1
RG
589#define MBUF_PUTC(c) \
590 STMT_START { \
7a6a85bf
RG
591 if (mptr < mend) \
592 *mptr++ = (char) c; \
593 else { \
594 MBUF_XTEND(1); \
595 *mptr++ = (char) c; \
596 } \
111e03c1 597 } STMT_END
7a6a85bf 598
9e21b3d0 599#ifdef CRAY_HACK
111e03c1
RG
600#define MBUF_PUTINT(i) \
601 STMT_START { \
9e21b3d0
JH
602 MBUF_CHK(4); \
603 memcpy(mptr, oI(&i), 4); \
604 mptr += 4; \
111e03c1 605 } STMT_END
9e21b3d0 606#else
111e03c1
RG
607#define MBUF_PUTINT(i) \
608 STMT_START { \
7a6a85bf
RG
609 MBUF_CHK(sizeof(int)); \
610 if (int_aligned(mptr)) \
611 *(int *) mptr = i; \
612 else \
613 memcpy(mptr, &i, sizeof(int)); \
614 mptr += sizeof(int); \
111e03c1 615 } STMT_END
9e21b3d0 616#endif
7a6a85bf 617
111e03c1
RG
618#define MBUF_WRITE(x,s) \
619 STMT_START { \
7a6a85bf
RG
620 MBUF_CHK(s); \
621 memcpy(mptr, x, s); \
622 mptr += s; \
111e03c1 623 } STMT_END
7a6a85bf
RG
624
625/*
7a6a85bf
RG
626 * Possible return values for sv_type().
627 */
628
629#define svis_REF 0
630#define svis_SCALAR 1
631#define svis_ARRAY 2
632#define svis_HASH 3
633#define svis_TIED 4
634#define svis_TIED_ITEM 5
635#define svis_OTHER 6
636
637/*
638 * Flags for SX_HOOK.
639 */
640
641#define SHF_TYPE_MASK 0x03
642#define SHF_LARGE_CLASSLEN 0x04
643#define SHF_LARGE_STRLEN 0x08
644#define SHF_LARGE_LISTLEN 0x10
645#define SHF_IDX_CLASSNAME 0x20
646#define SHF_NEED_RECURSE 0x40
647#define SHF_HAS_LIST 0x80
648
649/*
b12202d0 650 * Types for SX_HOOK (last 2 bits in flags).
7a6a85bf
RG
651 */
652
653#define SHT_SCALAR 0
654#define SHT_ARRAY 1
655#define SHT_HASH 2
b12202d0
JH
656#define SHT_EXTRA 3 /* Read extra byte for type */
657
658/*
659 * The following are held in the "extra byte"...
660 */
661
662#define SHT_TSCALAR 4 /* 4 + 0 -- tied scalar */
663#define SHT_TARRAY 5 /* 4 + 1 -- tied array */
664#define SHT_THASH 6 /* 4 + 2 -- tied hash */
7a6a85bf
RG
665
666/*
e16e2ff8
NC
667 * per hash flags for flagged hashes
668 */
669
670#define SHV_RESTRICTED 0x01
671
672/*
673 * per key flags for flagged hashes
674 */
675
676#define SHV_K_UTF8 0x01
677#define SHV_K_WASUTF8 0x02
678#define SHV_K_LOCKED 0x04
679#define SHV_K_ISSV 0x08
680#define SHV_K_PLACEHOLDER 0x10
681
682/*
7a6a85bf
RG
683 * Before 0.6, the magic string was "perl-store" (binary version number 0).
684 *
685 * Since 0.6 introduced many binary incompatibilities, the magic string has
686 * been changed to "pst0" to allow an old image to be properly retrieved by
687 * a newer Storable, but ensure a newer image cannot be retrieved with an
688 * older version.
689 *
690 * At 0.7, objects are given the ability to serialize themselves, and the
691 * set of markers is extended, backward compatibility is not jeopardized,
692 * so the binary version number could have remained unchanged. To correctly
693 * spot errors if a file making use of 0.7-specific extensions is given to
694 * 0.6 for retrieval, the binary version was moved to "2". And I'm introducing
695 * a "minor" version, to better track this kind of evolution from now on.
696 *
697 */
2aeb6432
NC
698static const char old_magicstr[] = "perl-store"; /* Magic number before 0.6 */
699static const char magicstr[] = "pst0"; /* Used as a magic number */
7a6a85bf 700
2aeb6432
NC
701#define MAGICSTR_BYTES 'p','s','t','0'
702#define OLDMAGICSTR_BYTES 'p','e','r','l','-','s','t','o','r','e'
703
704#if BYTEORDER == 0x1234
705#define BYTEORDER_BYTES '1','2','3','4'
706#else
707#if BYTEORDER == 0x12345678
708#define BYTEORDER_BYTES '1','2','3','4','5','6','7','8'
709#else
710#if BYTEORDER == 0x87654321
711#define BYTEORDER_BYTES '8','7','6','5','4','3','2','1'
712#else
713#if BYTEORDER == 0x4321
714#define BYTEORDER_BYTES '4','3','2','1'
715#else
716#error Unknown byteoder. Please append your byteorder to Storable.xs
717#endif
718#endif
719#endif
720#endif
721
722static const char byteorderstr[] = {BYTEORDER_BYTES, 0};
530b72ba 723
e16e2ff8 724#define STORABLE_BIN_MAJOR 2 /* Binary major "version" */
530b72ba
NC
725#define STORABLE_BIN_MINOR 5 /* Binary minor "version" */
726
727/* If we aren't 5.7.3 or later, we won't be writing out files that use the
728 * new flagged hash introdued in 2.5, so put 2.4 in the binary header to
729 * maximise ease of interoperation with older Storables.
730 * Could we write 2.3s if we're on 5.005_03? NWC
731 */
e16e2ff8 732#if (PATCHLEVEL <= 6)
530b72ba 733#define STORABLE_BIN_WRITE_MINOR 4
e16e2ff8
NC
734#else
735/*
736 * As of perl 5.7.3, utf8 hash key is introduced.
737 * So this must change -- dankogai
738*/
530b72ba 739#define STORABLE_BIN_WRITE_MINOR 5
e16e2ff8 740#endif /* (PATCHLEVEL <= 6) */
7a6a85bf
RG
741
742/*
743 * Useful store shortcuts...
744 */
745
111e03c1
RG
746#define PUTMARK(x) \
747 STMT_START { \
7a6a85bf
RG
748 if (!cxt->fio) \
749 MBUF_PUTC(x); \
750 else if (PerlIO_putc(cxt->fio, x) == EOF) \
751 return -1; \
111e03c1 752 } STMT_END
7a6a85bf 753
111e03c1
RG
754#define WRITE_I32(x) \
755 STMT_START { \
9e21b3d0
JH
756 ASSERT(sizeof(x) == sizeof(I32), ("writing an I32")); \
757 if (!cxt->fio) \
758 MBUF_PUTINT(x); \
759 else if (PerlIO_write(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
760 return -1; \
111e03c1 761 } STMT_END
9e21b3d0 762
7a6a85bf 763#ifdef HAS_HTONL
111e03c1
RG
764#define WLEN(x) \
765 STMT_START { \
7a6a85bf
RG
766 if (cxt->netorder) { \
767 int y = (int) htonl(x); \
768 if (!cxt->fio) \
769 MBUF_PUTINT(y); \
9e21b3d0 770 else if (PerlIO_write(cxt->fio,oI(&y),oS(sizeof(y))) != oS(sizeof(y))) \
7a6a85bf
RG
771 return -1; \
772 } else { \
773 if (!cxt->fio) \
774 MBUF_PUTINT(x); \
9e21b3d0 775 else if (PerlIO_write(cxt->fio,oI(&x),oS(sizeof(x))) != oS(sizeof(x))) \
7a6a85bf
RG
776 return -1; \
777 } \
111e03c1 778 } STMT_END
7a6a85bf 779#else
9e21b3d0 780#define WLEN(x) WRITE_I32(x)
7a6a85bf
RG
781#endif
782
111e03c1
RG
783#define WRITE(x,y) \
784 STMT_START { \
7a6a85bf
RG
785 if (!cxt->fio) \
786 MBUF_WRITE(x,y); \
787 else if (PerlIO_write(cxt->fio, x, y) != y) \
788 return -1; \
111e03c1 789 } STMT_END
7a6a85bf 790
111e03c1
RG
791#define STORE_PV_LEN(pv, len, small, large) \
792 STMT_START { \
7a6a85bf
RG
793 if (len <= LG_SCALAR) { \
794 unsigned char clen = (unsigned char) len; \
dd19458b 795 PUTMARK(small); \
7a6a85bf
RG
796 PUTMARK(clen); \
797 if (len) \
798 WRITE(pv, len); \
799 } else { \
dd19458b 800 PUTMARK(large); \
7a6a85bf
RG
801 WLEN(len); \
802 WRITE(pv, len); \
803 } \
111e03c1 804 } STMT_END
7a6a85bf 805
dd19458b
JH
806#define STORE_SCALAR(pv, len) STORE_PV_LEN(pv, len, SX_SCALAR, SX_LSCALAR)
807
808/*
7a6a85bf
RG
809 * Store undef in arrays and hashes without recursing through store().
810 */
111e03c1
RG
811#define STORE_UNDEF() \
812 STMT_START { \
7a6a85bf
RG
813 cxt->tagnum++; \
814 PUTMARK(SX_UNDEF); \
111e03c1 815 } STMT_END
7a6a85bf
RG
816
817/*
818 * Useful retrieve shortcuts...
819 */
820
821#define GETCHAR() \
822 (cxt->fio ? PerlIO_getc(cxt->fio) : (mptr >= mend ? EOF : (int) *mptr++))
823
111e03c1
RG
824#define GETMARK(x) \
825 STMT_START { \
7a6a85bf
RG
826 if (!cxt->fio) \
827 MBUF_GETC(x); \
76df4757 828 else if ((int) (x = PerlIO_getc(cxt->fio)) == EOF) \
7a6a85bf 829 return (SV *) 0; \
111e03c1 830 } STMT_END
7a6a85bf 831
111e03c1
RG
832#define READ_I32(x) \
833 STMT_START { \
9e21b3d0
JH
834 ASSERT(sizeof(x) == sizeof(I32), ("reading an I32")); \
835 oC(x); \
7a6a85bf
RG
836 if (!cxt->fio) \
837 MBUF_GETINT(x); \
9e21b3d0 838 else if (PerlIO_read(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
7a6a85bf 839 return (SV *) 0; \
111e03c1 840 } STMT_END
9e21b3d0
JH
841
842#ifdef HAS_NTOHL
111e03c1
RG
843#define RLEN(x) \
844 STMT_START { \
9e21b3d0 845 oC(x); \
7a6a85bf
RG
846 if (!cxt->fio) \
847 MBUF_GETINT(x); \
9e21b3d0 848 else if (PerlIO_read(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
7a6a85bf 849 return (SV *) 0; \
9e21b3d0
JH
850 if (cxt->netorder) \
851 x = (int) ntohl(x); \
111e03c1 852 } STMT_END
9e21b3d0
JH
853#else
854#define RLEN(x) READ_I32(x)
7a6a85bf
RG
855#endif
856
111e03c1
RG
857#define READ(x,y) \
858 STMT_START { \
7a6a85bf
RG
859 if (!cxt->fio) \
860 MBUF_READ(x, y); \
861 else if (PerlIO_read(cxt->fio, x, y) != y) \
862 return (SV *) 0; \
111e03c1 863 } STMT_END
7a6a85bf 864
111e03c1
RG
865#define SAFEREAD(x,y,z) \
866 STMT_START { \
7a6a85bf
RG
867 if (!cxt->fio) \
868 MBUF_SAFEREAD(x,y,z); \
869 else if (PerlIO_read(cxt->fio, x, y) != y) { \
870 sv_free(z); \
871 return (SV *) 0; \
872 } \
111e03c1 873 } STMT_END
7a6a85bf
RG
874
875/*
876 * This macro is used at retrieve time, to remember where object 'y', bearing a
877 * given tag 'tagnum', has been retrieved. Next time we see an SX_OBJECT marker,
878 * we'll therefore know where it has been retrieved and will be able to
879 * share the same reference, as in the original stored memory image.
b12202d0
JH
880 *
881 * We also need to bless objects ASAP for hooks (which may compute "ref $x"
882 * on the objects given to STORABLE_thaw and expect that to be defined), and
883 * also for overloaded objects (for which we might not find the stash if the
884 * object is not blessed yet--this might occur for overloaded objects that
885 * refer to themselves indirectly: if we blessed upon return from a sub
886 * retrieve(), the SX_OBJECT marker we'd found could not have overloading
887 * restored on it because the underlying object would not be blessed yet!).
888 *
889 * To achieve that, the class name of the last retrieved object is passed down
890 * recursively, and the first SEEN() call for which the class name is not NULL
891 * will bless the object.
7a6a85bf 892 */
111e03c1
RG
893#define SEEN(y,c) \
894 STMT_START { \
7a6a85bf
RG
895 if (!y) \
896 return (SV *) 0; \
897 if (av_store(cxt->aseen, cxt->tagnum++, SvREFCNT_inc(y)) == 0) \
898 return (SV *) 0; \
43d061fe 899 TRACEME(("aseen(#%d) = 0x%"UVxf" (refcnt=%d)", cxt->tagnum-1, \
b12202d0
JH
900 PTR2UV(y), SvREFCNT(y)-1)); \
901 if (c) \
902 BLESS((SV *) (y), c); \
111e03c1 903 } STMT_END
7a6a85bf
RG
904
905/*
906 * Bless `s' in `p', via a temporary reference, required by sv_bless().
907 */
111e03c1
RG
908#define BLESS(s,p) \
909 STMT_START { \
7a6a85bf
RG
910 SV *ref; \
911 HV *stash; \
43d061fe 912 TRACEME(("blessing 0x%"UVxf" in %s", PTR2UV(s), (p))); \
7a6a85bf
RG
913 stash = gv_stashpv((p), TRUE); \
914 ref = newRV_noinc(s); \
915 (void) sv_bless(ref, stash); \
916 SvRV(ref) = 0; \
917 SvREFCNT_dec(ref); \
111e03c1 918 } STMT_END
7a6a85bf
RG
919
920static int store();
b12202d0 921static SV *retrieve(stcxt_t *cxt, char *cname);
7a6a85bf
RG
922
923/*
924 * Dynamic dispatching table for SV store.
925 */
926
927static int store_ref(stcxt_t *cxt, SV *sv);
928static int store_scalar(stcxt_t *cxt, SV *sv);
929static int store_array(stcxt_t *cxt, AV *av);
930static int store_hash(stcxt_t *cxt, HV *hv);
931static int store_tied(stcxt_t *cxt, SV *sv);
932static int store_tied_item(stcxt_t *cxt, SV *sv);
933static int store_other(stcxt_t *cxt, SV *sv);
f0ffaed8 934static int store_blessed(stcxt_t *cxt, SV *sv, int type, HV *pkg);
7a6a85bf 935
862382c7
JH
936static int (*sv_store[])(stcxt_t *cxt, SV *sv) = {
937 store_ref, /* svis_REF */
938 store_scalar, /* svis_SCALAR */
939 (int (*)(stcxt_t *cxt, SV *sv)) store_array, /* svis_ARRAY */
940 (int (*)(stcxt_t *cxt, SV *sv)) store_hash, /* svis_HASH */
941 store_tied, /* svis_TIED */
942 store_tied_item, /* svis_TIED_ITEM */
943 store_other, /* svis_OTHER */
7a6a85bf
RG
944};
945
946#define SV_STORE(x) (*sv_store[x])
947
948/*
949 * Dynamic dispatching tables for SV retrieval.
950 */
951
b12202d0
JH
952static SV *retrieve_lscalar(stcxt_t *cxt, char *cname);
953static SV *retrieve_lutf8str(stcxt_t *cxt, char *cname);
954static SV *old_retrieve_array(stcxt_t *cxt, char *cname);
955static SV *old_retrieve_hash(stcxt_t *cxt, char *cname);
956static SV *retrieve_ref(stcxt_t *cxt, char *cname);
957static SV *retrieve_undef(stcxt_t *cxt, char *cname);
958static SV *retrieve_integer(stcxt_t *cxt, char *cname);
959static SV *retrieve_double(stcxt_t *cxt, char *cname);
960static SV *retrieve_byte(stcxt_t *cxt, char *cname);
961static SV *retrieve_netint(stcxt_t *cxt, char *cname);
962static SV *retrieve_scalar(stcxt_t *cxt, char *cname);
963static SV *retrieve_utf8str(stcxt_t *cxt, char *cname);
964static SV *retrieve_tied_array(stcxt_t *cxt, char *cname);
965static SV *retrieve_tied_hash(stcxt_t *cxt, char *cname);
966static SV *retrieve_tied_scalar(stcxt_t *cxt, char *cname);
967static SV *retrieve_other(stcxt_t *cxt, char *cname);
968
969static SV *(*sv_old_retrieve[])(stcxt_t *cxt, char *cname) = {
7a6a85bf
RG
970 0, /* SX_OBJECT -- entry unused dynamically */
971 retrieve_lscalar, /* SX_LSCALAR */
972 old_retrieve_array, /* SX_ARRAY -- for pre-0.6 binaries */
973 old_retrieve_hash, /* SX_HASH -- for pre-0.6 binaries */
974 retrieve_ref, /* SX_REF */
975 retrieve_undef, /* SX_UNDEF */
976 retrieve_integer, /* SX_INTEGER */
977 retrieve_double, /* SX_DOUBLE */
978 retrieve_byte, /* SX_BYTE */
979 retrieve_netint, /* SX_NETINT */
980 retrieve_scalar, /* SX_SCALAR */
981 retrieve_tied_array, /* SX_ARRAY */
982 retrieve_tied_hash, /* SX_HASH */
983 retrieve_tied_scalar, /* SX_SCALAR */
984 retrieve_other, /* SX_SV_UNDEF not supported */
985 retrieve_other, /* SX_SV_YES not supported */
986 retrieve_other, /* SX_SV_NO not supported */
987 retrieve_other, /* SX_BLESS not supported */
988 retrieve_other, /* SX_IX_BLESS not supported */
989 retrieve_other, /* SX_HOOK not supported */
990 retrieve_other, /* SX_OVERLOADED not supported */
991 retrieve_other, /* SX_TIED_KEY not supported */
992 retrieve_other, /* SX_TIED_IDX not supported */
dd19458b
JH
993 retrieve_other, /* SX_UTF8STR not supported */
994 retrieve_other, /* SX_LUTF8STR not supported */
e16e2ff8 995 retrieve_other, /* SX_FLAG_HASH not supported */
7a6a85bf
RG
996 retrieve_other, /* SX_ERROR */
997};
998
b12202d0
JH
999static SV *retrieve_array(stcxt_t *cxt, char *cname);
1000static SV *retrieve_hash(stcxt_t *cxt, char *cname);
1001static SV *retrieve_sv_undef(stcxt_t *cxt, char *cname);
1002static SV *retrieve_sv_yes(stcxt_t *cxt, char *cname);
1003static SV *retrieve_sv_no(stcxt_t *cxt, char *cname);
1004static SV *retrieve_blessed(stcxt_t *cxt, char *cname);
1005static SV *retrieve_idx_blessed(stcxt_t *cxt, char *cname);
1006static SV *retrieve_hook(stcxt_t *cxt, char *cname);
1007static SV *retrieve_overloaded(stcxt_t *cxt, char *cname);
1008static SV *retrieve_tied_key(stcxt_t *cxt, char *cname);
1009static SV *retrieve_tied_idx(stcxt_t *cxt, char *cname);
e16e2ff8 1010static SV *retrieve_flag_hash(stcxt_t *cxt, char *cname);
b12202d0
JH
1011
1012static SV *(*sv_retrieve[])(stcxt_t *cxt, char *cname) = {
7a6a85bf
RG
1013 0, /* SX_OBJECT -- entry unused dynamically */
1014 retrieve_lscalar, /* SX_LSCALAR */
1015 retrieve_array, /* SX_ARRAY */
1016 retrieve_hash, /* SX_HASH */
1017 retrieve_ref, /* SX_REF */
1018 retrieve_undef, /* SX_UNDEF */
1019 retrieve_integer, /* SX_INTEGER */
1020 retrieve_double, /* SX_DOUBLE */
1021 retrieve_byte, /* SX_BYTE */
1022 retrieve_netint, /* SX_NETINT */
1023 retrieve_scalar, /* SX_SCALAR */
1024 retrieve_tied_array, /* SX_ARRAY */
1025 retrieve_tied_hash, /* SX_HASH */
1026 retrieve_tied_scalar, /* SX_SCALAR */
1027 retrieve_sv_undef, /* SX_SV_UNDEF */
1028 retrieve_sv_yes, /* SX_SV_YES */
1029 retrieve_sv_no, /* SX_SV_NO */
1030 retrieve_blessed, /* SX_BLESS */
1031 retrieve_idx_blessed, /* SX_IX_BLESS */
1032 retrieve_hook, /* SX_HOOK */
1033 retrieve_overloaded, /* SX_OVERLOAD */
1034 retrieve_tied_key, /* SX_TIED_KEY */
1035 retrieve_tied_idx, /* SX_TIED_IDX */
dd19458b
JH
1036 retrieve_utf8str, /* SX_UTF8STR */
1037 retrieve_lutf8str, /* SX_LUTF8STR */
e16e2ff8 1038 retrieve_flag_hash, /* SX_HASH */
7a6a85bf
RG
1039 retrieve_other, /* SX_ERROR */
1040};
1041
1042#define RETRIEVE(c,x) (*(c)->retrieve_vtbl[(x) >= SX_ERROR ? SX_ERROR : (x)])
1043
f0ffaed8 1044static SV *mbuf2sv(void);
7a6a85bf
RG
1045
1046/***
1047 *** Context management.
1048 ***/
1049
1050/*
1051 * init_perinterp
1052 *
1053 * Called once per "thread" (interpreter) to initialize some global context.
1054 */
f0ffaed8
JH
1055static void init_perinterp(void)
1056{
7a6a85bf
RG
1057 INIT_STCXT;
1058
1059 cxt->netorder = 0; /* true if network order used */
1060 cxt->forgive_me = -1; /* whether to be forgiving... */
1061}
1062
1063/*
e993d95c
JH
1064 * reset_context
1065 *
1066 * Called at the end of every context cleaning, to perform common reset
1067 * operations.
1068 */
1069static void reset_context(stcxt_t *cxt)
1070{
1071 cxt->entry = 0;
1072 cxt->s_dirty = 0;
1073 cxt->optype &= ~(ST_STORE|ST_RETRIEVE); /* Leave ST_CLONE alone */
1074}
1075
1076/*
7a6a85bf
RG
1077 * init_store_context
1078 *
1079 * Initialize a new store context for real recursion.
1080 */
f0ffaed8
JH
1081static void init_store_context(
1082 stcxt_t *cxt,
1083 PerlIO *f,
1084 int optype,
1085 int network_order)
7a6a85bf
RG
1086{
1087 TRACEME(("init_store_context"));
1088
1089 cxt->netorder = network_order;
1090 cxt->forgive_me = -1; /* Fetched from perl if needed */
1091 cxt->canonical = -1; /* Idem */
1092 cxt->tagnum = -1; /* Reset tag numbers */
1093 cxt->classnum = -1; /* Reset class numbers */
1094 cxt->fio = f; /* Where I/O are performed */
1095 cxt->optype = optype; /* A store, or a deep clone */
1096 cxt->entry = 1; /* No recursion yet */
1097
1098 /*
1099 * The `hseen' table is used to keep track of each SV stored and their
1100 * associated tag numbers is special. It is "abused" because the
1101 * values stored are not real SV, just integers cast to (SV *),
1102 * which explains the freeing below.
1103 *
1104 * It is also one possible bottlneck to achieve good storing speed,
1105 * so the "shared keys" optimization is turned off (unlikely to be
1106 * of any use here), and the hash table is "pre-extended". Together,
1107 * those optimizations increase the throughput by 12%.
1108 */
1109
1110 cxt->hseen = newHV(); /* Table where seen objects are stored */
1111 HvSHAREKEYS_off(cxt->hseen);
1112
1113 /*
1114 * The following does not work well with perl5.004_04, and causes
1115 * a core dump later on, in a completely unrelated spot, which
1116 * makes me think there is a memory corruption going on.
1117 *
1118 * Calling hv_ksplit(hseen, HBUCKETS) instead of manually hacking
1119 * it below does not make any difference. It seems to work fine
1120 * with perl5.004_68 but given the probable nature of the bug,
1121 * that does not prove anything.
1122 *
1123 * It's a shame because increasing the amount of buckets raises
1124 * store() throughput by 5%, but until I figure this out, I can't
1125 * allow for this to go into production.
1126 *
1127 * It is reported fixed in 5.005, hence the #if.
1128 */
f0ffaed8 1129#if PERL_VERSION >= 5
7a6a85bf
RG
1130#define HBUCKETS 4096 /* Buckets for %hseen */
1131 HvMAX(cxt->hseen) = HBUCKETS - 1; /* keys %hseen = $HBUCKETS; */
1132#endif
1133
1134 /*
1135 * The `hclass' hash uses the same settings as `hseen' above, but it is
1136 * used to assign sequential tags (numbers) to class names for blessed
1137 * objects.
1138 *
1139 * We turn the shared key optimization on.
1140 */
1141
1142 cxt->hclass = newHV(); /* Where seen classnames are stored */
1143
f0ffaed8 1144#if PERL_VERSION >= 5
7a6a85bf
RG
1145 HvMAX(cxt->hclass) = HBUCKETS - 1; /* keys %hclass = $HBUCKETS; */
1146#endif
1147
1148 /*
1149 * The `hook' hash table is used to keep track of the references on
1150 * the STORABLE_freeze hook routines, when found in some class name.
1151 *
1152 * It is assumed that the inheritance tree will not be changed during
1153 * storing, and that no new method will be dynamically created by the
1154 * hooks.
1155 */
1156
1157 cxt->hook = newHV(); /* Table where hooks are cached */
90826881
JH
1158
1159 /*
1160 * The `hook_seen' array keeps track of all the SVs returned by
1161 * STORABLE_freeze hooks for us to serialize, so that they are not
1162 * reclaimed until the end of the serialization process. Each SV is
1163 * only stored once, the first time it is seen.
1164 */
1165
1166 cxt->hook_seen = newAV(); /* Lists SVs returned by STORABLE_freeze */
7a6a85bf
RG
1167}
1168
1169/*
1170 * clean_store_context
1171 *
1172 * Clean store context by
1173 */
f0ffaed8 1174static void clean_store_context(stcxt_t *cxt)
7a6a85bf
RG
1175{
1176 HE *he;
1177
1178 TRACEME(("clean_store_context"));
1179
1180 ASSERT(cxt->optype & ST_STORE, ("was performing a store()"));
1181
1182 /*
1183 * Insert real values into hashes where we stored faked pointers.
1184 */
1185
e993d95c
JH
1186 if (cxt->hseen) {
1187 hv_iterinit(cxt->hseen);
1188 while ((he = hv_iternext(cxt->hseen))) /* Extra () for -Wall, grr.. */
1189 HeVAL(he) = &PL_sv_undef;
1190 }
7a6a85bf 1191
e993d95c
JH
1192 if (cxt->hclass) {
1193 hv_iterinit(cxt->hclass);
1194 while ((he = hv_iternext(cxt->hclass))) /* Extra () for -Wall, grr.. */
1195 HeVAL(he) = &PL_sv_undef;
1196 }
7a6a85bf
RG
1197
1198 /*
1199 * And now dispose of them...
862382c7
JH
1200 *
1201 * The surrounding if() protection has been added because there might be
1202 * some cases where this routine is called more than once, during
1203 * exceptionnal events. This was reported by Marc Lehmann when Storable
1204 * is executed from mod_perl, and the fix was suggested by him.
1205 * -- RAM, 20/12/2000
1206 */
1207
1208 if (cxt->hseen) {
1209 HV *hseen = cxt->hseen;
1210 cxt->hseen = 0;
1211 hv_undef(hseen);
1212 sv_free((SV *) hseen);
1213 }
7a6a85bf 1214
862382c7
JH
1215 if (cxt->hclass) {
1216 HV *hclass = cxt->hclass;
1217 cxt->hclass = 0;
1218 hv_undef(hclass);
1219 sv_free((SV *) hclass);
1220 }
7a6a85bf 1221
862382c7
JH
1222 if (cxt->hook) {
1223 HV *hook = cxt->hook;
1224 cxt->hook = 0;
1225 hv_undef(hook);
1226 sv_free((SV *) hook);
1227 }
7a6a85bf 1228
862382c7
JH
1229 if (cxt->hook_seen) {
1230 AV *hook_seen = cxt->hook_seen;
1231 cxt->hook_seen = 0;
1232 av_undef(hook_seen);
1233 sv_free((SV *) hook_seen);
1234 }
90826881 1235
e8189732
NC
1236 cxt->forgive_me = -1; /* Fetched from perl if needed */
1237 cxt->canonical = -1; /* Idem */
1238
e993d95c 1239 reset_context(cxt);
7a6a85bf
RG
1240}
1241
1242/*
1243 * init_retrieve_context
1244 *
1245 * Initialize a new retrieve context for real recursion.
1246 */
dd19458b 1247static void init_retrieve_context(stcxt_t *cxt, int optype, int is_tainted)
7a6a85bf
RG
1248{
1249 TRACEME(("init_retrieve_context"));
1250
1251 /*
1252 * The hook hash table is used to keep track of the references on
1253 * the STORABLE_thaw hook routines, when found in some class name.
1254 *
1255 * It is assumed that the inheritance tree will not be changed during
1256 * storing, and that no new method will be dynamically created by the
1257 * hooks.
1258 */
1259
1260 cxt->hook = newHV(); /* Caches STORABLE_thaw */
1261
1262 /*
1263 * If retrieving an old binary version, the cxt->retrieve_vtbl variable
1264 * was set to sv_old_retrieve. We'll need a hash table to keep track of
1265 * the correspondance between the tags and the tag number used by the
1266 * new retrieve routines.
1267 */
1268
e16e2ff8 1269 cxt->hseen = ((cxt->retrieve_vtbl == sv_old_retrieve) ? newHV() : 0);
7a6a85bf
RG
1270
1271 cxt->aseen = newAV(); /* Where retrieved objects are kept */
1272 cxt->aclass = newAV(); /* Where seen classnames are kept */
1273 cxt->tagnum = 0; /* Have to count objects... */
1274 cxt->classnum = 0; /* ...and class names as well */
1275 cxt->optype = optype;
dd19458b 1276 cxt->s_tainted = is_tainted;
7a6a85bf 1277 cxt->entry = 1; /* No recursion yet */
530b72ba
NC
1278#ifndef HAS_RESTRICTED_HASHES
1279 cxt->derestrict = -1; /* Fetched from perl if needed */
1280#endif
1281#ifndef HAS_UTF8_ALL
1282 cxt->use_bytes = -1; /* Fetched from perl if needed */
1283#endif
e8189732 1284 cxt->accept_future_minor = -1; /* Fetched from perl if needed */
7a6a85bf
RG
1285}
1286
1287/*
1288 * clean_retrieve_context
1289 *
1290 * Clean retrieve context by
1291 */
dd19458b 1292static void clean_retrieve_context(stcxt_t *cxt)
7a6a85bf
RG
1293{
1294 TRACEME(("clean_retrieve_context"));
1295
1296 ASSERT(cxt->optype & ST_RETRIEVE, ("was performing a retrieve()"));
1297
862382c7
JH
1298 if (cxt->aseen) {
1299 AV *aseen = cxt->aseen;
1300 cxt->aseen = 0;
1301 av_undef(aseen);
1302 sv_free((SV *) aseen);
1303 }
7a6a85bf 1304
862382c7
JH
1305 if (cxt->aclass) {
1306 AV *aclass = cxt->aclass;
1307 cxt->aclass = 0;
1308 av_undef(aclass);
1309 sv_free((SV *) aclass);
1310 }
7a6a85bf 1311
862382c7
JH
1312 if (cxt->hook) {
1313 HV *hook = cxt->hook;
1314 cxt->hook = 0;
1315 hv_undef(hook);
1316 sv_free((SV *) hook);
1317 }
7a6a85bf 1318
862382c7
JH
1319 if (cxt->hseen) {
1320 HV *hseen = cxt->hseen;
1321 cxt->hseen = 0;
1322 hv_undef(hseen);
1323 sv_free((SV *) hseen); /* optional HV, for backward compat. */
1324 }
7a6a85bf 1325
e8189732
NC
1326#ifndef HAS_RESTRICTED_HASHES
1327 cxt->derestrict = -1; /* Fetched from perl if needed */
1328#endif
1329#ifndef HAS_UTF8_ALL
1330 cxt->use_bytes = -1; /* Fetched from perl if needed */
1331#endif
1332 cxt->accept_future_minor = -1; /* Fetched from perl if needed */
1333
e993d95c 1334 reset_context(cxt);
7a6a85bf
RG
1335}
1336
1337/*
1338 * clean_context
1339 *
1340 * A workaround for the CROAK bug: cleanup the last context.
1341 */
e993d95c 1342static void clean_context(stcxt_t *cxt)
7a6a85bf
RG
1343{
1344 TRACEME(("clean_context"));
1345
dd19458b 1346 ASSERT(cxt->s_dirty, ("dirty context"));
7a6a85bf 1347
e993d95c
JH
1348 if (cxt->membuf_ro)
1349 MBUF_RESTORE();
1350
1351 ASSERT(!cxt->membuf_ro, ("mbase is not read-only"));
1352
7a6a85bf
RG
1353 if (cxt->optype & ST_RETRIEVE)
1354 clean_retrieve_context(cxt);
e993d95c 1355 else if (cxt->optype & ST_STORE)
7a6a85bf 1356 clean_store_context(cxt);
e993d95c
JH
1357 else
1358 reset_context(cxt);
862382c7
JH
1359
1360 ASSERT(!cxt->s_dirty, ("context is clean"));
e993d95c 1361 ASSERT(cxt->entry == 0, ("context is reset"));
7a6a85bf
RG
1362}
1363
1364/*
1365 * allocate_context
1366 *
1367 * Allocate a new context and push it on top of the parent one.
1368 * This new context is made globally visible via SET_STCXT().
1369 */
1370static stcxt_t *allocate_context(parent_cxt)
1371stcxt_t *parent_cxt;
1372{
1373 stcxt_t *cxt;
1374
1375 TRACEME(("allocate_context"));
1376
dd19458b 1377 ASSERT(!parent_cxt->s_dirty, ("parent context clean"));
7a6a85bf 1378
111e03c1
RG
1379 NEW_STORABLE_CXT_OBJ(cxt);
1380 cxt->prev = parent_cxt->my_sv;
7a6a85bf
RG
1381 SET_STCXT(cxt);
1382
e993d95c
JH
1383 ASSERT(!cxt->s_dirty, ("clean context"));
1384
7a6a85bf
RG
1385 return cxt;
1386}
1387
1388/*
1389 * free_context
1390 *
1391 * Free current context, which cannot be the "root" one.
1392 * Make the context underneath globally visible via SET_STCXT().
1393 */
1394static void free_context(cxt)
1395stcxt_t *cxt;
1396{
111e03c1 1397 stcxt_t *prev = (stcxt_t *)(cxt->prev ? SvPVX(SvRV(cxt->prev)) : 0);
7a6a85bf
RG
1398
1399 TRACEME(("free_context"));
1400
dd19458b 1401 ASSERT(!cxt->s_dirty, ("clean context"));
7a6a85bf
RG
1402 ASSERT(prev, ("not freeing root context"));
1403
111e03c1 1404 SvREFCNT_dec(cxt->my_sv);
7a6a85bf 1405 SET_STCXT(prev);
e993d95c
JH
1406
1407 ASSERT(cxt, ("context not void"));
7a6a85bf
RG
1408}
1409
1410/***
1411 *** Predicates.
1412 ***/
1413
1414/*
1415 * is_storing
1416 *
1417 * Tells whether we're in the middle of a store operation.
1418 */
f0ffaed8 1419int is_storing(void)
7a6a85bf
RG
1420{
1421 dSTCXT;
1422
1423 return cxt->entry && (cxt->optype & ST_STORE);
1424}
1425
1426/*
1427 * is_retrieving
1428 *
1429 * Tells whether we're in the middle of a retrieve operation.
1430 */
f0ffaed8 1431int is_retrieving(void)
7a6a85bf
RG
1432{
1433 dSTCXT;
1434
1435 return cxt->entry && (cxt->optype & ST_RETRIEVE);
1436}
1437
1438/*
1439 * last_op_in_netorder
1440 *
1441 * Returns whether last operation was made using network order.
1442 *
1443 * This is typically out-of-band information that might prove useful
1444 * to people wishing to convert native to network order data when used.
1445 */
f0ffaed8 1446int last_op_in_netorder(void)
7a6a85bf
RG
1447{
1448 dSTCXT;
1449
1450 return cxt->netorder;
1451}
1452
1453/***
1454 *** Hook lookup and calling routines.
1455 ***/
1456
1457/*
1458 * pkg_fetchmeth
1459 *
1460 * A wrapper on gv_fetchmethod_autoload() which caches results.
1461 *
1462 * Returns the routine reference as an SV*, or null if neither the package
1463 * nor its ancestors know about the method.
1464 */
f0ffaed8
JH
1465static SV *pkg_fetchmeth(
1466 HV *cache,
1467 HV *pkg,
1468 char *method)
7a6a85bf
RG
1469{
1470 GV *gv;
1471 SV *sv;
7a6a85bf
RG
1472
1473 /*
1474 * The following code is the same as the one performed by UNIVERSAL::can
1475 * in the Perl core.
1476 */
1477
1478 gv = gv_fetchmethod_autoload(pkg, method, FALSE);
1479 if (gv && isGV(gv)) {
1480 sv = newRV((SV*) GvCV(gv));
9e21b3d0 1481 TRACEME(("%s->%s: 0x%"UVxf, HvNAME(pkg), method, PTR2UV(sv)));
7a6a85bf
RG
1482 } else {
1483 sv = newSVsv(&PL_sv_undef);
1484 TRACEME(("%s->%s: not found", HvNAME(pkg), method));
1485 }
1486
1487 /*
1488 * Cache the result, ignoring failure: if we can't store the value,
1489 * it just won't be cached.
1490 */
1491
1492 (void) hv_store(cache, HvNAME(pkg), strlen(HvNAME(pkg)), sv, 0);
1493
1494 return SvOK(sv) ? sv : (SV *) 0;
1495}
1496
1497/*
1498 * pkg_hide
1499 *
1500 * Force cached value to be undef: hook ignored even if present.
1501 */
f0ffaed8
JH
1502static void pkg_hide(
1503 HV *cache,
1504 HV *pkg,
1505 char *method)
7a6a85bf
RG
1506{
1507 (void) hv_store(cache,
1508 HvNAME(pkg), strlen(HvNAME(pkg)), newSVsv(&PL_sv_undef), 0);
1509}
1510
1511/*
212e9bde
JH
1512 * pkg_uncache
1513 *
1514 * Discard cached value: a whole fetch loop will be retried at next lookup.
1515 */
1516static void pkg_uncache(
1517 HV *cache,
1518 HV *pkg,
1519 char *method)
1520{
1521 (void) hv_delete(cache, HvNAME(pkg), strlen(HvNAME(pkg)), G_DISCARD);
1522}
1523
1524/*
7a6a85bf
RG
1525 * pkg_can
1526 *
1527 * Our own "UNIVERSAL::can", which caches results.
1528 *
1529 * Returns the routine reference as an SV*, or null if the object does not
1530 * know about the method.
1531 */
f0ffaed8
JH
1532static SV *pkg_can(
1533 HV *cache,
1534 HV *pkg,
1535 char *method)
7a6a85bf
RG
1536{
1537 SV **svh;
1538 SV *sv;
1539
1540 TRACEME(("pkg_can for %s->%s", HvNAME(pkg), method));
1541
1542 /*
1543 * Look into the cache to see whether we already have determined
1544 * where the routine was, if any.
1545 *
1546 * NOTA BENE: we don't use `method' at all in our lookup, since we know
1547 * that only one hook (i.e. always the same) is cached in a given cache.
1548 */
1549
1550 svh = hv_fetch(cache, HvNAME(pkg), strlen(HvNAME(pkg)), FALSE);
1551 if (svh) {
1552 sv = *svh;
1553 if (!SvOK(sv)) {
1554 TRACEME(("cached %s->%s: not found", HvNAME(pkg), method));
1555 return (SV *) 0;
1556 } else {
43d061fe 1557 TRACEME(("cached %s->%s: 0x%"UVxf,
9e21b3d0 1558 HvNAME(pkg), method, PTR2UV(sv)));
7a6a85bf
RG
1559 return sv;
1560 }
1561 }
1562
1563 TRACEME(("not cached yet"));
1564 return pkg_fetchmeth(cache, pkg, method); /* Fetch and cache */
1565}
1566
1567/*
1568 * scalar_call
1569 *
1570 * Call routine as obj->hook(av) in scalar context.
1571 * Propagates the single returned value if not called in void context.
1572 */
f0ffaed8
JH
1573static SV *scalar_call(
1574 SV *obj,
1575 SV *hook,
1576 int cloning,
1577 AV *av,
1578 I32 flags)
7a6a85bf
RG
1579{
1580 dSP;
1581 int count;
1582 SV *sv = 0;
1583
1584 TRACEME(("scalar_call (cloning=%d)", cloning));
1585
1586 ENTER;
1587 SAVETMPS;
1588
1589 PUSHMARK(sp);
1590 XPUSHs(obj);
1591 XPUSHs(sv_2mortal(newSViv(cloning))); /* Cloning flag */
1592 if (av) {
1593 SV **ary = AvARRAY(av);
1594 int cnt = AvFILLp(av) + 1;
1595 int i;
1596 XPUSHs(ary[0]); /* Frozen string */
1597 for (i = 1; i < cnt; i++) {
43d061fe
JH
1598 TRACEME(("pushing arg #%d (0x%"UVxf")...",
1599 i, PTR2UV(ary[i])));
7a6a85bf
RG
1600 XPUSHs(sv_2mortal(newRV(ary[i])));
1601 }
1602 }
1603 PUTBACK;
1604
1605 TRACEME(("calling..."));
1606 count = perl_call_sv(hook, flags); /* Go back to Perl code */
1607 TRACEME(("count = %d", count));
1608
1609 SPAGAIN;
1610
1611 if (count) {
1612 sv = POPs;
1613 SvREFCNT_inc(sv); /* We're returning it, must stay alive! */
1614 }
1615
1616 PUTBACK;
1617 FREETMPS;
1618 LEAVE;
1619
1620 return sv;
1621}
1622
1623/*
1624 * array_call
1625 *
f9a1036d 1626 * Call routine obj->hook(cloning) in list context.
7a6a85bf
RG
1627 * Returns the list of returned values in an array.
1628 */
f0ffaed8
JH
1629static AV *array_call(
1630 SV *obj,
1631 SV *hook,
1632 int cloning)
7a6a85bf
RG
1633{
1634 dSP;
1635 int count;
1636 AV *av;
1637 int i;
1638
f0ffaed8 1639 TRACEME(("array_call (cloning=%d)", cloning));
7a6a85bf
RG
1640
1641 ENTER;
1642 SAVETMPS;
1643
1644 PUSHMARK(sp);
1645 XPUSHs(obj); /* Target object */
1646 XPUSHs(sv_2mortal(newSViv(cloning))); /* Cloning flag */
1647 PUTBACK;
1648
1649 count = perl_call_sv(hook, G_ARRAY); /* Go back to Perl code */
1650
1651 SPAGAIN;
1652
1653 av = newAV();
1654 for (i = count - 1; i >= 0; i--) {
1655 SV *sv = POPs;
1656 av_store(av, i, SvREFCNT_inc(sv));
1657 }
1658
1659 PUTBACK;
1660 FREETMPS;
1661 LEAVE;
1662
1663 return av;
1664}
1665
1666/*
1667 * known_class
1668 *
1669 * Lookup the class name in the `hclass' table and either assign it a new ID
1670 * or return the existing one, by filling in `classnum'.
1671 *
1672 * Return true if the class was known, false if the ID was just generated.
1673 */
f0ffaed8
JH
1674static int known_class(
1675 stcxt_t *cxt,
1676 char *name, /* Class name */
1677 int len, /* Name length */
1678 I32 *classnum)
7a6a85bf
RG
1679{
1680 SV **svh;
1681 HV *hclass = cxt->hclass;
1682
1683 TRACEME(("known_class (%s)", name));
1684
1685 /*
1686 * Recall that we don't store pointers in this hash table, but tags.
1687 * Therefore, we need LOW_32BITS() to extract the relevant parts.
1688 */
1689
1690 svh = hv_fetch(hclass, name, len, FALSE);
1691 if (svh) {
1692 *classnum = LOW_32BITS(*svh);
1693 return TRUE;
1694 }
1695
1696 /*
1697 * Unknown classname, we need to record it.
7a6a85bf
RG
1698 */
1699
1700 cxt->classnum++;
3341c981 1701 if (!hv_store(hclass, name, len, INT2PTR(SV*, cxt->classnum), 0))
7a6a85bf
RG
1702 CROAK(("Unable to record new classname"));
1703
1704 *classnum = cxt->classnum;
1705 return FALSE;
1706}
1707
1708/***
1709 *** Sepcific store routines.
1710 ***/
1711
1712/*
1713 * store_ref
1714 *
1715 * Store a reference.
1716 * Layout is SX_REF <object> or SX_OVERLOAD <object>.
1717 */
f0ffaed8 1718static int store_ref(stcxt_t *cxt, SV *sv)
7a6a85bf 1719{
43d061fe 1720 TRACEME(("store_ref (0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
1721
1722 /*
1723 * Follow reference, and check if target is overloaded.
1724 */
1725
1726 sv = SvRV(sv);
1727
1728 if (SvOBJECT(sv)) {
1729 HV *stash = (HV *) SvSTASH(sv);
1730 if (stash && Gv_AMG(stash)) {
9e21b3d0 1731 TRACEME(("ref (0x%"UVxf") is overloaded", PTR2UV(sv)));
7a6a85bf
RG
1732 PUTMARK(SX_OVERLOAD);
1733 } else
1734 PUTMARK(SX_REF);
1735 } else
1736 PUTMARK(SX_REF);
1737
1738 return store(cxt, sv);
1739}
1740
1741/*
1742 * store_scalar
1743 *
1744 * Store a scalar.
1745 *
e16e2ff8 1746 * Layout is SX_LSCALAR <length> <data>, SX_SCALAR <length> <data> or SX_UNDEF.
7a6a85bf
RG
1747 * The <data> section is omitted if <length> is 0.
1748 *
1749 * If integer or double, the layout is SX_INTEGER <data> or SX_DOUBLE <data>.
1750 * Small integers (within [-127, +127]) are stored as SX_BYTE <byte>.
1751 */
f0ffaed8 1752static int store_scalar(stcxt_t *cxt, SV *sv)
7a6a85bf
RG
1753{
1754 IV iv;
1755 char *pv;
1756 STRLEN len;
1757 U32 flags = SvFLAGS(sv); /* "cc -O" may put it in register */
1758
43d061fe 1759 TRACEME(("store_scalar (0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
1760
1761 /*
1762 * For efficiency, break the SV encapsulation by peaking at the flags
1763 * directly without using the Perl macros to avoid dereferencing
1764 * sv->sv_flags each time we wish to check the flags.
1765 */
1766
1767 if (!(flags & SVf_OK)) { /* !SvOK(sv) */
1768 if (sv == &PL_sv_undef) {
1769 TRACEME(("immortal undef"));
1770 PUTMARK(SX_SV_UNDEF);
1771 } else {
86bbd6dc 1772 TRACEME(("undef at 0x%"UVxf, PTR2UV(sv)));
7a6a85bf
RG
1773 PUTMARK(SX_UNDEF);
1774 }
1775 return 0;
1776 }
1777
1778 /*
1779 * Always store the string representation of a scalar if it exists.
1780 * Gisle Aas provided me with this test case, better than a long speach:
1781 *
1782 * perl -MDevel::Peek -le '$a="abc"; $a+0; Dump($a)'
1783 * SV = PVNV(0x80c8520)
1784 * REFCNT = 1
1785 * FLAGS = (NOK,POK,pNOK,pPOK)
1786 * IV = 0
1787 * NV = 0
1788 * PV = 0x80c83d0 "abc"\0
1789 * CUR = 3
1790 * LEN = 4
1791 *
1792 * Write SX_SCALAR, length, followed by the actual data.
1793 *
1794 * Otherwise, write an SX_BYTE, SX_INTEGER or an SX_DOUBLE as
1795 * appropriate, followed by the actual (binary) data. A double
1796 * is written as a string if network order, for portability.
1797 *
1798 * NOTE: instead of using SvNOK(sv), we test for SvNOKp(sv).
1799 * The reason is that when the scalar value is tainted, the SvNOK(sv)
1800 * value is false.
1801 *
1802 * The test for a read-only scalar with both POK and NOK set is meant
1803 * to quickly detect &PL_sv_yes and &PL_sv_no without having to pay the
1804 * address comparison for each scalar we store.
1805 */
1806
1807#define SV_MAYBE_IMMORTAL (SVf_READONLY|SVf_POK|SVf_NOK)
1808
1809 if ((flags & SV_MAYBE_IMMORTAL) == SV_MAYBE_IMMORTAL) {
1810 if (sv == &PL_sv_yes) {
1811 TRACEME(("immortal yes"));
1812 PUTMARK(SX_SV_YES);
1813 } else if (sv == &PL_sv_no) {
1814 TRACEME(("immortal no"));
1815 PUTMARK(SX_SV_NO);
1816 } else {
1817 pv = SvPV(sv, len); /* We know it's SvPOK */
1818 goto string; /* Share code below */
1819 }
db670f21
NC
1820 } else if (flags & SVf_POK) {
1821 /* public string - go direct to string read. */
1822 goto string_readlen;
1823 } else if (
1824#if (PATCHLEVEL <= 6)
1825 /* For 5.6 and earlier NV flag trumps IV flag, so only use integer
1826 direct if NV flag is off. */
1827 (flags & (SVf_NOK | SVf_IOK)) == SVf_IOK
1828#else
1829 /* 5.7 rules are that if IV public flag is set, IV value is as
1830 good, if not better, than NV value. */
1831 flags & SVf_IOK
1832#endif
1833 ) {
1834 iv = SvIV(sv);
1835 /*
1836 * Will come here from below with iv set if double is an integer.
1837 */
1838 integer:
7a6a85bf 1839
db670f21
NC
1840 /* Sorry. This isn't in 5.005_56 (IIRC) or earlier. */
1841#ifdef SVf_IVisUV
1842 /* Need to do this out here, else 0xFFFFFFFF becomes iv of -1
1843 * (for example) and that ends up in the optimised small integer
1844 * case.
1845 */
1846 if ((flags & SVf_IVisUV) && SvUV(sv) > IV_MAX) {
1847 TRACEME(("large unsigned integer as string, value = %"UVuf, SvUV(sv)));
1848 goto string_readlen;
1849 }
1850#endif
1851 /*
1852 * Optimize small integers into a single byte, otherwise store as
1853 * a real integer (converted into network order if they asked).
1854 */
7a6a85bf 1855
db670f21
NC
1856 if (iv >= -128 && iv <= 127) {
1857 unsigned char siv = (unsigned char) (iv + 128); /* [0,255] */
1858 PUTMARK(SX_BYTE);
1859 PUTMARK(siv);
1860 TRACEME(("small integer stored as %d", siv));
1861 } else if (cxt->netorder) {
1862#ifndef HAS_HTONL
1863 TRACEME(("no htonl, fall back to string for integer"));
1864 goto string_readlen;
1865#else
1866 I32 niv;
7a6a85bf 1867
7a6a85bf 1868
db670f21
NC
1869#if IVSIZE > 4
1870 if (
1871#ifdef SVf_IVisUV
1872 /* Sorry. This isn't in 5.005_56 (IIRC) or earlier. */
1873 ((flags & SVf_IVisUV) && SvUV(sv) > 0x7FFFFFFF) ||
1874#endif
1875 (iv > 0x7FFFFFFF) || (iv < -0x80000000)) {
1876 /* Bigger than 32 bits. */
1877 TRACEME(("large network order integer as string, value = %"IVdf, iv));
1878 goto string_readlen;
1879 }
1880#endif
7a6a85bf 1881
db670f21
NC
1882 niv = (I32) htonl((I32) iv);
1883 TRACEME(("using network order"));
1884 PUTMARK(SX_NETINT);
1885 WRITE_I32(niv);
1886#endif
1887 } else {
1888 PUTMARK(SX_INTEGER);
1889 WRITE(&iv, sizeof(iv));
1890 }
1891
1892 TRACEME(("ok (integer 0x%"UVxf", value = %"IVdf")", PTR2UV(sv), iv));
1893 } else if (flags & SVf_NOK) {
1894 NV nv;
1895#if (PATCHLEVEL <= 6)
1896 nv = SvNV(sv);
1897 /*
1898 * Watch for number being an integer in disguise.
1899 */
1900 if (nv == (NV) (iv = I_V(nv))) {
1901 TRACEME(("double %"NVff" is actually integer %"IVdf, nv, iv));
1902 goto integer; /* Share code above */
1903 }
1904#else
7a6a85bf 1905
db670f21
NC
1906 SvIV_please(sv);
1907 if (SvIOK(sv)) {
1908 iv = SvIV(sv);
1909 goto integer; /* Share code above */
1910 }
1911 nv = SvNV(sv);
1912#endif
7a6a85bf 1913
db670f21
NC
1914 if (cxt->netorder) {
1915 TRACEME(("double %"NVff" stored as string", nv));
1916 goto string_readlen; /* Share code below */
1917 }
7a6a85bf 1918
db670f21
NC
1919 PUTMARK(SX_DOUBLE);
1920 WRITE(&nv, sizeof(nv));
7a6a85bf 1921
db670f21 1922 TRACEME(("ok (double 0x%"UVxf", value = %"NVff")", PTR2UV(sv), nv));
7a6a85bf 1923
db670f21
NC
1924 } else if (flags & (SVp_POK | SVp_NOK | SVp_IOK)) {
1925 I32 wlen; /* For 64-bit machines */
7a6a85bf 1926
db670f21
NC
1927 string_readlen:
1928 pv = SvPV(sv, len);
7a6a85bf 1929
db670f21
NC
1930 /*
1931 * Will come here from above if it was readonly, POK and NOK but
1932 * neither &PL_sv_yes nor &PL_sv_no.
1933 */
1934 string:
1935
1936 wlen = (I32) len; /* WLEN via STORE_SCALAR expects I32 */
1937 if (SvUTF8 (sv))
1938 STORE_UTF8STR(pv, wlen);
1939 else
1940 STORE_SCALAR(pv, wlen);
1941 TRACEME(("ok (scalar 0x%"UVxf" '%s', length = %"IVdf")",
1942 PTR2UV(sv), SvPVX(sv), (IV)len));
7a6a85bf 1943 } else
db670f21
NC
1944 CROAK(("Can't determine type of %s(0x%"UVxf")",
1945 sv_reftype(sv, FALSE),
1946 PTR2UV(sv)));
1947 return 0; /* Ok, no recursion on scalars */
7a6a85bf
RG
1948}
1949
1950/*
1951 * store_array
1952 *
1953 * Store an array.
1954 *
1955 * Layout is SX_ARRAY <size> followed by each item, in increading index order.
1956 * Each item is stored as <object>.
1957 */
f0ffaed8 1958static int store_array(stcxt_t *cxt, AV *av)
7a6a85bf
RG
1959{
1960 SV **sav;
1961 I32 len = av_len(av) + 1;
1962 I32 i;
1963 int ret;
1964
43d061fe 1965 TRACEME(("store_array (0x%"UVxf")", PTR2UV(av)));
7a6a85bf
RG
1966
1967 /*
1968 * Signal array by emitting SX_ARRAY, followed by the array length.
1969 */
1970
1971 PUTMARK(SX_ARRAY);
1972 WLEN(len);
1973 TRACEME(("size = %d", len));
1974
1975 /*
1976 * Now store each item recursively.
1977 */
1978
1979 for (i = 0; i < len; i++) {
1980 sav = av_fetch(av, i, 0);
1981 if (!sav) {
1982 TRACEME(("(#%d) undef item", i));
1983 STORE_UNDEF();
1984 continue;
1985 }
1986 TRACEME(("(#%d) item", i));
e993d95c 1987 if ((ret = store(cxt, *sav))) /* Extra () for -Wall, grr... */
7a6a85bf
RG
1988 return ret;
1989 }
1990
1991 TRACEME(("ok (array)"));
1992
1993 return 0;
1994}
1995
1996/*
1997 * sortcmp
1998 *
1999 * Sort two SVs
2000 * Borrowed from perl source file pp_ctl.c, where it is used by pp_sort.
2001 */
2002static int
f0ffaed8 2003sortcmp(const void *a, const void *b)
7a6a85bf
RG
2004{
2005 return sv_cmp(*(SV * const *) a, *(SV * const *) b);
2006}
2007
2008
2009/*
2010 * store_hash
2011 *
d1be9408 2012 * Store a hash table.
7a6a85bf 2013 *
e16e2ff8
NC
2014 * For a "normal" hash (not restricted, no utf8 keys):
2015 *
7a6a85bf
RG
2016 * Layout is SX_HASH <size> followed by each key/value pair, in random order.
2017 * Values are stored as <object>.
2018 * Keys are stored as <length> <data>, the <data> section being omitted
2019 * if length is 0.
c194a0a3
TB
2020 *
2021 * For a "fancy" hash (restricted or utf8 keys):
2022 *
2023 * Layout is SX_FLAG_HASH <size> <hash flags> followed by each key/value pair,
e16e2ff8
NC
2024 * in random order.
2025 * Values are stored as <object>.
2026 * Keys are stored as <flags> <length> <data>, the <data> section being omitted
2027 * if length is 0.
2028 * Currently the only hash flag is "restriced"
2029 * Key flags are as for hv.h
7a6a85bf 2030 */
f0ffaed8 2031static int store_hash(stcxt_t *cxt, HV *hv)
7a6a85bf 2032{
530b72ba
NC
2033 I32 len =
2034#ifdef HAS_RESTRICTED_HASHES
2035 HvTOTALKEYS(hv);
2036#else
2037 HvKEYS(hv);
2038#endif
7a6a85bf
RG
2039 I32 i;
2040 int ret = 0;
2041 I32 riter;
2042 HE *eiter;
530b72ba
NC
2043 int flagged_hash = ((SvREADONLY(hv)
2044#ifdef HAS_HASH_KEY_FLAGS
2045 || HvHASKFLAGS(hv)
2046#endif
2047 ) ? 1 : 0);
e16e2ff8 2048 unsigned char hash_flags = (SvREADONLY(hv) ? SHV_RESTRICTED : 0);
7a6a85bf 2049
e16e2ff8
NC
2050 if (flagged_hash) {
2051 /* needs int cast for C++ compilers, doesn't it? */
2052 TRACEME(("store_hash (0x%"UVxf") (flags %x)", PTR2UV(hv),
2053 (int) hash_flags));
2054 } else {
2055 TRACEME(("store_hash (0x%"UVxf")", PTR2UV(hv)));
2056 }
7a6a85bf
RG
2057
2058 /*
2059 * Signal hash by emitting SX_HASH, followed by the table length.
2060 */
2061
e16e2ff8
NC
2062 if (flagged_hash) {
2063 PUTMARK(SX_FLAG_HASH);
2064 PUTMARK(hash_flags);
2065 } else {
2066 PUTMARK(SX_HASH);
2067 }
7a6a85bf
RG
2068 WLEN(len);
2069 TRACEME(("size = %d", len));
2070
2071 /*
2072 * Save possible iteration state via each() on that table.
2073 */
2074
2075 riter = HvRITER(hv);
2076 eiter = HvEITER(hv);
2077 hv_iterinit(hv);
2078
2079 /*
2080 * Now store each item recursively.
2081 *
2082 * If canonical is defined to some true value then store each
2083 * key/value pair in sorted order otherwise the order is random.
2084 * Canonical order is irrelevant when a deep clone operation is performed.
2085 *
2086 * Fetch the value from perl only once per store() operation, and only
2087 * when needed.
2088 */
2089
2090 if (
2091 !(cxt->optype & ST_CLONE) && (cxt->canonical == 1 ||
2092 (cxt->canonical < 0 && (cxt->canonical =
e16e2ff8 2093 (SvTRUE(perl_get_sv("Storable::canonical", TRUE)) ? 1 : 0))))
7a6a85bf
RG
2094 ) {
2095 /*
2096 * Storing in order, sorted by key.
2097 * Run through the hash, building up an array of keys in a
2098 * mortal array, sort the array and then run through the
2099 * array.
2100 */
2101
2102 AV *av = newAV();
2103
e16e2ff8
NC
2104 /*av_extend (av, len);*/
2105
7a6a85bf
RG
2106 TRACEME(("using canonical order"));
2107
2108 for (i = 0; i < len; i++) {
530b72ba 2109#ifdef HAS_RESTRICTED_HASHES
e16e2ff8 2110 HE *he = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS);
530b72ba
NC
2111#else
2112 HE *he = hv_iternext(hv);
2113#endif
7a6a85bf
RG
2114 SV *key = hv_iterkeysv(he);
2115 av_store(av, AvFILLp(av)+1, key); /* av_push(), really */
2116 }
2117
2118 qsort((char *) AvARRAY(av), len, sizeof(SV *), sortcmp);
2119
2120 for (i = 0; i < len; i++) {
e16e2ff8 2121 unsigned char flags;
7a6a85bf 2122 char *keyval;
e16e2ff8
NC
2123 STRLEN keylen_tmp;
2124 I32 keylen;
7a6a85bf
RG
2125 SV *key = av_shift(av);
2126 HE *he = hv_fetch_ent(hv, key, 0, 0);
2127 SV *val = HeVAL(he);
2128 if (val == 0)
2129 return 1; /* Internal error, not I/O error */
2130
2131 /*
2132 * Store value first.
2133 */
2134
9e21b3d0 2135 TRACEME(("(#%d) value 0x%"UVxf, i, PTR2UV(val)));
7a6a85bf 2136
e993d95c 2137 if ((ret = store(cxt, val))) /* Extra () for -Wall, grr... */
7a6a85bf
RG
2138 goto out;
2139
2140 /*
2141 * Write key string.
2142 * Keys are written after values to make sure retrieval
2143 * can be optimal in terms of memory usage, where keys are
2144 * read into a fixed unique buffer called kbuf.
2145 * See retrieve_hash() for details.
2146 */
2147
e16e2ff8
NC
2148 /* Implementation of restricted hashes isn't nicely
2149 abstracted: */
2150 flags
2151 = (((hash_flags & SHV_RESTRICTED)
2152 && SvREADONLY(val))
2153 ? SHV_K_LOCKED : 0);
2154 if (val == &PL_sv_undef)
2155 flags |= SHV_K_PLACEHOLDER;
2156
2157 keyval = SvPV(key, keylen_tmp);
2158 keylen = keylen_tmp;
530b72ba
NC
2159#ifdef HAS_UTF8_HASHES
2160 /* If you build without optimisation on pre 5.6
2161 then nothing spots that SvUTF8(key) is always 0,
2162 so the block isn't optimised away, at which point
2163 the linker dislikes the reference to
2164 bytes_from_utf8. */
e16e2ff8
NC
2165 if (SvUTF8(key)) {
2166 const char *keysave = keyval;
2167 bool is_utf8 = TRUE;
2168
2169 /* Just casting the &klen to (STRLEN) won't work
2170 well if STRLEN and I32 are of different widths.
2171 --jhi */
2172 keyval = (char*)bytes_from_utf8((U8*)keyval,
2173 &keylen_tmp,
2174 &is_utf8);
2175
2176 /* If we were able to downgrade here, then than
2177 means that we have a key which only had chars
2178 0-255, but was utf8 encoded. */
2179
2180 if (keyval != keysave) {
2181 keylen = keylen_tmp;
2182 flags |= SHV_K_WASUTF8;
2183 } else {
2184 /* keylen_tmp can't have changed, so no need
2185 to assign back to keylen. */
2186 flags |= SHV_K_UTF8;
2187 }
2188 }
530b72ba 2189#endif
e16e2ff8
NC
2190
2191 if (flagged_hash) {
2192 PUTMARK(flags);
2193 TRACEME(("(#%d) key '%s' flags %x %u", i, keyval, flags, *keyval));
2194 } else {
2195 assert (flags == 0);
2196 TRACEME(("(#%d) key '%s'", i, keyval));
2197 }
7a6a85bf
RG
2198 WLEN(keylen);
2199 if (keylen)
2200 WRITE(keyval, keylen);
e16e2ff8
NC
2201 if (flags & SHV_K_WASUTF8)
2202 Safefree (keyval);
7a6a85bf
RG
2203 }
2204
2205 /*
2206 * Free up the temporary array
2207 */
2208
2209 av_undef(av);
2210 sv_free((SV *) av);
2211
2212 } else {
2213
2214 /*
2215 * Storing in "random" order (in the order the keys are stored
2216 * within the the hash). This is the default and will be faster!
2217 */
2218
2219 for (i = 0; i < len; i++) {
2220 char *key;
2221 I32 len;
e16e2ff8 2222 unsigned char flags;
530b72ba 2223#ifdef HV_ITERNEXT_WANTPLACEHOLDERS
e16e2ff8 2224 HE *he = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS);
530b72ba
NC
2225#else
2226 HE *he = hv_iternext(hv);
2227#endif
e16e2ff8
NC
2228 SV *val = (he ? hv_iterval(hv, he) : 0);
2229 SV *key_sv = NULL;
2230 HEK *hek;
7a6a85bf
RG
2231
2232 if (val == 0)
2233 return 1; /* Internal error, not I/O error */
2234
2235 /*
2236 * Store value first.
2237 */
2238
9e21b3d0 2239 TRACEME(("(#%d) value 0x%"UVxf, i, PTR2UV(val)));
7a6a85bf 2240
e993d95c 2241 if ((ret = store(cxt, val))) /* Extra () for -Wall, grr... */
7a6a85bf
RG
2242 goto out;
2243
e16e2ff8
NC
2244 /* Implementation of restricted hashes isn't nicely
2245 abstracted: */
2246 flags
2247 = (((hash_flags & SHV_RESTRICTED)
2248 && SvREADONLY(val))
2249 ? SHV_K_LOCKED : 0);
2250 if (val == &PL_sv_undef)
2251 flags |= SHV_K_PLACEHOLDER;
2252
2253 hek = HeKEY_hek(he);
2254 len = HEK_LEN(hek);
2255 if (len == HEf_SVKEY) {
2256 /* This is somewhat sick, but the internal APIs are
2257 * such that XS code could put one of these in in
2258 * a regular hash.
2259 * Maybe we should be capable of storing one if
2260 * found.
2261 */
2262 key_sv = HeKEY_sv(he);
2263 flags |= SHV_K_ISSV;
2264 } else {
2265 /* Regular string key. */
530b72ba 2266#ifdef HAS_HASH_KEY_FLAGS
e16e2ff8
NC
2267 if (HEK_UTF8(hek))
2268 flags |= SHV_K_UTF8;
2269 if (HEK_WASUTF8(hek))
2270 flags |= SHV_K_WASUTF8;
530b72ba 2271#endif
e16e2ff8
NC
2272 key = HEK_KEY(hek);
2273 }
7a6a85bf
RG
2274 /*
2275 * Write key string.
2276 * Keys are written after values to make sure retrieval
2277 * can be optimal in terms of memory usage, where keys are
2278 * read into a fixed unique buffer called kbuf.
2279 * See retrieve_hash() for details.
2280 */
2281
e16e2ff8
NC
2282 if (flagged_hash) {
2283 PUTMARK(flags);
2284 TRACEME(("(#%d) key '%s' flags %x", i, key, flags));
2285 } else {
2286 assert (flags == 0);
2287 TRACEME(("(#%d) key '%s'", i, key));
2288 }
2289 if (flags & SHV_K_ISSV) {
2290 store(cxt, key_sv);
2291 } else {
2292 WLEN(len);
2293 if (len)
7a6a85bf 2294 WRITE(key, len);
e16e2ff8 2295 }
7a6a85bf
RG
2296 }
2297 }
2298
43d061fe 2299 TRACEME(("ok (hash 0x%"UVxf")", PTR2UV(hv)));
7a6a85bf
RG
2300
2301out:
2302 HvRITER(hv) = riter; /* Restore hash iterator state */
2303 HvEITER(hv) = eiter;
2304
2305 return ret;
2306}
2307
2308/*
2309 * store_tied
2310 *
2311 * When storing a tied object (be it a tied scalar, array or hash), we lay out
2312 * a special mark, followed by the underlying tied object. For instance, when
2313 * dealing with a tied hash, we store SX_TIED_HASH <hash object>, where
2314 * <hash object> stands for the serialization of the tied hash.
2315 */
f0ffaed8 2316static int store_tied(stcxt_t *cxt, SV *sv)
7a6a85bf
RG
2317{
2318 MAGIC *mg;
2319 int ret = 0;
2320 int svt = SvTYPE(sv);
2321 char mtype = 'P';
2322
43d061fe 2323 TRACEME(("store_tied (0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
2324
2325 /*
2326 * We have a small run-time penalty here because we chose to factorise
2327 * all tieds objects into the same routine, and not have a store_tied_hash,
2328 * a store_tied_array, etc...
2329 *
2330 * Don't use a switch() statement, as most compilers don't optimize that
2331 * well for 2/3 values. An if() else if() cascade is just fine. We put
2332 * tied hashes first, as they are the most likely beasts.
2333 */
2334
2335 if (svt == SVt_PVHV) {
2336 TRACEME(("tied hash"));
2337 PUTMARK(SX_TIED_HASH); /* Introduces tied hash */
2338 } else if (svt == SVt_PVAV) {
2339 TRACEME(("tied array"));
2340 PUTMARK(SX_TIED_ARRAY); /* Introduces tied array */
2341 } else {
2342 TRACEME(("tied scalar"));
2343 PUTMARK(SX_TIED_SCALAR); /* Introduces tied scalar */
2344 mtype = 'q';
2345 }
2346
2347 if (!(mg = mg_find(sv, mtype)))
2348 CROAK(("No magic '%c' found while storing tied %s", mtype,
2349 (svt == SVt_PVHV) ? "hash" :
2350 (svt == SVt_PVAV) ? "array" : "scalar"));
2351
2352 /*
2353 * The mg->mg_obj found by mg_find() above actually points to the
2354 * underlying tied Perl object implementation. For instance, if the
2355 * original SV was that of a tied array, then mg->mg_obj is an AV.
2356 *
2357 * Note that we store the Perl object as-is. We don't call its FETCH
2358 * method along the way. At retrieval time, we won't call its STORE
2359 * method either, but the tieing magic will be re-installed. In itself,
2360 * that ensures that the tieing semantics are preserved since futher
2361 * accesses on the retrieved object will indeed call the magic methods...
2362 */
2363
e993d95c 2364 if ((ret = store(cxt, mg->mg_obj))) /* Extra () for -Wall, grr... */
7a6a85bf
RG
2365 return ret;
2366
2367 TRACEME(("ok (tied)"));
2368
2369 return 0;
2370}
2371
2372/*
2373 * store_tied_item
2374 *
2375 * Stores a reference to an item within a tied structure:
2376 *
2377 * . \$h{key}, stores both the (tied %h) object and 'key'.
2378 * . \$a[idx], stores both the (tied @a) object and 'idx'.
2379 *
2380 * Layout is therefore either:
2381 * SX_TIED_KEY <object> <key>
2382 * SX_TIED_IDX <object> <index>
2383 */
f0ffaed8 2384static int store_tied_item(stcxt_t *cxt, SV *sv)
7a6a85bf
RG
2385{
2386 MAGIC *mg;
2387 int ret;
2388
43d061fe 2389 TRACEME(("store_tied_item (0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
2390
2391 if (!(mg = mg_find(sv, 'p')))
2392 CROAK(("No magic 'p' found while storing reference to tied item"));
2393
2394 /*
2395 * We discriminate between \$h{key} and \$a[idx] via mg_ptr.
2396 */
2397
2398 if (mg->mg_ptr) {
2399 TRACEME(("store_tied_item: storing a ref to a tied hash item"));
2400 PUTMARK(SX_TIED_KEY);
9e21b3d0 2401 TRACEME(("store_tied_item: storing OBJ 0x%"UVxf, PTR2UV(mg->mg_obj)));
7a6a85bf 2402
e993d95c 2403 if ((ret = store(cxt, mg->mg_obj))) /* Extra () for -Wall, grr... */
7a6a85bf
RG
2404 return ret;
2405
9e21b3d0 2406 TRACEME(("store_tied_item: storing PTR 0x%"UVxf, PTR2UV(mg->mg_ptr)));
7a6a85bf 2407
e993d95c 2408 if ((ret = store(cxt, (SV *) mg->mg_ptr))) /* Idem, for -Wall */
7a6a85bf
RG
2409 return ret;
2410 } else {
2411 I32 idx = mg->mg_len;
2412
2413 TRACEME(("store_tied_item: storing a ref to a tied array item "));
2414 PUTMARK(SX_TIED_IDX);
9e21b3d0 2415 TRACEME(("store_tied_item: storing OBJ 0x%"UVxf, PTR2UV(mg->mg_obj)));
7a6a85bf 2416
e993d95c 2417 if ((ret = store(cxt, mg->mg_obj))) /* Idem, for -Wall */
7a6a85bf
RG
2418 return ret;
2419
2420 TRACEME(("store_tied_item: storing IDX %d", idx));
2421
2422 WLEN(idx);
2423 }
2424
2425 TRACEME(("ok (tied item)"));
2426
2427 return 0;
2428}
2429
2430/*
2431 * store_hook -- dispatched manually, not via sv_store[]
2432 *
2433 * The blessed SV is serialized by a hook.
2434 *
2435 * Simple Layout is:
2436 *
2437 * SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
2438 *
2439 * where <flags> indicates how long <len>, <len2> and <len3> are, whether
2440 * the trailing part [] is present, the type of object (scalar, array or hash).
2441 * There is also a bit which says how the classname is stored between:
2442 *
2443 * <len> <classname>
2444 * <index>
2445 *
2446 * and when the <index> form is used (classname already seen), the "large
2447 * classname" bit in <flags> indicates how large the <index> is.
2448 *
2449 * The serialized string returned by the hook is of length <len2> and comes
2450 * next. It is an opaque string for us.
2451 *
2452 * Those <len3> object IDs which are listed last represent the extra references
2453 * not directly serialized by the hook, but which are linked to the object.
2454 *
2455 * When recursion is mandated to resolve object-IDs not yet seen, we have
2456 * instead, with <header> being flags with bits set to indicate the object type
2457 * and that recursion was indeed needed:
2458 *
2459 * SX_HOOK <header> <object> <header> <object> <flags>
2460 *
2461 * that same header being repeated between serialized objects obtained through
2462 * recursion, until we reach flags indicating no recursion, at which point
2463 * we know we've resynchronized with a single layout, after <flags>.
b12202d0
JH
2464 *
2465 * When storing a blessed ref to a tied variable, the following format is
2466 * used:
2467 *
2468 * SX_HOOK <flags> <extra> ... [<len3> <object-IDs>] <magic object>
2469 *
2470 * The first <flags> indication carries an object of type SHT_EXTRA, and the
2471 * real object type is held in the <extra> flag. At the very end of the
2472 * serialization stream, the underlying magic object is serialized, just like
2473 * any other tied variable.
7a6a85bf 2474 */
f0ffaed8
JH
2475static int store_hook(
2476 stcxt_t *cxt,
2477 SV *sv,
2478 int type,
2479 HV *pkg,
2480 SV *hook)
7a6a85bf
RG
2481{
2482 I32 len;
2483 char *class;
2484 STRLEN len2;
2485 SV *ref;
2486 AV *av;
2487 SV **ary;
2488 int count; /* really len3 + 1 */
2489 unsigned char flags;
2490 char *pv;
2491 int i;
2492 int recursed = 0; /* counts recursion */
2493 int obj_type; /* object type, on 2 bits */
2494 I32 classnum;
2495 int ret;
2496 int clone = cxt->optype & ST_CLONE;
e993d95c
JH
2497 char mtype = '\0'; /* for blessed ref to tied structures */
2498 unsigned char eflags = '\0'; /* used when object type is SHT_EXTRA */
7a6a85bf
RG
2499
2500 TRACEME(("store_hook, class \"%s\", tagged #%d", HvNAME(pkg), cxt->tagnum));
2501
2502 /*
2503 * Determine object type on 2 bits.
2504 */
2505
2506 switch (type) {
2507 case svis_SCALAR:
2508 obj_type = SHT_SCALAR;
2509 break;
2510 case svis_ARRAY:
2511 obj_type = SHT_ARRAY;
2512 break;
2513 case svis_HASH:
2514 obj_type = SHT_HASH;
2515 break;
b12202d0
JH
2516 case svis_TIED:
2517 /*
2518 * Produced by a blessed ref to a tied data structure, $o in the
2519 * following Perl code.
2520 *
2521 * my %h;
2522 * tie %h, 'FOO';
2523 * my $o = bless \%h, 'BAR';
2524 *
2525 * Signal the tie-ing magic by setting the object type as SHT_EXTRA
2526 * (since we have only 2 bits in <flags> to store the type), and an
2527 * <extra> byte flag will be emitted after the FIRST <flags> in the
2528 * stream, carrying what we put in `eflags'.
2529 */
2530 obj_type = SHT_EXTRA;
2531 switch (SvTYPE(sv)) {
2532 case SVt_PVHV:
2533 eflags = (unsigned char) SHT_THASH;
2534 mtype = 'P';
2535 break;
2536 case SVt_PVAV:
2537 eflags = (unsigned char) SHT_TARRAY;
2538 mtype = 'P';
2539 break;
2540 default:
2541 eflags = (unsigned char) SHT_TSCALAR;
2542 mtype = 'q';
2543 break;
2544 }
2545 break;
7a6a85bf
RG
2546 default:
2547 CROAK(("Unexpected object type (%d) in store_hook()", type));
2548 }
2549 flags = SHF_NEED_RECURSE | obj_type;
2550
2551 class = HvNAME(pkg);
2552 len = strlen(class);
2553
2554 /*
2555 * To call the hook, we need to fake a call like:
2556 *
2557 * $object->STORABLE_freeze($cloning);
2558 *
2559 * but we don't have the $object here. For instance, if $object is
2560 * a blessed array, what we have in `sv' is the array, and we can't
2561 * call a method on those.
2562 *
2563 * Therefore, we need to create a temporary reference to the object and
2564 * make the call on that reference.
2565 */
2566
2567 TRACEME(("about to call STORABLE_freeze on class %s", class));
2568
2569 ref = newRV_noinc(sv); /* Temporary reference */
2570 av = array_call(ref, hook, clone); /* @a = $object->STORABLE_freeze($c) */
2571 SvRV(ref) = 0;
2572 SvREFCNT_dec(ref); /* Reclaim temporary reference */
2573
2574 count = AvFILLp(av) + 1;
2575 TRACEME(("store_hook, array holds %d items", count));
2576
2577 /*
2578 * If they return an empty list, it means they wish to ignore the
2579 * hook for this class (and not just this instance -- that's for them
2580 * to handle if they so wish).
2581 *
2582 * Simply disable the cached entry for the hook (it won't be recomputed
2583 * since it's present in the cache) and recurse to store_blessed().
2584 */
2585
2586 if (!count) {
2587 /*
2588 * They must not change their mind in the middle of a serialization.
2589 */
2590
2591 if (hv_fetch(cxt->hclass, class, len, FALSE))
2592 CROAK(("Too late to ignore hooks for %s class \"%s\"",
2593 (cxt->optype & ST_CLONE) ? "cloning" : "storing", class));
2594
2595 pkg_hide(cxt->hook, pkg, "STORABLE_freeze");
2596
2597 ASSERT(!pkg_can(cxt->hook, pkg, "STORABLE_freeze"), ("hook invisible"));
cc964657 2598 TRACEME(("ignoring STORABLE_freeze in class \"%s\"", class));
7a6a85bf
RG
2599
2600 return store_blessed(cxt, sv, type, pkg);
2601 }
2602
2603 /*
2604 * Get frozen string.
2605 */
2606
2607 ary = AvARRAY(av);
2608 pv = SvPV(ary[0], len2);
2609
2610 /*
7a6a85bf
RG
2611 * If they returned more than one item, we need to serialize some
2612 * extra references if not already done.
2613 *
2614 * Loop over the array, starting at postion #1, and for each item,
2615 * ensure it is a reference, serialize it if not already done, and
2616 * replace the entry with the tag ID of the corresponding serialized
2617 * object.
2618 *
2619 * We CHEAT by not calling av_fetch() and read directly within the
2620 * array, for speed.
2621 */
2622
2623 for (i = 1; i < count; i++) {
2624 SV **svh;
90826881
JH
2625 SV *rsv = ary[i];
2626 SV *xsv;
2627 AV *av_hook = cxt->hook_seen;
7a6a85bf 2628
90826881
JH
2629 if (!SvROK(rsv))
2630 CROAK(("Item #%d returned by STORABLE_freeze "
2631 "for %s is not a reference", i, class));
2632 xsv = SvRV(rsv); /* Follow ref to know what to look for */
7a6a85bf
RG
2633
2634 /*
2635 * Look in hseen and see if we have a tag already.
2636 * Serialize entry if not done already, and get its tag.
2637 */
2638
13689cfe 2639 if ((svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE)))
7a6a85bf
RG
2640 goto sv_seen; /* Avoid moving code too far to the right */
2641
9e21b3d0 2642 TRACEME(("listed object %d at 0x%"UVxf" is unknown", i-1, PTR2UV(xsv)));
7a6a85bf
RG
2643
2644 /*
2645 * We need to recurse to store that object and get it to be known
2646 * so that we can resolve the list of object-IDs at retrieve time.
2647 *
2648 * The first time we do this, we need to emit the proper header
2649 * indicating that we recursed, and what the type of object is (the
2650 * object we're storing via a user-hook). Indeed, during retrieval,
2651 * we'll have to create the object before recursing to retrieve the
2652 * others, in case those would point back at that object.
2653 */
2654
b12202d0
JH
2655 /* [SX_HOOK] <flags> [<extra>] <object>*/
2656 if (!recursed++) {
7a6a85bf 2657 PUTMARK(SX_HOOK);
b12202d0
JH
2658 PUTMARK(flags);
2659 if (obj_type == SHT_EXTRA)
2660 PUTMARK(eflags);
2661 } else
2662 PUTMARK(flags);
7a6a85bf 2663
e993d95c 2664 if ((ret = store(cxt, xsv))) /* Given by hook for us to store */
7a6a85bf
RG
2665 return ret;
2666
2667 svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE);
2668 if (!svh)
2669 CROAK(("Could not serialize item #%d from hook in %s", i, class));
2670
2671 /*
90826881
JH
2672 * It was the first time we serialized `xsv'.
2673 *
2674 * Keep this SV alive until the end of the serialization: if we
2675 * disposed of it right now by decrementing its refcount, and it was
2676 * a temporary value, some next temporary value allocated during
2677 * another STORABLE_freeze might take its place, and we'd wrongly
2678 * assume that new SV was already serialized, based on its presence
2679 * in cxt->hseen.
2680 *
2681 * Therefore, push it away in cxt->hook_seen.
7a6a85bf
RG
2682 */
2683
90826881
JH
2684 av_store(av_hook, AvFILLp(av_hook)+1, SvREFCNT_inc(xsv));
2685
7a6a85bf 2686 sv_seen:
90826881
JH
2687 /*
2688 * Dispose of the REF they returned. If we saved the `xsv' away
2689 * in the array of returned SVs, that will not cause the underlying
2690 * referenced SV to be reclaimed.
2691 */
2692
2693 ASSERT(SvREFCNT(xsv) > 1, ("SV will survive disposal of its REF"));
2694 SvREFCNT_dec(rsv); /* Dispose of reference */
2695
2696 /*
2697 * Replace entry with its tag (not a real SV, so no refcnt increment)
2698 */
2699
7a6a85bf 2700 ary[i] = *svh;
76edffbb 2701 TRACEME(("listed object %d at 0x%"UVxf" is tag #%"UVuf,
d67b2c17 2702 i-1, PTR2UV(xsv), PTR2UV(*svh)));
7a6a85bf
RG
2703 }
2704
2705 /*
dd19458b
JH
2706 * Allocate a class ID if not already done.
2707 *
2708 * This needs to be done after the recursion above, since at retrieval
2709 * time, we'll see the inner objects first. Many thanks to
2710 * Salvador Ortiz Garcia <sog@msg.com.mx> who spot that bug and
2711 * proposed the right fix. -- RAM, 15/09/2000
2712 */
2713
2714 if (!known_class(cxt, class, len, &classnum)) {
2715 TRACEME(("first time we see class %s, ID = %d", class, classnum));
2716 classnum = -1; /* Mark: we must store classname */
2717 } else {
2718 TRACEME(("already seen class %s, ID = %d", class, classnum));
2719 }
2720
2721 /*
7a6a85bf
RG
2722 * Compute leading flags.
2723 */
2724
2725 flags = obj_type;
2726 if (((classnum == -1) ? len : classnum) > LG_SCALAR)
2727 flags |= SHF_LARGE_CLASSLEN;
2728 if (classnum != -1)
2729 flags |= SHF_IDX_CLASSNAME;
2730 if (len2 > LG_SCALAR)
2731 flags |= SHF_LARGE_STRLEN;
2732 if (count > 1)
2733 flags |= SHF_HAS_LIST;
2734 if (count > (LG_SCALAR + 1))
2735 flags |= SHF_LARGE_LISTLEN;
2736
2737 /*
2738 * We're ready to emit either serialized form:
2739 *
2740 * SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
2741 * SX_HOOK <flags> <index> <len2> <str> [<len3> <object-IDs>]
2742 *
2743 * If we recursed, the SX_HOOK has already been emitted.
2744 */
2745
9e21b3d0
JH
2746 TRACEME(("SX_HOOK (recursed=%d) flags=0x%x "
2747 "class=%"IVdf" len=%"IVdf" len2=%"IVdf" len3=%d",
d67b2c17 2748 recursed, flags, (IV)classnum, (IV)len, (IV)len2, count-1));
7a6a85bf 2749
b12202d0
JH
2750 /* SX_HOOK <flags> [<extra>] */
2751 if (!recursed) {
7a6a85bf 2752 PUTMARK(SX_HOOK);
b12202d0
JH
2753 PUTMARK(flags);
2754 if (obj_type == SHT_EXTRA)
2755 PUTMARK(eflags);
2756 } else
2757 PUTMARK(flags);
7a6a85bf
RG
2758
2759 /* <len> <classname> or <index> */
2760 if (flags & SHF_IDX_CLASSNAME) {
2761 if (flags & SHF_LARGE_CLASSLEN)
2762 WLEN(classnum);
2763 else {
2764 unsigned char cnum = (unsigned char) classnum;
2765 PUTMARK(cnum);
2766 }
2767 } else {
2768 if (flags & SHF_LARGE_CLASSLEN)
2769 WLEN(len);
2770 else {
2771 unsigned char clen = (unsigned char) len;
2772 PUTMARK(clen);
2773 }
2774 WRITE(class, len); /* Final \0 is omitted */
2775 }
2776
2777 /* <len2> <frozen-str> */
cc964657
JH
2778 if (flags & SHF_LARGE_STRLEN) {
2779 I32 wlen2 = len2; /* STRLEN might be 8 bytes */
2780 WLEN(wlen2); /* Must write an I32 for 64-bit machines */
2781 } else {
7a6a85bf
RG
2782 unsigned char clen = (unsigned char) len2;
2783 PUTMARK(clen);
2784 }
2785 if (len2)
7c436af3 2786 WRITE(pv, (SSize_t)len2); /* Final \0 is omitted */
7a6a85bf
RG
2787
2788 /* [<len3> <object-IDs>] */
2789 if (flags & SHF_HAS_LIST) {
2790 int len3 = count - 1;
2791 if (flags & SHF_LARGE_LISTLEN)
2792 WLEN(len3);
2793 else {
2794 unsigned char clen = (unsigned char) len3;
2795 PUTMARK(clen);
2796 }
2797
2798 /*
2799 * NOTA BENE, for 64-bit machines: the ary[i] below does not yield a
2800 * real pointer, rather a tag number, well under the 32-bit limit.
2801 */
2802
2803 for (i = 1; i < count; i++) {
2804 I32 tagval = htonl(LOW_32BITS(ary[i]));
9e21b3d0 2805 WRITE_I32(tagval);
7a6a85bf
RG
2806 TRACEME(("object %d, tag #%d", i-1, ntohl(tagval)));
2807 }
2808 }
2809
2810 /*
2811 * Free the array. We need extra care for indices after 0, since they
2812 * don't hold real SVs but integers cast.
2813 */
2814
2815 if (count > 1)
2816 AvFILLp(av) = 0; /* Cheat, nothing after 0 interests us */
2817 av_undef(av);
2818 sv_free((SV *) av);
2819
b12202d0
JH
2820 /*
2821 * If object was tied, need to insert serialization of the magic object.
2822 */
2823
2824 if (obj_type == SHT_EXTRA) {
2825 MAGIC *mg;
2826
2827 if (!(mg = mg_find(sv, mtype))) {
2828 int svt = SvTYPE(sv);
2829 CROAK(("No magic '%c' found while storing ref to tied %s with hook",
2830 mtype, (svt == SVt_PVHV) ? "hash" :
2831 (svt == SVt_PVAV) ? "array" : "scalar"));
2832 }
2833
2834 TRACEME(("handling the magic object 0x%"UVxf" part of 0x%"UVxf,
2835 PTR2UV(mg->mg_obj), PTR2UV(sv)));
2836
2837 /*
2838 * [<magic object>]
2839 */
2840
e993d95c 2841 if ((ret = store(cxt, mg->mg_obj))) /* Extra () for -Wall, grr... */
b12202d0
JH
2842 return ret;
2843 }
2844
7a6a85bf
RG
2845 return 0;
2846}
2847
2848/*
2849 * store_blessed -- dispatched manually, not via sv_store[]
2850 *
2851 * Check whether there is a STORABLE_xxx hook defined in the class or in one
2852 * of its ancestors. If there is, then redispatch to store_hook();
2853 *
2854 * Otherwise, the blessed SV is stored using the following layout:
2855 *
2856 * SX_BLESS <flag> <len> <classname> <object>
2857 *
2858 * where <flag> indicates whether <len> is stored on 0 or 4 bytes, depending
2859 * on the high-order bit in flag: if 1, then length follows on 4 bytes.
2860 * Otherwise, the low order bits give the length, thereby giving a compact
2861 * representation for class names less than 127 chars long.
2862 *
2863 * Each <classname> seen is remembered and indexed, so that the next time
2864 * an object in the blessed in the same <classname> is stored, the following
2865 * will be emitted:
2866 *
2867 * SX_IX_BLESS <flag> <index> <object>
2868 *
2869 * where <index> is the classname index, stored on 0 or 4 bytes depending
2870 * on the high-order bit in flag (same encoding as above for <len>).
2871 */
f0ffaed8
JH
2872static int store_blessed(
2873 stcxt_t *cxt,
2874 SV *sv,
2875 int type,
2876 HV *pkg)
7a6a85bf
RG
2877{
2878 SV *hook;
2879 I32 len;
2880 char *class;
2881 I32 classnum;
2882
2883 TRACEME(("store_blessed, type %d, class \"%s\"", type, HvNAME(pkg)));
2884
2885 /*
2886 * Look for a hook for this blessed SV and redirect to store_hook()
2887 * if needed.
2888 */
2889
2890 hook = pkg_can(cxt->hook, pkg, "STORABLE_freeze");
2891 if (hook)
2892 return store_hook(cxt, sv, type, pkg, hook);
2893
2894 /*
2895 * This is a blessed SV without any serialization hook.
2896 */
2897
2898 class = HvNAME(pkg);
2899 len = strlen(class);
2900
43d061fe
JH
2901 TRACEME(("blessed 0x%"UVxf" in %s, no hook: tagged #%d",
2902 PTR2UV(sv), class, cxt->tagnum));
7a6a85bf
RG
2903
2904 /*
2905 * Determine whether it is the first time we see that class name (in which
2906 * case it will be stored in the SX_BLESS form), or whether we already
2907 * saw that class name before (in which case the SX_IX_BLESS form will be
2908 * used).
2909 */
2910
2911 if (known_class(cxt, class, len, &classnum)) {
2912 TRACEME(("already seen class %s, ID = %d", class, classnum));
2913 PUTMARK(SX_IX_BLESS);
2914 if (classnum <= LG_BLESS) {
2915 unsigned char cnum = (unsigned char) classnum;
2916 PUTMARK(cnum);
2917 } else {
2918 unsigned char flag = (unsigned char) 0x80;
2919 PUTMARK(flag);
2920 WLEN(classnum);
2921 }
2922 } else {
2923 TRACEME(("first time we see class %s, ID = %d", class, classnum));
2924 PUTMARK(SX_BLESS);
2925 if (len <= LG_BLESS) {
2926 unsigned char clen = (unsigned char) len;
2927 PUTMARK(clen);
2928 } else {
2929 unsigned char flag = (unsigned char) 0x80;
2930 PUTMARK(flag);
2931 WLEN(len); /* Don't BER-encode, this should be rare */
2932 }
2933 WRITE(class, len); /* Final \0 is omitted */
2934 }
2935
2936 /*
2937 * Now emit the <object> part.
2938 */
2939
2940 return SV_STORE(type)(cxt, sv);
2941}
2942
2943/*
2944 * store_other
2945 *
2946 * We don't know how to store the item we reached, so return an error condition.
2947 * (it's probably a GLOB, some CODE reference, etc...)
2948 *
2949 * If they defined the `forgive_me' variable at the Perl level to some
2950 * true value, then don't croak, just warn, and store a placeholder string
2951 * instead.
2952 */
f0ffaed8 2953static int store_other(stcxt_t *cxt, SV *sv)
7a6a85bf 2954{
cc964657 2955 I32 len;
7a6a85bf
RG
2956 static char buf[80];
2957
2958 TRACEME(("store_other"));
2959
2960 /*
2961 * Fetch the value from perl only once per store() operation.
2962 */
2963
2964 if (
2965 cxt->forgive_me == 0 ||
2966 (cxt->forgive_me < 0 && !(cxt->forgive_me =
2967 SvTRUE(perl_get_sv("Storable::forgive_me", TRUE)) ? 1 : 0))
2968 )
2969 CROAK(("Can't store %s items", sv_reftype(sv, FALSE)));
2970
43d061fe
JH
2971 warn("Can't store item %s(0x%"UVxf")",
2972 sv_reftype(sv, FALSE), PTR2UV(sv));
7a6a85bf
RG
2973
2974 /*
2975 * Store placeholder string as a scalar instead...
2976 */
2977
13689cfe 2978 (void) sprintf(buf, "You lost %s(0x%"UVxf")%c", sv_reftype(sv, FALSE),
e993d95c 2979 PTR2UV(sv), (char) 0);
7a6a85bf
RG
2980
2981 len = strlen(buf);
2982 STORE_SCALAR(buf, len);
1cf92b12 2983 TRACEME(("ok (dummy \"%s\", length = %"IVdf")", buf, (IV) len));
7a6a85bf
RG
2984
2985 return 0;
2986}
2987
2988/***
2989 *** Store driving routines
2990 ***/
2991
2992/*
2993 * sv_type
2994 *
2995 * WARNING: partially duplicates Perl's sv_reftype for speed.
2996 *
2997 * Returns the type of the SV, identified by an integer. That integer
2998 * may then be used to index the dynamic routine dispatch table.
2999 */
f0ffaed8 3000static int sv_type(SV *sv)
7a6a85bf
RG
3001{
3002 switch (SvTYPE(sv)) {
3003 case SVt_NULL:
3004 case SVt_IV:
3005 case SVt_NV:
3006 /*
3007 * No need to check for ROK, that can't be set here since there
3008 * is no field capable of hodling the xrv_rv reference.
3009 */
3010 return svis_SCALAR;
3011 case SVt_PV:
3012 case SVt_RV:
3013 case SVt_PVIV:
3014 case SVt_PVNV:
3015 /*
3016 * Starting from SVt_PV, it is possible to have the ROK flag
3017 * set, the pointer to the other SV being either stored in
3018 * the xrv_rv (in the case of a pure SVt_RV), or as the
3019 * xpv_pv field of an SVt_PV and its heirs.
3020 *
3021 * However, those SV cannot be magical or they would be an
3022 * SVt_PVMG at least.
3023 */
3024 return SvROK(sv) ? svis_REF : svis_SCALAR;
3025 case SVt_PVMG:
3026 case SVt_PVLV: /* Workaround for perl5.004_04 "LVALUE" bug */
3027 if (SvRMAGICAL(sv) && (mg_find(sv, 'p')))
3028 return svis_TIED_ITEM;
3029 /* FALL THROUGH */
3030 case SVt_PVBM:
3031 if (SvRMAGICAL(sv) && (mg_find(sv, 'q')))
3032 return svis_TIED;
3033 return SvROK(sv) ? svis_REF : svis_SCALAR;
3034 case SVt_PVAV:
3035 if (SvRMAGICAL(sv) && (mg_find(sv, 'P')))
3036 return svis_TIED;
3037 return svis_ARRAY;
3038 case SVt_PVHV:
3039 if (SvRMAGICAL(sv) && (mg_find(sv, 'P')))
3040 return svis_TIED;
3041 return svis_HASH;
3042 default:
3043 break;
3044 }
3045
3046 return svis_OTHER;
3047}
3048
3049/*
3050 * store
3051 *
3052 * Recursively store objects pointed to by the sv to the specified file.
3053 *
3054 * Layout is <content> or SX_OBJECT <tagnum> if we reach an already stored
3055 * object (one for which storage has started -- it may not be over if we have
3056 * a self-referenced structure). This data set forms a stored <object>.
3057 */
f0ffaed8 3058static int store(stcxt_t *cxt, SV *sv)
7a6a85bf
RG
3059{
3060 SV **svh;
3061 int ret;
7a6a85bf 3062 int type;
43d061fe 3063 HV *hseen = cxt->hseen;
7a6a85bf 3064
43d061fe 3065 TRACEME(("store (0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
3066
3067 /*
3068 * If object has already been stored, do not duplicate data.
3069 * Simply emit the SX_OBJECT marker followed by its tag data.
3070 * The tag is always written in network order.
3071 *
3072 * NOTA BENE, for 64-bit machines: the "*svh" below does not yield a
3073 * real pointer, rather a tag number (watch the insertion code below).
3074 * That means it pobably safe to assume it is well under the 32-bit limit,
3075 * and makes the truncation safe.
3076 * -- RAM, 14/09/1999
3077 */
3078
3079 svh = hv_fetch(hseen, (char *) &sv, sizeof(sv), FALSE);
3080 if (svh) {
3081 I32 tagval = htonl(LOW_32BITS(*svh));
3082
9e21b3d0 3083 TRACEME(("object 0x%"UVxf" seen as #%d", PTR2UV(sv), ntohl(tagval)));
7a6a85bf
RG
3084
3085 PUTMARK(SX_OBJECT);
9e21b3d0 3086 WRITE_I32(tagval);
7a6a85bf
RG
3087 return 0;
3088 }
3089
3090 /*
3091 * Allocate a new tag and associate it with the address of the sv being
3092 * stored, before recursing...
3093 *
3094 * In order to avoid creating new SvIVs to hold the tagnum we just
d1be9408 3095 * cast the tagnum to an SV pointer and store that in the hash. This
7a6a85bf
RG
3096 * means that we must clean up the hash manually afterwards, but gives
3097 * us a 15% throughput increase.
3098 *
7a6a85bf
RG
3099 */
3100
3101 cxt->tagnum++;
3102 if (!hv_store(hseen,
3341c981 3103 (char *) &sv, sizeof(sv), INT2PTR(SV*, cxt->tagnum), 0))
7a6a85bf
RG
3104 return -1;
3105
3106 /*
3107 * Store `sv' and everything beneath it, using appropriate routine.
3108 * Abort immediately if we get a non-zero status back.
3109 */
3110
3111 type = sv_type(sv);
3112
43d061fe
JH
3113 TRACEME(("storing 0x%"UVxf" tag #%d, type %d...",
3114 PTR2UV(sv), cxt->tagnum, type));
7a6a85bf
RG
3115
3116 if (SvOBJECT(sv)) {
3117 HV *pkg = SvSTASH(sv);
3118 ret = store_blessed(cxt, sv, type, pkg);
3119 } else
3120 ret = SV_STORE(type)(cxt, sv);
3121
43d061fe
JH
3122 TRACEME(("%s (stored 0x%"UVxf", refcnt=%d, %s)",
3123 ret ? "FAILED" : "ok", PTR2UV(sv),
7a6a85bf
RG
3124 SvREFCNT(sv), sv_reftype(sv, FALSE)));
3125
3126 return ret;
3127}
3128
3129/*
3130 * magic_write
3131 *
3132 * Write magic number and system information into the file.
3133 * Layout is <magic> <network> [<len> <byteorder> <sizeof int> <sizeof long>
3134 * <sizeof ptr>] where <len> is the length of the byteorder hexa string.
3135 * All size and lenghts are written as single characters here.
3136 *
3137 * Note that no byte ordering info is emitted when <network> is true, since
3138 * integers will be emitted in network order in that case.
3139 */
f0ffaed8 3140static int magic_write(stcxt_t *cxt)
7a6a85bf 3141{
2aeb6432
NC
3142 /*
3143 * Starting with 0.6, the "use_network_order" byte flag is also used to
3144 * indicate the version number of the binary image, encoded in the upper
3145 * bits. The bit 0 is always used to indicate network order.
3146 */
3147 /*
3148 * Starting with 0.7, a full byte is dedicated to the minor version of
3149 * the binary format, which is incremented only when new markers are
3150 * introduced, for instance, but when backward compatibility is preserved.
3151 */
7a6a85bf 3152
2aeb6432
NC
3153 /* Make these at compile time. The WRITE() macro is sufficiently complex
3154 that it saves about 200 bytes doing it this way and only using it
3155 once. */
3156 static const unsigned char network_file_header[] = {
3157 MAGICSTR_BYTES,
3158 (STORABLE_BIN_MAJOR << 1) | 1,
3159 STORABLE_BIN_WRITE_MINOR
3160 };
3161 static const unsigned char file_header[] = {
3162 MAGICSTR_BYTES,
3163 (STORABLE_BIN_MAJOR << 1) | 0,
3164 STORABLE_BIN_WRITE_MINOR,
3165 /* sizeof the array includes the 0 byte at the end: */
3166 (char) sizeof (byteorderstr) - 1,
3167 BYTEORDER_BYTES,
3168 (unsigned char) sizeof(int),
3169 (unsigned char) sizeof(long),
3170 (unsigned char) sizeof(char *),
3171 (unsigned char) sizeof(NV)
3172 };
3173 const unsigned char *header;
3174 SSize_t length;
3175
3176 TRACEME(("magic_write on fd=%d", cxt->fio ? PerlIO_fileno(cxt->fio) : -1));
3177
3178 if (cxt->netorder) {
3179 header = network_file_header;
3180 length = sizeof (network_file_header);
3181 } else {
3182 header = file_header;
3183 length = sizeof (file_header);
3184 }
3185
3186 if (!cxt->fio) {
3187 /* sizeof the array includes the 0 byte at the end. */
3188 header += sizeof (magicstr) - 1;
3189 length -= sizeof (magicstr) - 1;
3190 }
3191
3192 WRITE(header, length);
3193
3194 if (!cxt->netorder) {
9e21b3d0 3195 TRACEME(("ok (magic_write byteorder = 0x%lx [%d], I%d L%d P%d D%d)",
2aeb6432 3196 (unsigned long) BYTEORDER, (int) sizeof (byteorderstr) - 1,
9e21b3d0
JH
3197 (int) sizeof(int), (int) sizeof(long),
3198 (int) sizeof(char *), (int) sizeof(NV)));
2aeb6432
NC
3199 }
3200 return 0;
7a6a85bf
RG
3201}
3202
3203/*
3204 * do_store
3205 *
3206 * Common code for store operations.
3207 *
3208 * When memory store is requested (f = NULL) and a non null SV* is given in
3209 * `res', it is filled with a new SV created out of the memory buffer.
3210 *
3211 * It is required to provide a non-null `res' when the operation type is not
3212 * dclone() and store() is performed to memory.
3213 */
f0ffaed8
JH
3214static int do_store(
3215 PerlIO *f,
3216 SV *sv,
3217 int optype,
3218 int network_order,
3219 SV **res)
7a6a85bf
RG
3220{
3221 dSTCXT;
3222 int status;
3223
3224 ASSERT(!(f == 0 && !(optype & ST_CLONE)) || res,
3225 ("must supply result SV pointer for real recursion to memory"));
3226
3227 TRACEME(("do_store (optype=%d, netorder=%d)",
3228 optype, network_order));
3229
3230 optype |= ST_STORE;
3231
3232 /*
3233 * Workaround for CROAK leak: if they enter with a "dirty" context,
3234 * free up memory for them now.
3235 */
3236
dd19458b 3237 if (cxt->s_dirty)
7a6a85bf
RG
3238 clean_context(cxt);
3239
3240 /*
3241 * Now that STORABLE_xxx hooks exist, it is possible that they try to
3242 * re-enter store() via the hooks. We need to stack contexts.
3243 */
3244
3245 if (cxt->entry)
3246 cxt = allocate_context(cxt);
3247
3248 cxt->entry++;
3249
3250 ASSERT(cxt->entry == 1, ("starting new recursion"));
dd19458b 3251 ASSERT(!cxt->s_dirty, ("clean context"));
7a6a85bf
RG
3252
3253 /*
3254 * Ensure sv is actually a reference. From perl, we called something
3255 * like:
3256 * pstore(FILE, \@array);
3257 * so we must get the scalar value behing that reference.
3258 */
3259
3260 if (!SvROK(sv))
3261 CROAK(("Not a reference"));
3262 sv = SvRV(sv); /* So follow it to know what to store */
3263
3264 /*
3265 * If we're going to store to memory, reset the buffer.
3266 */
3267
3268 if (!f)
3269 MBUF_INIT(0);
3270
3271 /*
3272 * Prepare context and emit headers.
3273 */
3274
3275 init_store_context(cxt, f, optype, network_order);
3276
3277 if (-1 == magic_write(cxt)) /* Emit magic and ILP info */
3278 return 0; /* Error */
3279
3280 /*
3281 * Recursively store object...
3282 */
3283
3284 ASSERT(is_storing(), ("within store operation"));
3285
3286 status = store(cxt, sv); /* Just do it! */
3287
3288 /*
3289 * If they asked for a memory store and they provided an SV pointer,
3290 * make an SV string out of the buffer and fill their pointer.
3291 *
3292 * When asking for ST_REAL, it's MANDATORY for the caller to provide
3293 * an SV, since context cleanup might free the buffer if we did recurse.
3294 * (unless caller is dclone(), which is aware of that).
3295 */
3296
3297 if (!cxt->fio && res)
3298 *res = mbuf2sv();
3299
3300 /*
3301 * Final cleanup.
3302 *
3303 * The "root" context is never freed, since it is meant to be always
3304 * handy for the common case where no recursion occurs at all (i.e.
3305 * we enter store() outside of any Storable code and leave it, period).
3306 * We know it's the "root" context because there's nothing stacked
3307 * underneath it.
3308 *
3309 * OPTIMIZATION:
3310 *
3311 * When deep cloning, we don't free the context: doing so would force
3312 * us to copy the data in the memory buffer. Sicne we know we're
3313 * about to enter do_retrieve...
3314 */
3315
3316 clean_store_context(cxt);
3317 if (cxt->prev && !(cxt->optype & ST_CLONE))
3318 free_context(cxt);
3319
3320 TRACEME(("do_store returns %d", status));
3321
3322 return status == 0;
3323}
3324
3325/*
3326 * pstore
3327 *
3328 * Store the transitive data closure of given object to disk.
3329 * Returns 0 on error, a true value otherwise.
3330 */
f0ffaed8 3331int pstore(PerlIO *f, SV *sv)
7a6a85bf
RG
3332{
3333 TRACEME(("pstore"));
f0ffaed8 3334 return do_store(f, sv, 0, FALSE, (SV**) 0);
7a6a85bf
RG
3335
3336}
3337
3338/*
3339 * net_pstore
3340 *
3341 * Same as pstore(), but network order is used for integers and doubles are
3342 * emitted as strings.
3343 */
f0ffaed8 3344int net_pstore(PerlIO *f, SV *sv)
7a6a85bf
RG
3345{
3346 TRACEME(("net_pstore"));
f0ffaed8 3347 return do_store(f, sv, 0, TRUE, (SV**) 0);
7a6a85bf
RG
3348}
3349
3350/***
3351 *** Memory stores.
3352 ***/
3353
3354/*
3355 * mbuf2sv
3356 *
3357 * Build a new SV out of the content of the internal memory buffer.
3358 */
f0ffaed8 3359static SV *mbuf2sv(void)
7a6a85bf
RG
3360{
3361 dSTCXT;
3362
3363 return newSVpv(mbase, MBUF_SIZE());
3364}
3365
3366/*
3367 * mstore
3368 *
3369 * Store the transitive data closure of given object to memory.
3370 * Returns undef on error, a scalar value containing the data otherwise.
3371 */
f0ffaed8 3372SV *mstore(SV *sv)
7a6a85bf 3373{
7a6a85bf
RG
3374 SV *out;
3375
3376 TRACEME(("mstore"));
3377
f0ffaed8 3378 if (!do_store((PerlIO*) 0, sv, 0, FALSE, &out))
7a6a85bf
RG
3379 return &PL_sv_undef;
3380
3381 return out;
3382}
3383
3384/*
3385 * net_mstore
3386 *
3387 * Same as mstore(), but network order is used for integers and doubles are
3388 * emitted as strings.
3389 */
f0ffaed8 3390SV *net_mstore(SV *sv)
7a6a85bf 3391{
7a6a85bf
RG
3392 SV *out;
3393
3394 TRACEME(("net_mstore"));
3395
f0ffaed8 3396 if (!do_store((PerlIO*) 0, sv, 0, TRUE, &out))
7a6a85bf
RG
3397 return &PL_sv_undef;
3398
3399 return out;
3400}
3401
3402/***
3403 *** Specific retrieve callbacks.
3404 ***/
3405
3406/*
3407 * retrieve_other
3408 *
3409 * Return an error via croak, since it is not possible that we get here
3410 * under normal conditions, when facing a file produced via pstore().
3411 */
b12202d0 3412static SV *retrieve_other(stcxt_t *cxt, char *cname)
7a6a85bf
RG
3413{
3414 if (
3415 cxt->ver_major != STORABLE_BIN_MAJOR &&
3416 cxt->ver_minor != STORABLE_BIN_MINOR
3417 ) {
3418 CROAK(("Corrupted storable %s (binary v%d.%d), current is v%d.%d",
3419 cxt->fio ? "file" : "string",
3420 cxt->ver_major, cxt->ver_minor,
3421 STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR));
3422 } else {
3423 CROAK(("Corrupted storable %s (binary v%d.%d)",
3424 cxt->fio ? "file" : "string",
3425 cxt->ver_major, cxt->ver_minor));
3426 }
3427
3428 return (SV *) 0; /* Just in case */
3429}
3430
3431/*
3432 * retrieve_idx_blessed
3433 *
3434 * Layout is SX_IX_BLESS <index> <object> with SX_IX_BLESS already read.
3435 * <index> can be coded on either 1 or 5 bytes.
3436 */
b12202d0 3437static SV *retrieve_idx_blessed(stcxt_t *cxt, char *cname)
7a6a85bf
RG
3438{
3439 I32 idx;
3440 char *class;
3441 SV **sva;
3442 SV *sv;
3443
3444 TRACEME(("retrieve_idx_blessed (#%d)", cxt->tagnum));
b12202d0 3445 ASSERT(!cname, ("no bless-into class given here, got %s", cname));
7a6a85bf
RG
3446
3447 GETMARK(idx); /* Index coded on a single char? */
3448 if (idx & 0x80)
3449 RLEN(idx);
3450
3451 /*
3452 * Fetch classname in `aclass'
3453 */
3454
3455 sva = av_fetch(cxt->aclass, idx, FALSE);
3456 if (!sva)
e993d95c 3457 CROAK(("Class name #%"IVdf" should have been seen already", (IV) idx));
7a6a85bf
RG
3458
3459 class = SvPVX(*sva); /* We know it's a PV, by construction */
3460
3461 TRACEME(("class ID %d => %s", idx, class));
3462
3463 /*
3464 * Retrieve object and bless it.
3465 */
3466
b12202d0 3467 sv = retrieve(cxt, class); /* First SV which is SEEN will be blessed */
7a6a85bf
RG
3468
3469 return sv;
3470}
3471
3472/*
3473 * retrieve_blessed
3474 *
3475 * Layout is SX_BLESS <len> <classname> <object> with SX_BLESS already read.
3476 * <len> can be coded on either 1 or 5 bytes.
3477 */
b12202d0 3478static SV *retrieve_blessed(stcxt_t *cxt, char *cname)
7a6a85bf
RG
3479{
3480 I32 len;
3481 SV *sv;
3482 char buf[LG_BLESS + 1]; /* Avoid malloc() if possible */
3483 char *class = buf;
3484
3485 TRACEME(("retrieve_blessed (#%d)", cxt->tagnum));
b12202d0 3486 ASSERT(!cname, ("no bless-into class given here, got %s", cname));
7a6a85bf
RG
3487
3488 /*
3489 * Decode class name length and read that name.
3490 *
3491 * Short classnames have two advantages: their length is stored on one
3492 * single byte, and the string can be read on the stack.
3493 */
3494
3495 GETMARK(len); /* Length coded on a single char? */
3496 if (len & 0x80) {
3497 RLEN(len);
3498 TRACEME(("** allocating %d bytes for class name", len+1));
3499 New(10003, class, len+1, char);
3500 }
3501 READ(class, len);
3502 class[len] = '\0'; /* Mark string end */
3503
3504 /*
3505 * It's a new classname, otherwise it would have been an SX_IX_BLESS.
3506 */
3507
b12202d0
JH
3508 TRACEME(("new class name \"%s\" will bear ID = %d", class, cxt->classnum));
3509
7a6a85bf
RG
3510 if (!av_store(cxt->aclass, cxt->classnum++, newSVpvn(class, len)))
3511 return (SV *) 0;
3512
3513 /*
3514 * Retrieve object and bless it.
3515 */
3516
b12202d0
JH
3517 sv = retrieve(cxt, class); /* First SV which is SEEN will be blessed */
3518 if (class != buf)
3519 Safefree(class);
7a6a85bf
RG
3520
3521 return sv;
3522}
3523
3524/*
3525 * retrieve_hook
3526 *
3527 * Layout: SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
3528 * with leading mark already read, as usual.
3529 *
3530 * When recursion was involved during serialization of the object, there
3531 * is an unknown amount of serialized objects after the SX_HOOK mark. Until
3532 * we reach a <flags> marker with the recursion bit cleared.
b12202d0
JH
3533 *
3534 * If the first <flags> byte contains a type of SHT_EXTRA, then the real type
3535 * is held in the <extra> byte, and if the object is tied, the serialized
3536 * magic object comes at the very end:
3537 *
3538 * SX_HOOK <flags> <extra> ... [<len3> <object-IDs>] <magic object>
3539 *
3540 * This means the STORABLE_thaw hook will NOT get a tied variable during its
3541 * processing (since we won't have seen the magic object by the time the hook
3542 * is called). See comments below for why it was done that way.
7a6a85bf 3543 */
b12202d0 3544static SV *retrieve_hook(stcxt_t *cxt, char *cname)
7a6a85bf
RG
3545{
3546 I32 len;
3547 char buf[LG_BLESS + 1]; /* Avoid malloc() if possible */
3548 char *class = buf;
3549 unsigned int flags;
3550 I32 len2;
3551 SV *frozen;
3552 I32 len3 = 0;
3553 AV *av = 0;
3554 SV *hook;
3555 SV *sv;
3556 SV *rv;
3557 int obj_type;
7a6a85bf 3558 int clone = cxt->optype & ST_CLONE;
b12202d0
JH
3559 char mtype = '\0';
3560 unsigned int extra_type = 0;
7a6a85bf
RG
3561
3562 TRACEME(("retrieve_hook (#%d)", cxt->tagnum));
b12202d0 3563 ASSERT(!cname, ("no bless-into class given here, got %s", cname));
7a6a85bf
RG
3564
3565 /*
3566 * Read flags, which tell us about the type, and whether we need to recurse.
3567 */
3568
3569 GETMARK(flags);
3570
3571 /*
3572 * Create the (empty) object, and mark it as seen.
3573 *
3574 * This must be done now, because tags are incremented, and during
3575 * serialization, the object tag was affected before recursion could
3576 * take place.
3577 */
3578
3579 obj_type = flags & SHF_TYPE_MASK;
3580 switch (obj_type) {
3581 case SHT_SCALAR:
3582 sv = newSV(0);
3583 break;
3584 case SHT_ARRAY:
3585 sv = (SV *) newAV();
3586 break;
3587 case SHT_HASH:
3588 sv = (SV *) newHV();
3589 break;
b12202d0
JH
3590 case SHT_EXTRA:
3591 /*
3592 * Read <extra> flag to know the type of the object.
3593 * Record associated magic type for later.
3594 */
3595 GETMARK(extra_type);
3596 switch (extra_type) {
3597 case SHT_TSCALAR:
3598 sv = newSV(0);
3599 mtype = 'q';
3600 break;
3601 case SHT_TARRAY:
3602 sv = (SV *) newAV();
3603 mtype = 'P';
3604 break;
3605 case SHT_THASH:
3606 sv = (SV *) newHV();
3607 mtype = 'P';
3608 break;
3609 default:
3610 return retrieve_other(cxt, 0); /* Let it croak */
3611 }
3612 break;
7a6a85bf 3613 default:
b12202d0 3614 return retrieve_other(cxt, 0); /* Let it croak */
7a6a85bf 3615 }
b12202d0 3616 SEEN(sv, 0); /* Don't bless yet */
7a6a85bf
RG
3617
3618 /*
3619 * Whilst flags tell us to recurse, do so.
3620 *
3621 * We don't need to remember the addresses returned by retrieval, because
3622 * all the references will be obtained through indirection via the object
3623 * tags in the object-ID list.
3624 */
3625
3626 while (flags & SHF_NEED_RECURSE) {
3627 TRACEME(("retrieve_hook recursing..."));
b12202d0 3628 rv = retrieve(cxt, 0);
7a6a85bf
RG
3629 if (!rv)
3630 return (SV *) 0;
43d061fe
JH
3631 TRACEME(("retrieve_hook back with rv=0x%"UVxf,
3632 PTR2UV(rv)));
7a6a85bf
RG
3633 GETMARK(flags);
3634 }
3635
3636 if (flags & SHF_IDX_CLASSNAME) {
3637 SV **sva;
3638 I32 idx;
3639
3640 /*
3641 * Fetch index from `aclass'
3642 */
3643
3644 if (flags & SHF_LARGE_CLASSLEN)
3645 RLEN(idx);
3646 else
3647 GETMARK(idx);
3648
3649 sva = av_fetch(cxt->aclass, idx, FALSE);
3650 if (!sva)
e993d95c
JH
3651 CROAK(("Class name #%"IVdf" should have been seen already",
3652 (IV) idx));
7a6a85bf
RG
3653
3654 class = SvPVX(*sva); /* We know it's a PV, by construction */
3655 TRACEME(("class ID %d => %s", idx, class));
3656
3657 } else {
3658 /*
3659 * Decode class name length and read that name.
3660 *
3661 * NOTA BENE: even if the length is stored on one byte, we don't read
3662 * on the stack. Just like retrieve_blessed(), we limit the name to
3663 * LG_BLESS bytes. This is an arbitrary decision.
3664 */
3665
3666 if (flags & SHF_LARGE_CLASSLEN)
3667 RLEN(len);
3668 else
3669 GETMARK(len);
3670
3671 if (len > LG_BLESS) {
3672 TRACEME(("** allocating %d bytes for class name", len+1));
3673 New(10003, class, len+1, char);
3674 }
3675
3676 READ(class, len);
3677 class[len] = '\0'; /* Mark string end */
3678
3679 /*
3680 * Record new classname.
3681 */
3682
3683 if (!av_store(cxt->aclass, cxt->classnum++, newSVpvn(class, len)))
3684 return (SV *) 0;
3685 }
3686
3687 TRACEME(("class name: %s", class));
3688
3689 /*
d1be9408 3690 * Decode user-frozen string length and read it in an SV.
7a6a85bf
RG
3691 *
3692 * For efficiency reasons, we read data directly into the SV buffer.
3693 * To understand that code, read retrieve_scalar()
3694 */
3695
3696 if (flags & SHF_LARGE_STRLEN)
3697 RLEN(len2);
3698 else
3699 GETMARK(len2);
3700
3701 frozen = NEWSV(10002, len2);
3702 if (len2) {
3703 SAFEREAD(SvPVX(frozen), len2, frozen);
3704 SvCUR_set(frozen, len2);
3705 *SvEND(frozen) = '\0';
3706 }
3707 (void) SvPOK_only(frozen); /* Validates string pointer */
dd19458b
JH
3708 if (cxt->s_tainted) /* Is input source tainted? */
3709 SvTAINT(frozen);
7a6a85bf
RG
3710
3711 TRACEME(("frozen string: %d bytes", len2));
3712
3713 /*
3714 * Decode object-ID list length, if present.
3715 */
3716
3717 if (flags & SHF_HAS_LIST) {
3718 if (flags & SHF_LARGE_LISTLEN)
3719 RLEN(len3);
3720 else
3721 GETMARK(len3);
3722 if (len3) {
3723 av = newAV();
3724 av_extend(av, len3 + 1); /* Leave room for [0] */
3725 AvFILLp(av) = len3; /* About to be filled anyway */
3726 }
3727 }
3728
3729 TRACEME(("has %d object IDs to link", len3));
3730
3731 /*
3732 * Read object-ID list into array.
3733 * Because we pre-extended it, we can cheat and fill it manually.
3734 *
3735 * We read object tags and we can convert them into SV* on the fly
3736 * because we know all the references listed in there (as tags)
3737 * have been already serialized, hence we have a valid correspondance
3738 * between each of those tags and the recreated SV.
3739 */
3740
3741 if (av) {
3742 SV **ary = AvARRAY(av);
3743 int i;
3744 for (i = 1; i <= len3; i++) { /* We leave [0] alone */
3745 I32 tag;
3746 SV **svh;
3747 SV *xsv;
3748
9e21b3d0 3749 READ_I32(tag);
7a6a85bf
RG
3750 tag = ntohl(tag);
3751 svh = av_fetch(cxt->aseen, tag, FALSE);
3752 if (!svh)
e993d95c
JH
3753 CROAK(("Object #%"IVdf" should have been retrieved already",
3754 (IV) tag));
7a6a85bf
RG
3755 xsv = *svh;
3756 ary[i] = SvREFCNT_inc(xsv);
3757 }
3758 }
3759
3760 /*
3761 * Bless the object and look up the STORABLE_thaw hook.
3762 */
3763
3764 BLESS(sv, class);
3765 hook = pkg_can(cxt->hook, SvSTASH(sv), "STORABLE_thaw");
212e9bde
JH
3766 if (!hook) {
3767 /*
3768 * Hook not found. Maybe they did not require the module where this
3769 * hook is defined yet?
3770 *
3771 * If the require below succeeds, we'll be able to find the hook.
3772 * Still, it only works reliably when each class is defined in a
3773 * file of its own.
3774 */
3775
3776 SV *psv = newSVpvn("require ", 8);
3777 sv_catpv(psv, class);
3778
3779 TRACEME(("No STORABLE_thaw defined for objects of class %s", class));
3780 TRACEME(("Going to require module '%s' with '%s'", class, SvPVX(psv)));
3781
3782 perl_eval_sv(psv, G_DISCARD);
3783 sv_free(psv);
3784
3785 /*
3786 * We cache results of pkg_can, so we need to uncache before attempting
3787 * the lookup again.
3788 */
3789
3790 pkg_uncache(cxt->hook, SvSTASH(sv), "STORABLE_thaw");
3791 hook = pkg_can(cxt->hook, SvSTASH(sv), "STORABLE_thaw");
3792
3793 if (!hook)
3794 CROAK(("No STORABLE_thaw defined for objects of class %s "
3795 "(even after a \"require %s;\")", class, class));
3796 }
7a6a85bf
RG
3797
3798 /*
3799 * If we don't have an `av' yet, prepare one.
3800 * Then insert the frozen string as item [0].
3801 */
3802
3803 if (!av) {
3804 av = newAV();
3805 av_extend(av, 1);