c87ad92edb7bbc974726f0c04662b936bc86867c
[p5sagit/p5-mst-13.2.git] / ext / Storable / Storable.xs
1 /*
2  * Store and retrieve mechanism.
3  */
4
5 /*
6  * $Id: Storable.xs,v 1.0.1.10 2001/08/28 21:52:14 ram Exp $
7  *
8  *  Copyright (c) 1995-2000, Raphael Manfredi
9  *  
10  *  You may redistribute only under the same terms as Perl 5, as specified
11  *  in the README file that comes with the distribution.
12  *
13  * $Log: Storable.xs,v $
14  * Revision 1.0.1.10  2001/08/28 21:52:14  ram
15  * patch13: removed spurious debugging messages
16  *
17  * Revision 1.0.1.9  2001/07/01 11:25:02  ram
18  * patch12: fixed memory corruption on croaks during thaw()
19  * patch12: made code compile cleanly with -Wall (Jarkko Hietaniemi)
20  * patch12: changed tagnum and classnum from I32 to IV in context
21  *
22  * Revision 1.0.1.8  2001/03/15 00:20:55  ram
23  * patch11: last version was wrongly compiling with assertions on
24  *
25  * Revision 1.0.1.7  2001/02/17 12:25:26  ram
26  * patch8: now bless objects ASAP at retrieve time
27  * patch8: added support for blessed ref to tied structures
28  *
29  * Revision 1.0.1.6  2001/01/03 09:40:40  ram
30  * patch7: prototype and casting cleanup
31  * patch7: trace offending package when overloading cannot be restored
32  * patch7: made context cleanup safer to avoid dup freeing
33  *
34  * Revision 1.0.1.5  2000/11/05 17:21:24  ram
35  * patch6: fixed severe "object lost" bug for STORABLE_freeze returns
36  *
37  * Revision 1.0.1.4  2000/10/26 17:11:04  ram
38  * patch5: auto requires module of blessed ref when STORABLE_thaw misses
39  *
40  * Revision 1.0.1.3  2000/09/29 19:49:57  ram
41  * patch3: avoid using "tainted" and "dirty" since Perl remaps them via cpp
42  *
43  * Revision 1.0.1.2  2000/09/28 21:43:10  ram
44  * patch2: perls before 5.004_04 lack newSVpvn
45  *
46  * Revision 1.0.1.1  2000/09/17 16:47:49  ram
47  * patch1: now only taint retrieved data when source was tainted
48  * patch1: added support for UTF-8 strings
49  * patch1: fixed store hook bug: was allocating class id too soon
50  *
51  * Revision 1.0  2000/09/01 19:40:41  ram
52  * Baseline for first official release.
53  *
54  */
55
56 #include <EXTERN.h>
57 #include <perl.h>
58 #include <patchlevel.h>         /* Perl's one, needed since 5.6 */
59 #include <XSUB.h>
60
61 #if 1
62 #define DEBUGME /* Debug mode, turns assertions on as well */
63 #define DASSERT /* Assertion mode */
64 #endif
65
66 /*
67  * Pre PerlIO time when none of USE_PERLIO and PERLIO_IS_STDIO is defined
68  * Provide them with the necessary defines so they can build with pre-5.004.
69  */
70 #ifndef USE_PERLIO
71 #ifndef PERLIO_IS_STDIO
72 #define PerlIO FILE
73 #define PerlIO_getc(x) getc(x)
74 #define PerlIO_putc(f,x) putc(x,f)
75 #define PerlIO_read(x,y,z) fread(y,1,z,x)
76 #define PerlIO_write(x,y,z) fwrite(y,1,z,x)
77 #define PerlIO_stdoutf printf
78 #endif  /* PERLIO_IS_STDIO */
79 #endif  /* USE_PERLIO */
80
81 /*
82  * Earlier versions of perl might be used, we can't assume they have the latest!
83  */
84
85 #ifndef PERL_VERSION            /* For perls < 5.6 */
86 #define PERL_VERSION PATCHLEVEL
87 #ifndef newRV_noinc
88 #define newRV_noinc(sv)         ((Sv = newRV(sv)), --SvREFCNT(SvRV(Sv)), Sv)
89 #endif
90 #if (PATCHLEVEL <= 4)           /* Older perls (<= 5.004) lack PL_ namespace */
91 #define PL_sv_yes       sv_yes
92 #define PL_sv_no        sv_no
93 #define PL_sv_undef     sv_undef
94 #if (SUBVERSION <= 4)           /* 5.004_04 has been reported to lack newSVpvn */
95 #define newSVpvn newSVpv
96 #endif
97 #endif                                          /* PATCHLEVEL <= 4 */
98 #ifndef HvSHAREKEYS_off
99 #define HvSHAREKEYS_off(hv)     /* Ignore */
100 #endif
101 #ifndef AvFILLp                         /* Older perls (<=5.003) lack AvFILLp */
102 #define AvFILLp AvFILL
103 #endif
104 typedef double NV;                      /* Older perls lack the NV type */
105 #define IVdf            "ld"    /* Various printf formats for Perl types */
106 #define UVuf            "lu"
107 #define UVof            "lo"
108 #define UVxf            "lx"
109 #define INT2PTR(t,v) (t)(IV)(v)
110 #define PTR2UV(v)    (unsigned long)(v)
111 #endif                                          /* PERL_VERSION -- perls < 5.6 */
112
113 #ifndef NVef                            /* The following were not part of perl 5.6 */
114 #if defined(USE_LONG_DOUBLE) && \
115         defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
116 #define NVef            PERL_PRIeldbl
117 #define NVff            PERL_PRIfldbl
118 #define NVgf            PERL_PRIgldbl
119 #else
120 #define NVef            "e"
121 #define NVff            "f"
122 #define NVgf            "g"
123 #endif
124 #endif
125
126 #ifdef DEBUGME
127
128 #ifndef DASSERT
129 #define DASSERT
130 #endif
131
132 /*
133  * TRACEME() will only output things when the $Storable::DEBUGME is true.
134  */
135
136 #define TRACEME(x)      do {                                                                    \
137         if (SvTRUE(perl_get_sv("Storable::DEBUGME", TRUE)))     \
138                 { PerlIO_stdoutf x; PerlIO_stdoutf("\n"); }                     \
139 } while (0)
140 #else
141 #define TRACEME(x)
142 #endif  /* DEBUGME */
143
144 #ifdef DASSERT
145 #define ASSERT(x,y)     do {                                                                    \
146         if (!(x)) {                                                                                             \
147                 PerlIO_stdoutf("ASSERT FAILED (\"%s\", line %d): ",     \
148                         __FILE__, __LINE__);                                                    \
149                 PerlIO_stdoutf y; PerlIO_stdoutf("\n");                         \
150         }                                                                                                               \
151 } while (0)
152 #else
153 #define ASSERT(x,y)
154 #endif
155
156 /*
157  * Type markers.
158  */
159
160 #define C(x) ((char) (x))       /* For markers with dynamic retrieval handling */
161
162 #define SX_OBJECT       C(0)    /* Already stored object */
163 #define SX_LSCALAR      C(1)    /* Scalar (large binary) follows (length, data) */
164 #define SX_ARRAY        C(2)    /* Array forthcominng (size, item list) */
165 #define SX_HASH         C(3)    /* Hash forthcoming (size, key/value pair list) */
166 #define SX_REF          C(4)    /* Reference to object forthcoming */
167 #define SX_UNDEF        C(5)    /* Undefined scalar */
168 #define SX_INTEGER      C(6)    /* Integer forthcoming */
169 #define SX_DOUBLE       C(7)    /* Double forthcoming */
170 #define SX_BYTE         C(8)    /* (signed) byte forthcoming */
171 #define SX_NETINT       C(9)    /* Integer in network order forthcoming */
172 #define SX_SCALAR       C(10)   /* Scalar (binary, small) follows (length, data) */
173 #define SX_TIED_ARRAY  C(11)  /* Tied array forthcoming */
174 #define SX_TIED_HASH   C(12)  /* Tied hash forthcoming */
175 #define SX_TIED_SCALAR C(13)  /* Tied scalar forthcoming */
176 #define SX_SV_UNDEF     C(14)   /* Perl's immortal PL_sv_undef */
177 #define SX_SV_YES       C(15)   /* Perl's immortal PL_sv_yes */
178 #define SX_SV_NO        C(16)   /* Perl's immortal PL_sv_no */
179 #define SX_BLESS        C(17)   /* Object is blessed */
180 #define SX_IX_BLESS     C(18)   /* Object is blessed, classname given by index */
181 #define SX_HOOK         C(19)   /* Stored via hook, user-defined */
182 #define SX_OVERLOAD     C(20)   /* Overloaded reference */
183 #define SX_TIED_KEY C(21)   /* Tied magic key forthcoming */
184 #define SX_TIED_IDX C(22)   /* Tied magic index forthcoming */
185 #define SX_UTF8STR      C(23)   /* UTF-8 string forthcoming (small) */
186 #define SX_LUTF8STR     C(24)   /* UTF-8 string forthcoming (large) */
187 #define SX_FLAG_HASH    C(25)   /* Hash with flags forthcoming (size, flags, key/flags/value triplet list) */
188 #define SX_ERROR        C(26)   /* Error */
189
190 /*
191  * Those are only used to retrieve "old" pre-0.6 binary images.
192  */
193 #define SX_ITEM         'i'             /* An array item introducer */
194 #define SX_IT_UNDEF     'I'             /* Undefined array item */
195 #define SX_KEY          'k'             /* A hash key introducer */
196 #define SX_VALUE        'v'             /* A hash value introducer */
197 #define SX_VL_UNDEF     'V'             /* Undefined hash value */
198
199 /*
200  * Those are only used to retrieve "old" pre-0.7 binary images
201  */
202
203 #define SX_CLASS        'b'             /* Object is blessed, class name length <255 */
204 #define SX_LG_CLASS 'B'         /* Object is blessed, class name length >255 */
205 #define SX_STORED       'X'             /* End of object */
206
207 /*
208  * Limits between short/long length representation.
209  */
210
211 #define LG_SCALAR       255             /* Large scalar length limit */
212 #define LG_BLESS        127             /* Large classname bless limit */
213
214 /*
215  * Operation types
216  */
217
218 #define ST_STORE        0x1             /* Store operation */
219 #define ST_RETRIEVE     0x2             /* Retrieval operation */
220 #define ST_CLONE        0x4             /* Deep cloning operation */
221
222 /*
223  * The following structure is used for hash table key retrieval. Since, when
224  * retrieving objects, we'll be facing blessed hash references, it's best
225  * to pre-allocate that buffer once and resize it as the need arises, never
226  * freeing it (keys will be saved away someplace else anyway, so even large
227  * keys are not enough a motivation to reclaim that space).
228  *
229  * This structure is also used for memory store/retrieve operations which
230  * happen in a fixed place before being malloc'ed elsewhere if persistency
231  * is required. Hence the aptr pointer.
232  */
233 struct extendable {
234         char *arena;            /* Will hold hash key strings, resized as needed */
235         STRLEN asiz;            /* Size of aforementionned buffer */
236         char *aptr;                     /* Arena pointer, for in-place read/write ops */
237         char *aend;                     /* First invalid address */
238 };
239
240 /*
241  * At store time:
242  * A hash table records the objects which have already been stored.
243  * Those are referred to as SX_OBJECT in the file, and their "tag" (i.e.
244  * an arbitrary sequence number) is used to identify them.
245  *
246  * At retrieve time:
247  * An array table records the objects which have already been retrieved,
248  * as seen by the tag determind by counting the objects themselves. The
249  * reference to that retrieved object is kept in the table, and is returned
250  * when an SX_OBJECT is found bearing that same tag.
251  *
252  * The same processing is used to record "classname" for blessed objects:
253  * indexing by a hash at store time, and via an array at retrieve time.
254  */
255
256 typedef unsigned long stag_t;   /* Used by pre-0.6 binary format */
257
258 /*
259  * The following "thread-safe" related defines were contributed by
260  * Murray Nesbitt <murray@activestate.com> and integrated by RAM, who
261  * only renamed things a little bit to ensure consistency with surrounding
262  * code.        -- RAM, 14/09/1999
263  *
264  * The original patch suffered from the fact that the stcxt_t structure
265  * was global.  Murray tried to minimize the impact on the code as much as
266  * possible.
267  *
268  * Starting with 0.7, Storable can be re-entrant, via the STORABLE_xxx hooks
269  * on objects.  Therefore, the notion of context needs to be generalized,
270  * threading or not.
271  */
272
273 #define MY_VERSION "Storable(" XS_VERSION ")"
274
275 /*
276  * Fields s_tainted and s_dirty are prefixed with s_ because Perl's include
277  * files remap tainted and dirty when threading is enabled.  That's bad for
278  * perl to remap such common words.     -- RAM, 29/09/00
279  */
280
281 typedef struct stcxt {
282         int entry;                      /* flags recursion */
283         int optype;                     /* type of traversal operation */
284         HV *hseen;                      /* which objects have been seen, store time */
285         AV *hook_seen;          /* which SVs were returned by STORABLE_freeze() */
286         AV *aseen;                      /* which objects have been seen, retrieve time */
287         HV *hclass;                     /* which classnames have been seen, store time */
288         AV *aclass;                     /* which classnames have been seen, retrieve time */
289         HV *hook;                       /* cache for hook methods per class name */
290         IV tagnum;                      /* incremented at store time for each seen object */
291         IV classnum;            /* incremented at store time for each seen classname */
292         int netorder;           /* true if network order used */
293         int s_tainted;          /* true if input source is tainted, at retrieve time */
294         int forgive_me;         /* whether to be forgiving... */
295         int canonical;          /* whether to store hashes sorted by key */
296         int s_dirty;            /* context is dirty due to CROAK() -- can be cleaned */
297         int membuf_ro;          /* true means membuf is read-only and msaved is rw */
298         struct extendable keybuf;       /* for hash key retrieval */
299         struct extendable membuf;       /* for memory store/retrieve operations */
300         struct extendable msaved;       /* where potentially valid mbuf is saved */
301         PerlIO *fio;            /* where I/O are performed, NULL for memory */
302         int ver_major;          /* major of version for retrieved object */
303         int ver_minor;          /* minor of version for retrieved object */
304         SV *(**retrieve_vtbl)();        /* retrieve dispatch table */
305         struct stcxt *prev;     /* contexts chained backwards in real recursion */
306 } stcxt_t;
307
308 #if defined(MULTIPLICITY) || defined(PERL_OBJECT) || defined(PERL_CAPI)
309
310 #if (PATCHLEVEL <= 4) && (SUBVERSION < 68)
311 #define dSTCXT_SV                                                                       \
312         SV *perinterp_sv = perl_get_sv(MY_VERSION, FALSE)
313 #else   /* >= perl5.004_68 */
314 #define dSTCXT_SV                                                                       \
315         SV *perinterp_sv = *hv_fetch(PL_modglobal,              \
316                 MY_VERSION, sizeof(MY_VERSION)-1, TRUE)
317 #endif  /* < perl5.004_68 */
318
319 #define dSTCXT_PTR(T,name)                                                      \
320         T name = ((perinterp_sv && SvIOK(perinterp_sv)  \
321                                 ? INT2PTR(T, SvIVX(perinterp_sv)) : (T) 0))
322 #define dSTCXT                                                                          \
323         dSTCXT_SV;                                                                              \
324         dSTCXT_PTR(stcxt_t *, cxt)
325
326 #define INIT_STCXT                                                                      \
327       dSTCXT;                                                                           \
328       Newz(0, cxt, 1, stcxt_t);                                         \
329       sv_setiv(perinterp_sv, PTR2IV(cxt))
330
331 #define SET_STCXT(x) do {                                                       \
332         dSTCXT_SV;                                                                              \
333         sv_setiv(perinterp_sv, PTR2IV(x));                              \
334 } while (0)
335
336 #else /* !MULTIPLICITY && !PERL_OBJECT && !PERL_CAPI */
337
338 static stcxt_t Context;
339 static stcxt_t *Context_ptr = &Context;
340 #define dSTCXT                  stcxt_t *cxt = Context_ptr
341 #define INIT_STCXT              dSTCXT
342 #define SET_STCXT(x)    Context_ptr = x
343
344 #endif /* MULTIPLICITY || PERL_OBJECT || PERL_CAPI */
345
346 /*
347  * KNOWN BUG:
348  *   Croaking implies a memory leak, since we don't use setjmp/longjmp
349  *   to catch the exit and free memory used during store or retrieve
350  *   operations.  This is not too difficult to fix, but I need to understand
351  *   how Perl does it, and croaking is exceptional anyway, so I lack the
352  *   motivation to do it.
353  *
354  * The current workaround is to mark the context as dirty when croaking,
355  * so that data structures can be freed whenever we renter Storable code
356  * (but only *then*: it's a workaround, not a fix).
357  *
358  * This is also imperfect, because we don't really know how far they trapped
359  * the croak(), and when we were recursing, we won't be able to clean anything
360  * but the topmost context stacked.
361  */
362
363 #define CROAK(x)        do { cxt->s_dirty = 1; croak x; } while (0)
364
365 /*
366  * End of "thread-safe" related definitions.
367  */
368
369 /*
370  * LOW_32BITS
371  *
372  * Keep only the low 32 bits of a pointer (used for tags, which are not
373  * really pointers).
374  */
375
376 #if PTRSIZE <= 4
377 #define LOW_32BITS(x)   ((I32) (x))
378 #else
379 #define LOW_32BITS(x)   ((I32) ((unsigned long) (x) & 0xffffffffUL))
380 #endif
381
382 /*
383  * oI, oS, oC
384  *
385  * Hack for Crays, where sizeof(I32) == 8, and which are big-endians.
386  * Used in the WLEN and RLEN macros.
387  */
388
389 #if INTSIZE > 4
390 #define oI(x)   ((I32 *) ((char *) (x) + 4))
391 #define oS(x)   ((x) - 4)
392 #define oC(x)   (x = 0)
393 #define CRAY_HACK
394 #else
395 #define oI(x)   (x)
396 #define oS(x)   (x)
397 #define oC(x)
398 #endif
399
400 /*
401  * key buffer handling
402  */
403 #define kbuf    (cxt->keybuf).arena
404 #define ksiz    (cxt->keybuf).asiz
405 #define KBUFINIT() do {                                 \
406         if (!kbuf) {                                            \
407                 TRACEME(("** allocating kbuf of 128 bytes")); \
408                 New(10003, kbuf, 128, char);    \
409                 ksiz = 128;                                             \
410         }                                                                       \
411 } while (0)
412 #define KBUFCHK(x) do {                 \
413         if (x >= ksiz) {                        \
414                 TRACEME(("** extending kbuf to %d bytes (had %d)", x+1, ksiz)); \
415                 Renew(kbuf, x+1, char); \
416                 ksiz = x+1;                             \
417         }                                                       \
418 } while (0)
419
420 /*
421  * memory buffer handling
422  */
423 #define mbase   (cxt->membuf).arena
424 #define msiz    (cxt->membuf).asiz
425 #define mptr    (cxt->membuf).aptr
426 #define mend    (cxt->membuf).aend
427
428 #define MGROW   (1 << 13)
429 #define MMASK   (MGROW - 1)
430
431 #define round_mgrow(x)  \
432         ((unsigned long) (((unsigned long) (x) + MMASK) & ~MMASK))
433 #define trunc_int(x)    \
434         ((unsigned long) ((unsigned long) (x) & ~(sizeof(int)-1)))
435 #define int_aligned(x)  \
436         ((unsigned long) (x) == trunc_int(x))
437
438 #define MBUF_INIT(x) do {                               \
439         if (!mbase) {                                           \
440                 TRACEME(("** allocating mbase of %d bytes", MGROW)); \
441                 New(10003, mbase, MGROW, char); \
442                 msiz = MGROW;                                   \
443         }                                                                       \
444         mptr = mbase;                                           \
445         if (x)                                                          \
446                 mend = mbase + x;                               \
447         else                                                            \
448                 mend = mbase + msiz;                    \
449 } while (0)
450
451 #define MBUF_TRUNC(x)   mptr = mbase + x
452 #define MBUF_SIZE()             (mptr - mbase)
453
454 /*
455  * MBUF_SAVE_AND_LOAD
456  * MBUF_RESTORE
457  *
458  * Those macros are used in do_retrieve() to save the current memory
459  * buffer into cxt->msaved, before MBUF_LOAD() can be used to retrieve
460  * data from a string.
461  */
462 #define MBUF_SAVE_AND_LOAD(in) do {             \
463         ASSERT(!cxt->membuf_ro, ("mbase not already saved")); \
464         cxt->membuf_ro = 1;                                     \
465         TRACEME(("saving mbuf"));                       \
466         StructCopy(&cxt->membuf, &cxt->msaved, struct extendable); \
467         MBUF_LOAD(in);                                          \
468 } while (0)
469
470 #define MBUF_RESTORE() do {                             \
471         ASSERT(cxt->membuf_ro, ("mbase is read-only")); \
472         cxt->membuf_ro = 0;                                     \
473         TRACEME(("restoring mbuf"));            \
474         StructCopy(&cxt->msaved, &cxt->membuf, struct extendable); \
475 } while (0)
476
477 /*
478  * Use SvPOKp(), because SvPOK() fails on tainted scalars.
479  * See store_scalar() for other usage of this workaround.
480  */
481 #define MBUF_LOAD(v) do {                               \
482         ASSERT(cxt->membuf_ro, ("mbase is read-only")); \
483         if (!SvPOKp(v))                                         \
484                 CROAK(("Not a scalar string")); \
485         mptr = mbase = SvPV(v, msiz);           \
486         mend = mbase + msiz;                            \
487 } while (0)
488
489 #define MBUF_XTEND(x) do {                      \
490         int nsz = (int) round_mgrow((x)+msiz);  \
491         int offset = mptr - mbase;              \
492         ASSERT(!cxt->membuf_ro, ("mbase is not read-only")); \
493         TRACEME(("** extending mbase from %d to %d bytes (wants %d new)", \
494                 msiz, nsz, (x)));                       \
495         Renew(mbase, nsz, char);                \
496         msiz = nsz;                                             \
497         mptr = mbase + offset;                  \
498         mend = mbase + nsz;                             \
499 } while (0)
500
501 #define MBUF_CHK(x) do {                        \
502         if ((mptr + (x)) > mend)                \
503                 MBUF_XTEND(x);                          \
504 } while (0)
505
506 #define MBUF_GETC(x) do {                       \
507         if (mptr < mend)                                \
508                 x = (int) (unsigned char) *mptr++;      \
509         else                                                    \
510                 return (SV *) 0;                        \
511 } while (0)
512
513 #ifdef CRAY_HACK
514 #define MBUF_GETINT(x) do {                             \
515         oC(x);                                                          \
516         if ((mptr + 4) <= mend) {                       \
517                 memcpy(oI(&x), mptr, 4);                \
518                 mptr += 4;                                              \
519         } else                                                          \
520                 return (SV *) 0;                                \
521 } while (0)
522 #else
523 #define MBUF_GETINT(x) do {                             \
524         if ((mptr + sizeof(int)) <= mend) {     \
525                 if (int_aligned(mptr))                  \
526                         x = *(int *) mptr;                      \
527                 else                                                    \
528                         memcpy(&x, mptr, sizeof(int));  \
529                 mptr += sizeof(int);                    \
530         } else                                                          \
531                 return (SV *) 0;                                \
532 } while (0)
533 #endif
534
535 #define MBUF_READ(x,s) do {                     \
536         if ((mptr + (s)) <= mend) {             \
537                 memcpy(x, mptr, s);                     \
538                 mptr += s;                                      \
539         } else                                                  \
540                 return (SV *) 0;                        \
541 } while (0)
542
543 #define MBUF_SAFEREAD(x,s,z) do {       \
544         if ((mptr + (s)) <= mend) {             \
545                 memcpy(x, mptr, s);                     \
546                 mptr += s;                                      \
547         } else {                                                \
548                 sv_free(z);                                     \
549                 return (SV *) 0;                        \
550         }                                                               \
551 } while (0)
552
553 #define MBUF_PUTC(c) do {                       \
554         if (mptr < mend)                                \
555                 *mptr++ = (char) c;                     \
556         else {                                                  \
557                 MBUF_XTEND(1);                          \
558                 *mptr++ = (char) c;                     \
559         }                                                               \
560 } while (0)
561
562 #ifdef CRAY_HACK
563 #define MBUF_PUTINT(i) do {                     \
564         MBUF_CHK(4);                                    \
565         memcpy(mptr, oI(&i), 4);                \
566         mptr += 4;                                              \
567 } while (0)
568 #else
569 #define MBUF_PUTINT(i) do {                     \
570         MBUF_CHK(sizeof(int));                  \
571         if (int_aligned(mptr))                  \
572                 *(int *) mptr = i;                      \
573         else                                                    \
574                 memcpy(mptr, &i, sizeof(int));  \
575         mptr += sizeof(int);                    \
576 } while (0)
577 #endif
578
579 #define MBUF_WRITE(x,s) do {            \
580         MBUF_CHK(s);                                    \
581         memcpy(mptr, x, s);                             \
582         mptr += s;                                              \
583 } while (0)
584
585 /*
586  * Possible return values for sv_type().
587  */
588
589 #define svis_REF                0
590 #define svis_SCALAR             1
591 #define svis_ARRAY              2
592 #define svis_HASH               3
593 #define svis_TIED               4
594 #define svis_TIED_ITEM  5
595 #define svis_OTHER              6
596
597 /*
598  * Flags for SX_HOOK.
599  */
600
601 #define SHF_TYPE_MASK           0x03
602 #define SHF_LARGE_CLASSLEN      0x04
603 #define SHF_LARGE_STRLEN        0x08
604 #define SHF_LARGE_LISTLEN       0x10
605 #define SHF_IDX_CLASSNAME       0x20
606 #define SHF_NEED_RECURSE        0x40
607 #define SHF_HAS_LIST            0x80
608
609 /*
610  * Types for SX_HOOK (last 2 bits in flags).
611  */
612
613 #define SHT_SCALAR                      0
614 #define SHT_ARRAY                       1
615 #define SHT_HASH                        2
616 #define SHT_EXTRA                       3               /* Read extra byte for type */
617
618 /*
619  * The following are held in the "extra byte"...
620  */
621
622 #define SHT_TSCALAR                     4               /* 4 + 0 -- tied scalar */
623 #define SHT_TARRAY                      5               /* 4 + 1 -- tied array */
624 #define SHT_THASH                       6               /* 4 + 2 -- tied hash */
625
626 /*
627  * per hash flags for flagged hashes
628  */
629
630 #define SHV_RESTRICTED          0x01
631
632 /*
633  * per key flags for flagged hashes
634  */
635
636 #define SHV_K_UTF8              0x01
637 #define SHV_K_WASUTF8           0x02
638 #define SHV_K_LOCKED            0x04
639 #define SHV_K_ISSV              0x08
640 #define SHV_K_PLACEHOLDER       0x10
641
642 /*
643  * Before 0.6, the magic string was "perl-store" (binary version number 0).
644  *
645  * Since 0.6 introduced many binary incompatibilities, the magic string has
646  * been changed to "pst0" to allow an old image to be properly retrieved by
647  * a newer Storable, but ensure a newer image cannot be retrieved with an
648  * older version.
649  *
650  * At 0.7, objects are given the ability to serialize themselves, and the
651  * set of markers is extended, backward compatibility is not jeopardized,
652  * so the binary version number could have remained unchanged.  To correctly
653  * spot errors if a file making use of 0.7-specific extensions is given to
654  * 0.6 for retrieval, the binary version was moved to "2".  And I'm introducing
655  * a "minor" version, to better track this kind of evolution from now on.
656  * 
657  */
658 static char old_magicstr[] = "perl-store";      /* Magic number before 0.6 */
659 static char magicstr[] = "pst0";                        /* Used as a magic number */
660
661 #define STORABLE_BIN_MAJOR      2               /* Binary major "version" */
662 #if (PATCHLEVEL <= 6)
663 #define STORABLE_BIN_MINOR      4               /* Binary minor "version" */
664 #else 
665 /* 
666  * As of perl 5.7.3, utf8 hash key is introduced.
667  * So this must change -- dankogai
668 */
669 #define STORABLE_BIN_MINOR      5               /* Binary minor "version" */
670 #endif /* (PATCHLEVEL <= 6) */
671
672 /*
673  * Useful store shortcuts...
674  */
675
676 #define PUTMARK(x) do {                                         \
677         if (!cxt->fio)                                                  \
678                 MBUF_PUTC(x);                                           \
679         else if (PerlIO_putc(cxt->fio, x) == EOF)       \
680                 return -1;                                                      \
681 } while (0)
682
683 #define WRITE_I32(x)    do {                    \
684         ASSERT(sizeof(x) == sizeof(I32), ("writing an I32"));   \
685         if (!cxt->fio)                                          \
686                 MBUF_PUTINT(x);                                 \
687         else if (PerlIO_write(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
688                 return -1;                                      \
689         } while (0)
690
691 #ifdef HAS_HTONL
692 #define WLEN(x) do {                            \
693         if (cxt->netorder) {                    \
694                 int y = (int) htonl(x);         \
695                 if (!cxt->fio)                          \
696                         MBUF_PUTINT(y);                 \
697                 else if (PerlIO_write(cxt->fio,oI(&y),oS(sizeof(y))) != oS(sizeof(y))) \
698                         return -1;                              \
699         } else {                                                \
700                 if (!cxt->fio)                          \
701                         MBUF_PUTINT(x);                 \
702                 else if (PerlIO_write(cxt->fio,oI(&x),oS(sizeof(x))) != oS(sizeof(x))) \
703                         return -1;                              \
704         }                                                               \
705 } while (0)
706 #else
707 #define WLEN(x) WRITE_I32(x)
708 #endif
709
710 #define WRITE(x,y) do {                                         \
711         if (!cxt->fio)                                                  \
712                 MBUF_WRITE(x,y);                                        \
713         else if (PerlIO_write(cxt->fio, x, y) != y)     \
714                 return -1;                                                      \
715         } while (0)
716
717 #define STORE_PV_LEN(pv, len, small, large) do {        \
718         if (len <= LG_SCALAR) {                         \
719                 unsigned char clen = (unsigned char) len;       \
720                 PUTMARK(small);                                 \
721                 PUTMARK(clen);                                  \
722                 if (len)                                                \
723                         WRITE(pv, len);                         \
724         } else {                                                        \
725                 PUTMARK(large);                                 \
726                 WLEN(len);                                              \
727                 WRITE(pv, len);                                 \
728         }                                                                       \
729 } while (0)
730
731 #define STORE_SCALAR(pv, len)   STORE_PV_LEN(pv, len, SX_SCALAR, SX_LSCALAR)
732
733 /*
734  * Conditional UTF8 support.
735  * On non-UTF8 perls, UTF8 strings are returned as normal strings.
736  *
737  */
738 #ifdef SvUTF8_on
739 #define STORE_UTF8STR(pv, len)  STORE_PV_LEN(pv, len, SX_UTF8STR, SX_LUTF8STR)
740 #else
741 #define SvUTF8(sv) 0
742 #define STORE_UTF8STR(pv, len) CROAK(("panic: storing UTF8 in non-UTF8 perl"))
743 #define SvUTF8_on(sv) CROAK(("Cannot retrieve UTF8 data in non-UTF8 perl"))
744 #endif
745
746 /*
747  * Store undef in arrays and hashes without recursing through store().
748  */
749 #define STORE_UNDEF() do {                              \
750         cxt->tagnum++;                                          \
751         PUTMARK(SX_UNDEF);                                      \
752 } while (0)
753
754 /*
755  * Useful retrieve shortcuts...
756  */
757
758 #define GETCHAR() \
759         (cxt->fio ? PerlIO_getc(cxt->fio) : (mptr >= mend ? EOF : (int) *mptr++))
760
761 #define GETMARK(x) do {                                                 \
762         if (!cxt->fio)                                                          \
763                 MBUF_GETC(x);                                                   \
764         else if ((int) (x = PerlIO_getc(cxt->fio)) == EOF)      \
765                 return (SV *) 0;                                                \
766 } while (0)
767
768 #define READ_I32(x)     do {                            \
769         ASSERT(sizeof(x) == sizeof(I32), ("reading an I32"));   \
770         oC(x);                                                          \
771         if (!cxt->fio)                                          \
772                 MBUF_GETINT(x);                                 \
773         else if (PerlIO_read(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
774                 return (SV *) 0;                                \
775 } while (0)
776
777 #ifdef HAS_NTOHL
778 #define RLEN(x) do {                                    \
779         oC(x);                                                          \
780         if (!cxt->fio)                                          \
781                 MBUF_GETINT(x);                                 \
782         else if (PerlIO_read(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
783                 return (SV *) 0;                                \
784         if (cxt->netorder)                                      \
785                 x = (int) ntohl(x);                             \
786 } while (0)
787 #else
788 #define RLEN(x) READ_I32(x)
789 #endif
790
791 #define READ(x,y) do {                                          \
792         if (!cxt->fio)                                                  \
793                 MBUF_READ(x, y);                                        \
794         else if (PerlIO_read(cxt->fio, x, y) != y)      \
795                 return (SV *) 0;                                        \
796 } while (0)
797
798 #define SAFEREAD(x,y,z) do {                                    \
799         if (!cxt->fio)                                                          \
800                 MBUF_SAFEREAD(x,y,z);                                   \
801         else if (PerlIO_read(cxt->fio, x, y) != y)       {      \
802                 sv_free(z);                                                             \
803                 return (SV *) 0;                                                \
804         }                                                                                       \
805 } while (0)
806
807 /*
808  * This macro is used at retrieve time, to remember where object 'y', bearing a
809  * given tag 'tagnum', has been retrieved. Next time we see an SX_OBJECT marker,
810  * we'll therefore know where it has been retrieved and will be able to
811  * share the same reference, as in the original stored memory image.
812  *
813  * We also need to bless objects ASAP for hooks (which may compute "ref $x"
814  * on the objects given to STORABLE_thaw and expect that to be defined), and
815  * also for overloaded objects (for which we might not find the stash if the
816  * object is not blessed yet--this might occur for overloaded objects that
817  * refer to themselves indirectly: if we blessed upon return from a sub
818  * retrieve(), the SX_OBJECT marker we'd found could not have overloading
819  * restored on it because the underlying object would not be blessed yet!).
820  *
821  * To achieve that, the class name of the last retrieved object is passed down
822  * recursively, and the first SEEN() call for which the class name is not NULL
823  * will bless the object.
824  */
825 #define SEEN(y,c) do {                                          \
826         if (!y)                                                                 \
827                 return (SV *) 0;                                        \
828         if (av_store(cxt->aseen, cxt->tagnum++, SvREFCNT_inc(y)) == 0) \
829                 return (SV *) 0;                                        \
830         TRACEME(("aseen(#%d) = 0x%"UVxf" (refcnt=%d)", cxt->tagnum-1, \
831                  PTR2UV(y), SvREFCNT(y)-1));            \
832         if (c)                                                                  \
833                 BLESS((SV *) (y), c);                           \
834 } while (0)
835
836 /*
837  * Bless `s' in `p', via a temporary reference, required by sv_bless().
838  */
839 #define BLESS(s,p) do {                                 \
840         SV *ref;                                                                \
841         HV *stash;                                                              \
842         TRACEME(("blessing 0x%"UVxf" in %s", PTR2UV(s), (p))); \
843         stash = gv_stashpv((p), TRUE);                  \
844         ref = newRV_noinc(s);                                   \
845         (void) sv_bless(ref, stash);                    \
846         SvRV(ref) = 0;                                                  \
847         SvREFCNT_dec(ref);                                              \
848 } while (0)
849
850 static int store();
851 static SV *retrieve(stcxt_t *cxt, char *cname);
852
853 /*
854  * Dynamic dispatching table for SV store.
855  */
856
857 static int store_ref(stcxt_t *cxt, SV *sv);
858 static int store_scalar(stcxt_t *cxt, SV *sv);
859 static int store_array(stcxt_t *cxt, AV *av);
860 static int store_hash(stcxt_t *cxt, HV *hv);
861 static int store_tied(stcxt_t *cxt, SV *sv);
862 static int store_tied_item(stcxt_t *cxt, SV *sv);
863 static int store_other(stcxt_t *cxt, SV *sv);
864 static int store_blessed(stcxt_t *cxt, SV *sv, int type, HV *pkg);
865
866 static int (*sv_store[])(stcxt_t *cxt, SV *sv) = {
867         store_ref,                                                                              /* svis_REF */
868         store_scalar,                                                                   /* svis_SCALAR */
869         (int (*)(stcxt_t *cxt, SV *sv)) store_array,    /* svis_ARRAY */
870         (int (*)(stcxt_t *cxt, SV *sv)) store_hash,             /* svis_HASH */
871         store_tied,                                                                             /* svis_TIED */
872         store_tied_item,                                                                /* svis_TIED_ITEM */
873         store_other,                                                                    /* svis_OTHER */
874 };
875
876 #define SV_STORE(x)     (*sv_store[x])
877
878 /*
879  * Dynamic dispatching tables for SV retrieval.
880  */
881
882 static SV *retrieve_lscalar(stcxt_t *cxt, char *cname);
883 static SV *retrieve_lutf8str(stcxt_t *cxt, char *cname);
884 static SV *old_retrieve_array(stcxt_t *cxt, char *cname);
885 static SV *old_retrieve_hash(stcxt_t *cxt, char *cname);
886 static SV *retrieve_ref(stcxt_t *cxt, char *cname);
887 static SV *retrieve_undef(stcxt_t *cxt, char *cname);
888 static SV *retrieve_integer(stcxt_t *cxt, char *cname);
889 static SV *retrieve_double(stcxt_t *cxt, char *cname);
890 static SV *retrieve_byte(stcxt_t *cxt, char *cname);
891 static SV *retrieve_netint(stcxt_t *cxt, char *cname);
892 static SV *retrieve_scalar(stcxt_t *cxt, char *cname);
893 static SV *retrieve_utf8str(stcxt_t *cxt, char *cname);
894 static SV *retrieve_tied_array(stcxt_t *cxt, char *cname);
895 static SV *retrieve_tied_hash(stcxt_t *cxt, char *cname);
896 static SV *retrieve_tied_scalar(stcxt_t *cxt, char *cname);
897 static SV *retrieve_other(stcxt_t *cxt, char *cname);
898
899 static SV *(*sv_old_retrieve[])(stcxt_t *cxt, char *cname) = {
900         0,                      /* SX_OBJECT -- entry unused dynamically */
901         retrieve_lscalar,               /* SX_LSCALAR */
902         old_retrieve_array,             /* SX_ARRAY -- for pre-0.6 binaries */
903         old_retrieve_hash,              /* SX_HASH -- for pre-0.6 binaries */
904         retrieve_ref,                   /* SX_REF */
905         retrieve_undef,                 /* SX_UNDEF */
906         retrieve_integer,               /* SX_INTEGER */
907         retrieve_double,                /* SX_DOUBLE */
908         retrieve_byte,                  /* SX_BYTE */
909         retrieve_netint,                /* SX_NETINT */
910         retrieve_scalar,                /* SX_SCALAR */
911         retrieve_tied_array,    /* SX_ARRAY */
912         retrieve_tied_hash,             /* SX_HASH */
913         retrieve_tied_scalar,   /* SX_SCALAR */
914         retrieve_other,                 /* SX_SV_UNDEF not supported */
915         retrieve_other,                 /* SX_SV_YES not supported */
916         retrieve_other,                 /* SX_SV_NO not supported */
917         retrieve_other,                 /* SX_BLESS not supported */
918         retrieve_other,                 /* SX_IX_BLESS not supported */
919         retrieve_other,                 /* SX_HOOK not supported */
920         retrieve_other,                 /* SX_OVERLOADED not supported */
921         retrieve_other,                 /* SX_TIED_KEY not supported */
922         retrieve_other,                 /* SX_TIED_IDX not supported */
923         retrieve_other,                 /* SX_UTF8STR not supported */
924         retrieve_other,                 /* SX_LUTF8STR not supported */
925         retrieve_other,                 /* SX_FLAG_HASH not supported */
926         retrieve_other,                 /* SX_ERROR */
927 };
928
929 static SV *retrieve_array(stcxt_t *cxt, char *cname);
930 static SV *retrieve_hash(stcxt_t *cxt, char *cname);
931 static SV *retrieve_sv_undef(stcxt_t *cxt, char *cname);
932 static SV *retrieve_sv_yes(stcxt_t *cxt, char *cname);
933 static SV *retrieve_sv_no(stcxt_t *cxt, char *cname);
934 static SV *retrieve_blessed(stcxt_t *cxt, char *cname);
935 static SV *retrieve_idx_blessed(stcxt_t *cxt, char *cname);
936 static SV *retrieve_hook(stcxt_t *cxt, char *cname);
937 static SV *retrieve_overloaded(stcxt_t *cxt, char *cname);
938 static SV *retrieve_tied_key(stcxt_t *cxt, char *cname);
939 static SV *retrieve_tied_idx(stcxt_t *cxt, char *cname);
940 static SV *retrieve_flag_hash(stcxt_t *cxt, char *cname);
941
942 static SV *(*sv_retrieve[])(stcxt_t *cxt, char *cname) = {
943         0,                      /* SX_OBJECT -- entry unused dynamically */
944         retrieve_lscalar,               /* SX_LSCALAR */
945         retrieve_array,                 /* SX_ARRAY */
946         retrieve_hash,                  /* SX_HASH */
947         retrieve_ref,                   /* SX_REF */
948         retrieve_undef,                 /* SX_UNDEF */
949         retrieve_integer,               /* SX_INTEGER */
950         retrieve_double,                /* SX_DOUBLE */
951         retrieve_byte,                  /* SX_BYTE */
952         retrieve_netint,                /* SX_NETINT */
953         retrieve_scalar,                /* SX_SCALAR */
954         retrieve_tied_array,    /* SX_ARRAY */
955         retrieve_tied_hash,             /* SX_HASH */
956         retrieve_tied_scalar,   /* SX_SCALAR */
957         retrieve_sv_undef,              /* SX_SV_UNDEF */
958         retrieve_sv_yes,                /* SX_SV_YES */
959         retrieve_sv_no,                 /* SX_SV_NO */
960         retrieve_blessed,               /* SX_BLESS */
961         retrieve_idx_blessed,   /* SX_IX_BLESS */
962         retrieve_hook,                  /* SX_HOOK */
963         retrieve_overloaded,    /* SX_OVERLOAD */
964         retrieve_tied_key,              /* SX_TIED_KEY */
965         retrieve_tied_idx,              /* SX_TIED_IDX */
966         retrieve_utf8str,               /* SX_UTF8STR  */
967         retrieve_lutf8str,              /* SX_LUTF8STR */
968         retrieve_flag_hash,             /* SX_HASH */
969         retrieve_other,                 /* SX_ERROR */
970 };
971
972 #define RETRIEVE(c,x) (*(c)->retrieve_vtbl[(x) >= SX_ERROR ? SX_ERROR : (x)])
973
974 static SV *mbuf2sv(void);
975
976 /***
977  *** Context management.
978  ***/
979
980 /*
981  * init_perinterp
982  *
983  * Called once per "thread" (interpreter) to initialize some global context.
984  */
985 static void init_perinterp(void)
986 {
987     INIT_STCXT;
988
989     cxt->netorder = 0;          /* true if network order used */
990     cxt->forgive_me = -1;       /* whether to be forgiving... */
991 }
992
993 /*
994  * reset_context
995  *
996  * Called at the end of every context cleaning, to perform common reset
997  * operations.
998  */
999 static void reset_context(stcxt_t *cxt)
1000 {
1001         cxt->entry = 0;
1002         cxt->s_dirty = 0;
1003         cxt->optype &= ~(ST_STORE|ST_RETRIEVE);         /* Leave ST_CLONE alone */
1004 }
1005
1006 /*
1007  * init_store_context
1008  *
1009  * Initialize a new store context for real recursion.
1010  */
1011 static void init_store_context(
1012         stcxt_t *cxt,
1013         PerlIO *f,
1014         int optype,
1015         int network_order)
1016 {
1017         TRACEME(("init_store_context"));
1018
1019         cxt->netorder = network_order;
1020         cxt->forgive_me = -1;                   /* Fetched from perl if needed */
1021         cxt->canonical = -1;                    /* Idem */
1022         cxt->tagnum = -1;                               /* Reset tag numbers */
1023         cxt->classnum = -1;                             /* Reset class numbers */
1024         cxt->fio = f;                                   /* Where I/O are performed */
1025         cxt->optype = optype;                   /* A store, or a deep clone */
1026         cxt->entry = 1;                                 /* No recursion yet */
1027
1028         /*
1029          * The `hseen' table is used to keep track of each SV stored and their
1030          * associated tag numbers is special. It is "abused" because the
1031          * values stored are not real SV, just integers cast to (SV *),
1032          * which explains the freeing below.
1033          *
1034          * It is also one possible bottlneck to achieve good storing speed,
1035          * so the "shared keys" optimization is turned off (unlikely to be
1036          * of any use here), and the hash table is "pre-extended". Together,
1037          * those optimizations increase the throughput by 12%.
1038          */
1039
1040         cxt->hseen = newHV();                   /* Table where seen objects are stored */
1041         HvSHAREKEYS_off(cxt->hseen);
1042
1043         /*
1044          * The following does not work well with perl5.004_04, and causes
1045          * a core dump later on, in a completely unrelated spot, which
1046          * makes me think there is a memory corruption going on.
1047          *
1048          * Calling hv_ksplit(hseen, HBUCKETS) instead of manually hacking
1049          * it below does not make any difference. It seems to work fine
1050          * with perl5.004_68 but given the probable nature of the bug,
1051          * that does not prove anything.
1052          *
1053          * It's a shame because increasing the amount of buckets raises
1054          * store() throughput by 5%, but until I figure this out, I can't
1055          * allow for this to go into production.
1056          *
1057          * It is reported fixed in 5.005, hence the #if.
1058          */
1059 #if PERL_VERSION >= 5
1060 #define HBUCKETS        4096                            /* Buckets for %hseen */
1061         HvMAX(cxt->hseen) = HBUCKETS - 1;       /* keys %hseen = $HBUCKETS; */
1062 #endif
1063
1064         /*
1065          * The `hclass' hash uses the same settings as `hseen' above, but it is
1066          * used to assign sequential tags (numbers) to class names for blessed
1067          * objects.
1068          *
1069          * We turn the shared key optimization on.
1070          */
1071
1072         cxt->hclass = newHV();                  /* Where seen classnames are stored */
1073
1074 #if PERL_VERSION >= 5
1075         HvMAX(cxt->hclass) = HBUCKETS - 1;      /* keys %hclass = $HBUCKETS; */
1076 #endif
1077
1078         /*
1079          * The `hook' hash table is used to keep track of the references on
1080          * the STORABLE_freeze hook routines, when found in some class name.
1081          *
1082          * It is assumed that the inheritance tree will not be changed during
1083          * storing, and that no new method will be dynamically created by the
1084          * hooks.
1085          */
1086
1087         cxt->hook = newHV();                    /* Table where hooks are cached */
1088
1089         /*
1090          * The `hook_seen' array keeps track of all the SVs returned by
1091          * STORABLE_freeze hooks for us to serialize, so that they are not
1092          * reclaimed until the end of the serialization process.  Each SV is
1093          * only stored once, the first time it is seen.
1094          */
1095
1096         cxt->hook_seen = newAV();               /* Lists SVs returned by STORABLE_freeze */
1097 }
1098
1099 /*
1100  * clean_store_context
1101  *
1102  * Clean store context by
1103  */
1104 static void clean_store_context(stcxt_t *cxt)
1105 {
1106         HE *he;
1107
1108         TRACEME(("clean_store_context"));
1109
1110         ASSERT(cxt->optype & ST_STORE, ("was performing a store()"));
1111
1112         /*
1113          * Insert real values into hashes where we stored faked pointers.
1114          */
1115
1116         if (cxt->hseen) {
1117                 hv_iterinit(cxt->hseen);
1118                 while ((he = hv_iternext(cxt->hseen)))  /* Extra () for -Wall, grr.. */
1119                         HeVAL(he) = &PL_sv_undef;
1120         }
1121
1122         if (cxt->hclass) {
1123                 hv_iterinit(cxt->hclass);
1124                 while ((he = hv_iternext(cxt->hclass))) /* Extra () for -Wall, grr.. */
1125                         HeVAL(he) = &PL_sv_undef;
1126         }
1127
1128         /*
1129          * And now dispose of them...
1130          *
1131          * The surrounding if() protection has been added because there might be
1132          * some cases where this routine is called more than once, during
1133          * exceptionnal events.  This was reported by Marc Lehmann when Storable
1134          * is executed from mod_perl, and the fix was suggested by him.
1135          *              -- RAM, 20/12/2000
1136          */
1137
1138         if (cxt->hseen) {
1139                 HV *hseen = cxt->hseen;
1140                 cxt->hseen = 0;
1141                 hv_undef(hseen);
1142                 sv_free((SV *) hseen);
1143         }
1144
1145         if (cxt->hclass) {
1146                 HV *hclass = cxt->hclass;
1147                 cxt->hclass = 0;
1148                 hv_undef(hclass);
1149                 sv_free((SV *) hclass);
1150         }
1151
1152         if (cxt->hook) {
1153                 HV *hook = cxt->hook;
1154                 cxt->hook = 0;
1155                 hv_undef(hook);
1156                 sv_free((SV *) hook);
1157         }
1158
1159         if (cxt->hook_seen) {
1160                 AV *hook_seen = cxt->hook_seen;
1161                 cxt->hook_seen = 0;
1162                 av_undef(hook_seen);
1163                 sv_free((SV *) hook_seen);
1164         }
1165
1166         reset_context(cxt);
1167 }
1168
1169 /*
1170  * init_retrieve_context
1171  *
1172  * Initialize a new retrieve context for real recursion.
1173  */
1174 static void init_retrieve_context(stcxt_t *cxt, int optype, int is_tainted)
1175 {
1176         TRACEME(("init_retrieve_context"));
1177
1178         /*
1179          * The hook hash table is used to keep track of the references on
1180          * the STORABLE_thaw hook routines, when found in some class name.
1181          *
1182          * It is assumed that the inheritance tree will not be changed during
1183          * storing, and that no new method will be dynamically created by the
1184          * hooks.
1185          */
1186
1187         cxt->hook  = newHV();                   /* Caches STORABLE_thaw */
1188
1189         /*
1190          * If retrieving an old binary version, the cxt->retrieve_vtbl variable
1191          * was set to sv_old_retrieve. We'll need a hash table to keep track of
1192          * the correspondance between the tags and the tag number used by the
1193          * new retrieve routines.
1194          */
1195
1196         cxt->hseen = ((cxt->retrieve_vtbl == sv_old_retrieve) ? newHV() : 0);
1197
1198         cxt->aseen = newAV();                   /* Where retrieved objects are kept */
1199         cxt->aclass = newAV();                  /* Where seen classnames are kept */
1200         cxt->tagnum = 0;                                /* Have to count objects... */
1201         cxt->classnum = 0;                              /* ...and class names as well */
1202         cxt->optype = optype;
1203         cxt->s_tainted = is_tainted;
1204         cxt->entry = 1;                                 /* No recursion yet */
1205 }
1206
1207 /*
1208  * clean_retrieve_context
1209  *
1210  * Clean retrieve context by
1211  */
1212 static void clean_retrieve_context(stcxt_t *cxt)
1213 {
1214         TRACEME(("clean_retrieve_context"));
1215
1216         ASSERT(cxt->optype & ST_RETRIEVE, ("was performing a retrieve()"));
1217
1218         if (cxt->aseen) {
1219                 AV *aseen = cxt->aseen;
1220                 cxt->aseen = 0;
1221                 av_undef(aseen);
1222                 sv_free((SV *) aseen);
1223         }
1224
1225         if (cxt->aclass) {
1226                 AV *aclass = cxt->aclass;
1227                 cxt->aclass = 0;
1228                 av_undef(aclass);
1229                 sv_free((SV *) aclass);
1230         }
1231
1232         if (cxt->hook) {
1233                 HV *hook = cxt->hook;
1234                 cxt->hook = 0;
1235                 hv_undef(hook);
1236                 sv_free((SV *) hook);
1237         }
1238
1239         if (cxt->hseen) {
1240                 HV *hseen = cxt->hseen;
1241                 cxt->hseen = 0;
1242                 hv_undef(hseen);
1243                 sv_free((SV *) hseen);          /* optional HV, for backward compat. */
1244         }
1245
1246         reset_context(cxt);
1247 }
1248
1249 /*
1250  * clean_context
1251  *
1252  * A workaround for the CROAK bug: cleanup the last context.
1253  */
1254 static void clean_context(stcxt_t *cxt)
1255 {
1256         TRACEME(("clean_context"));
1257
1258         ASSERT(cxt->s_dirty, ("dirty context"));
1259
1260         if (cxt->membuf_ro)
1261                 MBUF_RESTORE();
1262
1263         ASSERT(!cxt->membuf_ro, ("mbase is not read-only"));
1264
1265         if (cxt->optype & ST_RETRIEVE)
1266                 clean_retrieve_context(cxt);
1267         else if (cxt->optype & ST_STORE)
1268                 clean_store_context(cxt);
1269         else
1270                 reset_context(cxt);
1271
1272         ASSERT(!cxt->s_dirty, ("context is clean"));
1273         ASSERT(cxt->entry == 0, ("context is reset"));
1274 }
1275
1276 /*
1277  * allocate_context
1278  *
1279  * Allocate a new context and push it on top of the parent one.
1280  * This new context is made globally visible via SET_STCXT().
1281  */
1282 static stcxt_t *allocate_context(parent_cxt)
1283 stcxt_t *parent_cxt;
1284 {
1285         stcxt_t *cxt;
1286
1287         TRACEME(("allocate_context"));
1288
1289         ASSERT(!parent_cxt->s_dirty, ("parent context clean"));
1290
1291         Newz(0, cxt, 1, stcxt_t);
1292         cxt->prev = parent_cxt;
1293         SET_STCXT(cxt);
1294
1295         ASSERT(!cxt->s_dirty, ("clean context"));
1296
1297         return cxt;
1298 }
1299
1300 /*
1301  * free_context
1302  *
1303  * Free current context, which cannot be the "root" one.
1304  * Make the context underneath globally visible via SET_STCXT().
1305  */
1306 static void free_context(cxt)
1307 stcxt_t *cxt;
1308 {
1309         stcxt_t *prev = cxt->prev;
1310
1311         TRACEME(("free_context"));
1312
1313         ASSERT(!cxt->s_dirty, ("clean context"));
1314         ASSERT(prev, ("not freeing root context"));
1315
1316         if (kbuf)
1317                 Safefree(kbuf);
1318         if (mbase)
1319                 Safefree(mbase);
1320
1321         Safefree(cxt);
1322         SET_STCXT(prev);
1323
1324         ASSERT(cxt, ("context not void"));
1325 }
1326
1327 /***
1328  *** Predicates.
1329  ***/
1330
1331 /*
1332  * is_storing
1333  *
1334  * Tells whether we're in the middle of a store operation.
1335  */
1336 int is_storing(void)
1337 {
1338         dSTCXT;
1339
1340         return cxt->entry && (cxt->optype & ST_STORE);
1341 }
1342
1343 /*
1344  * is_retrieving
1345  *
1346  * Tells whether we're in the middle of a retrieve operation.
1347  */
1348 int is_retrieving(void)
1349 {
1350         dSTCXT;
1351
1352         return cxt->entry && (cxt->optype & ST_RETRIEVE);
1353 }
1354
1355 /*
1356  * last_op_in_netorder
1357  *
1358  * Returns whether last operation was made using network order.
1359  *
1360  * This is typically out-of-band information that might prove useful
1361  * to people wishing to convert native to network order data when used.
1362  */
1363 int last_op_in_netorder(void)
1364 {
1365         dSTCXT;
1366
1367         return cxt->netorder;
1368 }
1369
1370 /***
1371  *** Hook lookup and calling routines.
1372  ***/
1373
1374 /*
1375  * pkg_fetchmeth
1376  *
1377  * A wrapper on gv_fetchmethod_autoload() which caches results.
1378  *
1379  * Returns the routine reference as an SV*, or null if neither the package
1380  * nor its ancestors know about the method.
1381  */
1382 static SV *pkg_fetchmeth(
1383         HV *cache,
1384         HV *pkg,
1385         char *method)
1386 {
1387         GV *gv;
1388         SV *sv;
1389
1390         /*
1391          * The following code is the same as the one performed by UNIVERSAL::can
1392          * in the Perl core.
1393          */
1394
1395         gv = gv_fetchmethod_autoload(pkg, method, FALSE);
1396         if (gv && isGV(gv)) {
1397                 sv = newRV((SV*) GvCV(gv));
1398                 TRACEME(("%s->%s: 0x%"UVxf, HvNAME(pkg), method, PTR2UV(sv)));
1399         } else {
1400                 sv = newSVsv(&PL_sv_undef);
1401                 TRACEME(("%s->%s: not found", HvNAME(pkg), method));
1402         }
1403
1404         /*
1405          * Cache the result, ignoring failure: if we can't store the value,
1406          * it just won't be cached.
1407          */
1408
1409         (void) hv_store(cache, HvNAME(pkg), strlen(HvNAME(pkg)), sv, 0);
1410
1411         return SvOK(sv) ? sv : (SV *) 0;
1412 }
1413
1414 /*
1415  * pkg_hide
1416  *
1417  * Force cached value to be undef: hook ignored even if present.
1418  */
1419 static void pkg_hide(
1420         HV *cache,
1421         HV *pkg,
1422         char *method)
1423 {
1424         (void) hv_store(cache,
1425                 HvNAME(pkg), strlen(HvNAME(pkg)), newSVsv(&PL_sv_undef), 0);
1426 }
1427
1428 /*
1429  * pkg_uncache
1430  *
1431  * Discard cached value: a whole fetch loop will be retried at next lookup.
1432  */
1433 static void pkg_uncache(
1434         HV *cache,
1435         HV *pkg,
1436         char *method)
1437 {
1438         (void) hv_delete(cache, HvNAME(pkg), strlen(HvNAME(pkg)), G_DISCARD);
1439 }
1440
1441 /*
1442  * pkg_can
1443  *
1444  * Our own "UNIVERSAL::can", which caches results.
1445  *
1446  * Returns the routine reference as an SV*, or null if the object does not
1447  * know about the method.
1448  */
1449 static SV *pkg_can(
1450         HV *cache,
1451         HV *pkg,
1452         char *method)
1453 {
1454         SV **svh;
1455         SV *sv;
1456
1457         TRACEME(("pkg_can for %s->%s", HvNAME(pkg), method));
1458
1459         /*
1460          * Look into the cache to see whether we already have determined
1461          * where the routine was, if any.
1462          *
1463          * NOTA BENE: we don't use `method' at all in our lookup, since we know
1464          * that only one hook (i.e. always the same) is cached in a given cache.
1465          */
1466
1467         svh = hv_fetch(cache, HvNAME(pkg), strlen(HvNAME(pkg)), FALSE);
1468         if (svh) {
1469                 sv = *svh;
1470                 if (!SvOK(sv)) {
1471                         TRACEME(("cached %s->%s: not found", HvNAME(pkg), method));
1472                         return (SV *) 0;
1473                 } else {
1474                         TRACEME(("cached %s->%s: 0x%"UVxf,
1475                                 HvNAME(pkg), method, PTR2UV(sv)));
1476                         return sv;
1477                 }
1478         }
1479
1480         TRACEME(("not cached yet"));
1481         return pkg_fetchmeth(cache, pkg, method);               /* Fetch and cache */
1482 }
1483
1484 /*
1485  * scalar_call
1486  *
1487  * Call routine as obj->hook(av) in scalar context.
1488  * Propagates the single returned value if not called in void context.
1489  */
1490 static SV *scalar_call(
1491         SV *obj,
1492         SV *hook,
1493         int cloning,
1494         AV *av,
1495         I32 flags)
1496 {
1497         dSP;
1498         int count;
1499         SV *sv = 0;
1500
1501         TRACEME(("scalar_call (cloning=%d)", cloning));
1502
1503         ENTER;
1504         SAVETMPS;
1505
1506         PUSHMARK(sp);
1507         XPUSHs(obj);
1508         XPUSHs(sv_2mortal(newSViv(cloning)));           /* Cloning flag */
1509         if (av) {
1510                 SV **ary = AvARRAY(av);
1511                 int cnt = AvFILLp(av) + 1;
1512                 int i;
1513                 XPUSHs(ary[0]);                                                 /* Frozen string */
1514                 for (i = 1; i < cnt; i++) {
1515                         TRACEME(("pushing arg #%d (0x%"UVxf")...",
1516                                  i, PTR2UV(ary[i])));
1517                         XPUSHs(sv_2mortal(newRV(ary[i])));
1518                 }
1519         }
1520         PUTBACK;
1521
1522         TRACEME(("calling..."));
1523         count = perl_call_sv(hook, flags);              /* Go back to Perl code */
1524         TRACEME(("count = %d", count));
1525
1526         SPAGAIN;
1527
1528         if (count) {
1529                 sv = POPs;
1530                 SvREFCNT_inc(sv);               /* We're returning it, must stay alive! */
1531         }
1532
1533         PUTBACK;
1534         FREETMPS;
1535         LEAVE;
1536
1537         return sv;
1538 }
1539
1540 /*
1541  * array_call
1542  *
1543  * Call routine obj->hook(cloning) in list context.
1544  * Returns the list of returned values in an array.
1545  */
1546 static AV *array_call(
1547         SV *obj,
1548         SV *hook,
1549         int cloning)
1550 {
1551         dSP;
1552         int count;
1553         AV *av;
1554         int i;
1555
1556         TRACEME(("array_call (cloning=%d)", cloning));
1557
1558         ENTER;
1559         SAVETMPS;
1560
1561         PUSHMARK(sp);
1562         XPUSHs(obj);                                                            /* Target object */
1563         XPUSHs(sv_2mortal(newSViv(cloning)));           /* Cloning flag */
1564         PUTBACK;
1565
1566         count = perl_call_sv(hook, G_ARRAY);            /* Go back to Perl code */
1567
1568         SPAGAIN;
1569
1570         av = newAV();
1571         for (i = count - 1; i >= 0; i--) {
1572                 SV *sv = POPs;
1573                 av_store(av, i, SvREFCNT_inc(sv));
1574         }
1575
1576         PUTBACK;
1577         FREETMPS;
1578         LEAVE;
1579
1580         return av;
1581 }
1582
1583 /*
1584  * known_class
1585  *
1586  * Lookup the class name in the `hclass' table and either assign it a new ID
1587  * or return the existing one, by filling in `classnum'.
1588  *
1589  * Return true if the class was known, false if the ID was just generated.
1590  */
1591 static int known_class(
1592         stcxt_t *cxt,
1593         char *name,             /* Class name */
1594         int len,                /* Name length */
1595         I32 *classnum)
1596 {
1597         SV **svh;
1598         HV *hclass = cxt->hclass;
1599
1600         TRACEME(("known_class (%s)", name));
1601
1602         /*
1603          * Recall that we don't store pointers in this hash table, but tags.
1604          * Therefore, we need LOW_32BITS() to extract the relevant parts.
1605          */
1606
1607         svh = hv_fetch(hclass, name, len, FALSE);
1608         if (svh) {
1609                 *classnum = LOW_32BITS(*svh);
1610                 return TRUE;
1611         }
1612
1613         /*
1614          * Unknown classname, we need to record it.
1615          */
1616
1617         cxt->classnum++;
1618         if (!hv_store(hclass, name, len, INT2PTR(SV*, cxt->classnum), 0))
1619                 CROAK(("Unable to record new classname"));
1620
1621         *classnum = cxt->classnum;
1622         return FALSE;
1623 }
1624
1625 /***
1626  *** Sepcific store routines.
1627  ***/
1628
1629 /*
1630  * store_ref
1631  *
1632  * Store a reference.
1633  * Layout is SX_REF <object> or SX_OVERLOAD <object>.
1634  */
1635 static int store_ref(stcxt_t *cxt, SV *sv)
1636 {
1637         TRACEME(("store_ref (0x%"UVxf")", PTR2UV(sv)));
1638
1639         /*
1640          * Follow reference, and check if target is overloaded.
1641          */
1642
1643         sv = SvRV(sv);
1644
1645         if (SvOBJECT(sv)) {
1646                 HV *stash = (HV *) SvSTASH(sv);
1647                 if (stash && Gv_AMG(stash)) {
1648                         TRACEME(("ref (0x%"UVxf") is overloaded", PTR2UV(sv)));
1649                         PUTMARK(SX_OVERLOAD);
1650                 } else
1651                         PUTMARK(SX_REF);
1652         } else
1653                 PUTMARK(SX_REF);
1654
1655         return store(cxt, sv);
1656 }
1657
1658 /*
1659  * store_scalar
1660  *
1661  * Store a scalar.
1662  *
1663  * Layout is SX_LSCALAR <length> <data>, SX_SCALAR <length> <data> or SX_UNDEF.
1664  * The <data> section is omitted if <length> is 0.
1665  *
1666  * If integer or double, the layout is SX_INTEGER <data> or SX_DOUBLE <data>.
1667  * Small integers (within [-127, +127]) are stored as SX_BYTE <byte>.
1668  */
1669 static int store_scalar(stcxt_t *cxt, SV *sv)
1670 {
1671         IV iv;
1672         char *pv;
1673         STRLEN len;
1674         U32 flags = SvFLAGS(sv);                        /* "cc -O" may put it in register */
1675
1676         TRACEME(("store_scalar (0x%"UVxf")", PTR2UV(sv)));
1677
1678         /*
1679          * For efficiency, break the SV encapsulation by peaking at the flags
1680          * directly without using the Perl macros to avoid dereferencing
1681          * sv->sv_flags each time we wish to check the flags.
1682          */
1683
1684         if (!(flags & SVf_OK)) {                        /* !SvOK(sv) */
1685                 if (sv == &PL_sv_undef) {
1686                         TRACEME(("immortal undef"));
1687                         PUTMARK(SX_SV_UNDEF);
1688                 } else {
1689                         TRACEME(("undef at 0x%"UVxf, PTR2UV(sv)));
1690                         PUTMARK(SX_UNDEF);
1691                 }
1692                 return 0;
1693         }
1694
1695         /*
1696          * Always store the string representation of a scalar if it exists.
1697          * Gisle Aas provided me with this test case, better than a long speach:
1698          *
1699          *  perl -MDevel::Peek -le '$a="abc"; $a+0; Dump($a)'
1700          *  SV = PVNV(0x80c8520)
1701          *       REFCNT = 1
1702          *       FLAGS = (NOK,POK,pNOK,pPOK)
1703          *       IV = 0
1704          *       NV = 0
1705          *       PV = 0x80c83d0 "abc"\0
1706          *       CUR = 3
1707          *       LEN = 4
1708          *
1709          * Write SX_SCALAR, length, followed by the actual data.
1710          *
1711          * Otherwise, write an SX_BYTE, SX_INTEGER or an SX_DOUBLE as
1712          * appropriate, followed by the actual (binary) data. A double
1713          * is written as a string if network order, for portability.
1714          *
1715          * NOTE: instead of using SvNOK(sv), we test for SvNOKp(sv).
1716          * The reason is that when the scalar value is tainted, the SvNOK(sv)
1717          * value is false.
1718          *
1719          * The test for a read-only scalar with both POK and NOK set is meant
1720          * to quickly detect &PL_sv_yes and &PL_sv_no without having to pay the
1721          * address comparison for each scalar we store.
1722          */
1723
1724 #define SV_MAYBE_IMMORTAL (SVf_READONLY|SVf_POK|SVf_NOK)
1725
1726         if ((flags & SV_MAYBE_IMMORTAL) == SV_MAYBE_IMMORTAL) {
1727                 if (sv == &PL_sv_yes) {
1728                         TRACEME(("immortal yes"));
1729                         PUTMARK(SX_SV_YES);
1730                 } else if (sv == &PL_sv_no) {
1731                         TRACEME(("immortal no"));
1732                         PUTMARK(SX_SV_NO);
1733                 } else {
1734                         pv = SvPV(sv, len);                     /* We know it's SvPOK */
1735                         goto string;                            /* Share code below */
1736                 }
1737         } else if (flags & SVp_POK) {           /* SvPOKp(sv) => string */
1738                 I32 wlen;                                               /* For 64-bit machines */
1739                 pv = SvPV(sv, len);
1740
1741                 /*
1742                  * Will come here from below with pv and len set if double & netorder,
1743                  * or from above if it was readonly, POK and NOK but neither &PL_sv_yes
1744                  * nor &PL_sv_no.
1745                  */
1746         string:
1747
1748                 wlen = (I32) len;                               /* WLEN via STORE_SCALAR expects I32 */
1749                 if (SvUTF8 (sv))
1750                         STORE_UTF8STR(pv, wlen);
1751                 else
1752                         STORE_SCALAR(pv, wlen);
1753                 TRACEME(("ok (scalar 0x%"UVxf" '%s', length = %"IVdf")",
1754                          PTR2UV(sv), SvPVX(sv), (IV)len));
1755
1756         } else if (flags & SVp_NOK) {           /* SvNOKp(sv) => double */
1757                 NV nv = SvNV(sv);
1758
1759                 /*
1760                  * Watch for number being an integer in disguise.
1761                  */
1762                 if (nv == (NV) (iv = I_V(nv))) {
1763                         TRACEME(("double %"NVff" is actually integer %"IVdf, nv, iv));
1764                         goto integer;           /* Share code below */
1765                 }
1766
1767                 if (cxt->netorder) {
1768                         TRACEME(("double %"NVff" stored as string", nv));
1769                         pv = SvPV(sv, len);
1770                         goto string;            /* Share code above */
1771                 }
1772
1773                 PUTMARK(SX_DOUBLE);
1774                 WRITE(&nv, sizeof(nv));
1775
1776                 TRACEME(("ok (double 0x%"UVxf", value = %"NVff")", PTR2UV(sv), nv));
1777
1778         } else if (flags & SVp_IOK) {           /* SvIOKp(sv) => integer */
1779                 iv = SvIV(sv);
1780
1781                 /*
1782                  * Will come here from above with iv set if double is an integer.
1783                  */
1784         integer:
1785
1786                 /*
1787                  * Optimize small integers into a single byte, otherwise store as
1788                  * a real integer (converted into network order if they asked).
1789                  */
1790
1791                 if (iv >= -128 && iv <= 127) {
1792                         unsigned char siv = (unsigned char) (iv + 128); /* [0,255] */
1793                         PUTMARK(SX_BYTE);
1794                         PUTMARK(siv);
1795                         TRACEME(("small integer stored as %d", siv));
1796                 } else if (cxt->netorder) {
1797                         I32 niv;
1798 #ifdef HAS_HTONL
1799                         niv = (I32) htonl(iv);
1800                         TRACEME(("using network order"));
1801 #else
1802                         niv = (I32) iv;
1803                         TRACEME(("as-is for network order"));
1804 #endif
1805                         PUTMARK(SX_NETINT);
1806                         WRITE_I32(niv);
1807                 } else {
1808                         PUTMARK(SX_INTEGER);
1809                         WRITE(&iv, sizeof(iv));
1810                 }
1811
1812                 TRACEME(("ok (integer 0x%"UVxf", value = %"IVdf")", PTR2UV(sv), iv));
1813
1814         } else
1815                 CROAK(("Can't determine type of %s(0x%"UVxf")",
1816                        sv_reftype(sv, FALSE),
1817                        PTR2UV(sv)));
1818
1819         return 0;               /* Ok, no recursion on scalars */
1820 }
1821
1822 /*
1823  * store_array
1824  *
1825  * Store an array.
1826  *
1827  * Layout is SX_ARRAY <size> followed by each item, in increading index order.
1828  * Each item is stored as <object>.
1829  */
1830 static int store_array(stcxt_t *cxt, AV *av)
1831 {
1832         SV **sav;
1833         I32 len = av_len(av) + 1;
1834         I32 i;
1835         int ret;
1836
1837         TRACEME(("store_array (0x%"UVxf")", PTR2UV(av)));
1838
1839         /* 
1840          * Signal array by emitting SX_ARRAY, followed by the array length.
1841          */
1842
1843         PUTMARK(SX_ARRAY);
1844         WLEN(len);
1845         TRACEME(("size = %d", len));
1846
1847         /*
1848          * Now store each item recursively.
1849          */
1850
1851         for (i = 0; i < len; i++) {
1852                 sav = av_fetch(av, i, 0);
1853                 if (!sav) {
1854                         TRACEME(("(#%d) undef item", i));
1855                         STORE_UNDEF();
1856                         continue;
1857                 }
1858                 TRACEME(("(#%d) item", i));
1859                 if ((ret = store(cxt, *sav)))   /* Extra () for -Wall, grr... */
1860                         return ret;
1861         }
1862
1863         TRACEME(("ok (array)"));
1864
1865         return 0;
1866 }
1867
1868 /*
1869  * sortcmp
1870  *
1871  * Sort two SVs
1872  * Borrowed from perl source file pp_ctl.c, where it is used by pp_sort.
1873  */
1874 static int
1875 sortcmp(const void *a, const void *b)
1876 {
1877         return sv_cmp(*(SV * const *) a, *(SV * const *) b);
1878 }
1879
1880
1881 /*
1882  * store_hash
1883  *
1884  * Store a hash table.
1885  *
1886  * For a "normal" hash (not restricted, no utf8 keys):
1887  *
1888  * Layout is SX_HASH <size> followed by each key/value pair, in random order.
1889  * Values are stored as <object>.
1890  * Keys are stored as <length> <data>, the <data> section being omitted
1891  * if length is 0.
1892
1893  * Layout is SX_HASH <size> <hash flags> followed by each key/value pair,
1894  * in random order.
1895  * Values are stored as <object>.
1896  * Keys are stored as <flags> <length> <data>, the <data> section being omitted
1897  * if length is 0.
1898  * Currently the only hash flag is "restriced"
1899  * Key flags are as for hv.h
1900  */
1901 static int store_hash(stcxt_t *cxt, HV *hv)
1902 {
1903         I32 len = HvTOTALKEYS(hv);
1904         I32 i;
1905         int ret = 0;
1906         I32 riter;
1907         HE *eiter;
1908         int flagged_hash = ((SvREADONLY(hv) || HvHASKFLAGS(hv)) ? 1 : 0);
1909         unsigned char hash_flags = (SvREADONLY(hv) ? SHV_RESTRICTED : 0);
1910
1911         if (flagged_hash) {
1912             /* needs int cast for C++ compilers, doesn't it?  */
1913             TRACEME(("store_hash (0x%"UVxf") (flags %x)", PTR2UV(hv),
1914                      (int) hash_flags));
1915         } else {
1916             TRACEME(("store_hash (0x%"UVxf")", PTR2UV(hv)));
1917         }
1918
1919         /* 
1920          * Signal hash by emitting SX_HASH, followed by the table length.
1921          */
1922
1923         if (flagged_hash) {
1924             PUTMARK(SX_FLAG_HASH);
1925             PUTMARK(hash_flags);
1926         } else {
1927             PUTMARK(SX_HASH);
1928         }
1929         WLEN(len);
1930         TRACEME(("size = %d", len));
1931
1932         /*
1933          * Save possible iteration state via each() on that table.
1934          */
1935
1936         riter = HvRITER(hv);
1937         eiter = HvEITER(hv);
1938         hv_iterinit(hv);
1939
1940         /*
1941          * Now store each item recursively.
1942          *
1943      * If canonical is defined to some true value then store each
1944      * key/value pair in sorted order otherwise the order is random.
1945          * Canonical order is irrelevant when a deep clone operation is performed.
1946          *
1947          * Fetch the value from perl only once per store() operation, and only
1948          * when needed.
1949          */
1950
1951         if (
1952                 !(cxt->optype & ST_CLONE) && (cxt->canonical == 1 ||
1953                 (cxt->canonical < 0 && (cxt->canonical =
1954                         (SvTRUE(perl_get_sv("Storable::canonical", TRUE)) ? 1 : 0))))
1955         ) {
1956                 /*
1957                  * Storing in order, sorted by key.
1958                  * Run through the hash, building up an array of keys in a
1959                  * mortal array, sort the array and then run through the
1960                  * array.  
1961                  */
1962
1963                 AV *av = newAV();
1964
1965                 /*av_extend (av, len);*/
1966
1967                 TRACEME(("using canonical order"));
1968
1969                 for (i = 0; i < len; i++) {
1970                         HE *he = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS);
1971                         SV *key = hv_iterkeysv(he);
1972                         av_store(av, AvFILLp(av)+1, key);       /* av_push(), really */
1973                 }
1974                         
1975                 qsort((char *) AvARRAY(av), len, sizeof(SV *), sortcmp);
1976
1977                 for (i = 0; i < len; i++) {
1978                         unsigned char flags;
1979                         char *keyval;
1980                         STRLEN keylen_tmp;
1981                         I32 keylen;
1982                         SV *key = av_shift(av);
1983                         HE *he  = hv_fetch_ent(hv, key, 0, 0);
1984                         SV *val = HeVAL(he);
1985                         if (val == 0)
1986                                 return 1;               /* Internal error, not I/O error */
1987                         
1988                         /*
1989                          * Store value first.
1990                          */
1991                         
1992                         TRACEME(("(#%d) value 0x%"UVxf, i, PTR2UV(val)));
1993
1994                         if ((ret = store(cxt, val)))    /* Extra () for -Wall, grr... */
1995                                 goto out;
1996
1997                         /*
1998                          * Write key string.
1999                          * Keys are written after values to make sure retrieval
2000                          * can be optimal in terms of memory usage, where keys are
2001                          * read into a fixed unique buffer called kbuf.
2002                          * See retrieve_hash() for details.
2003                          */
2004                          
2005                         /* Implementation of restricted hashes isn't nicely
2006                            abstracted:  */
2007                         flags
2008                             = (((hash_flags & SHV_RESTRICTED)
2009                                 && SvREADONLY(val))
2010                                ? SHV_K_LOCKED : 0);
2011                         if (val == &PL_sv_undef)
2012                             flags |= SHV_K_PLACEHOLDER;
2013
2014                         keyval = SvPV(key, keylen_tmp);
2015                         keylen = keylen_tmp;
2016                         if (SvUTF8(key)) {
2017                             const char *keysave = keyval;
2018                             bool is_utf8 = TRUE;
2019
2020                             /* Just casting the &klen to (STRLEN) won't work
2021                                well if STRLEN and I32 are of different widths.
2022                                --jhi */
2023                             keyval = (char*)bytes_from_utf8((U8*)keyval,
2024                                                             &keylen_tmp,
2025                                                             &is_utf8);
2026
2027                             /* If we were able to downgrade here, then than
2028                                means that we have  a key which only had chars
2029                                0-255, but was utf8 encoded.  */
2030
2031                             if (keyval != keysave) {
2032                                 keylen = keylen_tmp;
2033                                 flags |= SHV_K_WASUTF8;
2034                             } else {
2035                                 /* keylen_tmp can't have changed, so no need
2036                                    to assign back to keylen.  */
2037                                 flags |= SHV_K_UTF8;
2038                             }
2039                         }
2040
2041                         if (flagged_hash) {
2042                             PUTMARK(flags);
2043                             TRACEME(("(#%d) key '%s' flags %x %u", i, keyval, flags, *keyval));
2044                         } else {
2045                             assert (flags == 0);
2046                             TRACEME(("(#%d) key '%s'", i, keyval));
2047                         }
2048                         WLEN(keylen);
2049                         if (keylen)
2050                                 WRITE(keyval, keylen);
2051                         if (flags & SHV_K_WASUTF8)
2052                             Safefree (keyval);
2053                 }
2054
2055                 /* 
2056                  * Free up the temporary array
2057                  */
2058
2059                 av_undef(av);
2060                 sv_free((SV *) av);
2061
2062         } else {
2063
2064                 /*
2065                  * Storing in "random" order (in the order the keys are stored
2066                  * within the the hash).  This is the default and will be faster!
2067                  */
2068   
2069                 for (i = 0; i < len; i++) {
2070                         char *key;
2071                         I32 len;
2072                         unsigned char flags;
2073                         HE *he = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS);
2074                         SV *val = (he ? hv_iterval(hv, he) : 0);
2075                         SV *key_sv = NULL;
2076                         HEK *hek;
2077
2078                         if (val == 0)
2079                                 return 1;               /* Internal error, not I/O error */
2080
2081                         /*
2082                          * Store value first.
2083                          */
2084
2085                         TRACEME(("(#%d) value 0x%"UVxf, i, PTR2UV(val)));
2086
2087                         if ((ret = store(cxt, val)))    /* Extra () for -Wall, grr... */
2088                                 goto out;
2089
2090                         /* Implementation of restricted hashes isn't nicely
2091                            abstracted:  */
2092                         flags
2093                             = (((hash_flags & SHV_RESTRICTED)
2094                                 && SvREADONLY(val))
2095                                              ? SHV_K_LOCKED : 0);
2096                         if (val == &PL_sv_undef)
2097                             flags |= SHV_K_PLACEHOLDER;
2098
2099                         hek = HeKEY_hek(he);
2100                         len = HEK_LEN(hek);
2101                         if (len == HEf_SVKEY) {
2102                             /* This is somewhat sick, but the internal APIs are
2103                              * such that XS code could put one of these in in
2104                              * a regular hash.
2105                              * Maybe we should be capable of storing one if
2106                              * found.
2107                              */
2108                             key_sv = HeKEY_sv(he);
2109                             flags |= SHV_K_ISSV;
2110                         } else {
2111                             /* Regular string key. */
2112                             if (HEK_UTF8(hek))
2113                                 flags |= SHV_K_UTF8;
2114                             if (HEK_WASUTF8(hek))
2115                                 flags |= SHV_K_WASUTF8;
2116                             key = HEK_KEY(hek);
2117                         }
2118                         /*
2119                          * Write key string.
2120                          * Keys are written after values to make sure retrieval
2121                          * can be optimal in terms of memory usage, where keys are
2122                          * read into a fixed unique buffer called kbuf.
2123                          * See retrieve_hash() for details.
2124                          */
2125
2126                         if (flagged_hash) {
2127                             PUTMARK(flags);
2128                             TRACEME(("(#%d) key '%s' flags %x", i, key, flags));
2129                         } else {
2130                             assert (flags == 0);
2131                             TRACEME(("(#%d) key '%s'", i, key));
2132                         }
2133                         if (flags & SHV_K_ISSV) {
2134                             store(cxt, key_sv);
2135                         } else {
2136                             WLEN(len);
2137                             if (len)
2138                                 WRITE(key, len);
2139                         }
2140                 }
2141     }
2142
2143         TRACEME(("ok (hash 0x%"UVxf")", PTR2UV(hv)));
2144
2145 out:
2146         HvRITER(hv) = riter;            /* Restore hash iterator state */
2147         HvEITER(hv) = eiter;
2148
2149         return ret;
2150 }
2151
2152 /*
2153  * store_tied
2154  *
2155  * When storing a tied object (be it a tied scalar, array or hash), we lay out
2156  * a special mark, followed by the underlying tied object. For instance, when
2157  * dealing with a tied hash, we store SX_TIED_HASH <hash object>, where
2158  * <hash object> stands for the serialization of the tied hash.
2159  */
2160 static int store_tied(stcxt_t *cxt, SV *sv)
2161 {
2162         MAGIC *mg;
2163         int ret = 0;
2164         int svt = SvTYPE(sv);
2165         char mtype = 'P';
2166
2167         TRACEME(("store_tied (0x%"UVxf")", PTR2UV(sv)));
2168
2169         /*
2170          * We have a small run-time penalty here because we chose to factorise
2171          * all tieds objects into the same routine, and not have a store_tied_hash,
2172          * a store_tied_array, etc...
2173          *
2174          * Don't use a switch() statement, as most compilers don't optimize that
2175          * well for 2/3 values. An if() else if() cascade is just fine. We put
2176          * tied hashes first, as they are the most likely beasts.
2177          */
2178
2179         if (svt == SVt_PVHV) {
2180                 TRACEME(("tied hash"));
2181                 PUTMARK(SX_TIED_HASH);                  /* Introduces tied hash */
2182         } else if (svt == SVt_PVAV) {
2183                 TRACEME(("tied array"));
2184                 PUTMARK(SX_TIED_ARRAY);                 /* Introduces tied array */
2185         } else {
2186                 TRACEME(("tied scalar"));
2187                 PUTMARK(SX_TIED_SCALAR);                /* Introduces tied scalar */
2188                 mtype = 'q';
2189         }
2190
2191         if (!(mg = mg_find(sv, mtype)))
2192                 CROAK(("No magic '%c' found while storing tied %s", mtype,
2193                         (svt == SVt_PVHV) ? "hash" :
2194                                 (svt == SVt_PVAV) ? "array" : "scalar"));
2195
2196         /*
2197          * The mg->mg_obj found by mg_find() above actually points to the
2198          * underlying tied Perl object implementation. For instance, if the
2199          * original SV was that of a tied array, then mg->mg_obj is an AV.
2200          *
2201          * Note that we store the Perl object as-is. We don't call its FETCH
2202          * method along the way. At retrieval time, we won't call its STORE
2203          * method either, but the tieing magic will be re-installed. In itself,
2204          * that ensures that the tieing semantics are preserved since futher
2205          * accesses on the retrieved object will indeed call the magic methods...
2206          */
2207
2208         if ((ret = store(cxt, mg->mg_obj)))             /* Extra () for -Wall, grr... */
2209                 return ret;
2210
2211         TRACEME(("ok (tied)"));
2212
2213         return 0;
2214 }
2215
2216 /*
2217  * store_tied_item
2218  *
2219  * Stores a reference to an item within a tied structure:
2220  *
2221  *  . \$h{key}, stores both the (tied %h) object and 'key'.
2222  *  . \$a[idx], stores both the (tied @a) object and 'idx'.
2223  *
2224  * Layout is therefore either:
2225  *     SX_TIED_KEY <object> <key>
2226  *     SX_TIED_IDX <object> <index>
2227  */
2228 static int store_tied_item(stcxt_t *cxt, SV *sv)
2229 {
2230         MAGIC *mg;
2231         int ret;
2232
2233         TRACEME(("store_tied_item (0x%"UVxf")", PTR2UV(sv)));
2234
2235         if (!(mg = mg_find(sv, 'p')))
2236                 CROAK(("No magic 'p' found while storing reference to tied item"));
2237
2238         /*
2239          * We discriminate between \$h{key} and \$a[idx] via mg_ptr.
2240          */
2241
2242         if (mg->mg_ptr) {
2243                 TRACEME(("store_tied_item: storing a ref to a tied hash item"));
2244                 PUTMARK(SX_TIED_KEY);
2245                 TRACEME(("store_tied_item: storing OBJ 0x%"UVxf, PTR2UV(mg->mg_obj)));
2246
2247                 if ((ret = store(cxt, mg->mg_obj)))             /* Extra () for -Wall, grr... */
2248                         return ret;
2249
2250                 TRACEME(("store_tied_item: storing PTR 0x%"UVxf, PTR2UV(mg->mg_ptr)));
2251
2252                 if ((ret = store(cxt, (SV *) mg->mg_ptr)))      /* Idem, for -Wall */
2253                         return ret;
2254         } else {
2255                 I32 idx = mg->mg_len;
2256
2257                 TRACEME(("store_tied_item: storing a ref to a tied array item "));
2258                 PUTMARK(SX_TIED_IDX);
2259                 TRACEME(("store_tied_item: storing OBJ 0x%"UVxf, PTR2UV(mg->mg_obj)));
2260
2261                 if ((ret = store(cxt, mg->mg_obj)))             /* Idem, for -Wall */
2262                         return ret;
2263
2264                 TRACEME(("store_tied_item: storing IDX %d", idx));
2265
2266                 WLEN(idx);
2267         }
2268
2269         TRACEME(("ok (tied item)"));
2270
2271         return 0;
2272 }
2273
2274 /*
2275  * store_hook           -- dispatched manually, not via sv_store[]
2276  *
2277  * The blessed SV is serialized by a hook.
2278  *
2279  * Simple Layout is:
2280  *
2281  *     SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
2282  *
2283  * where <flags> indicates how long <len>, <len2> and <len3> are, whether
2284  * the trailing part [] is present, the type of object (scalar, array or hash).
2285  * There is also a bit which says how the classname is stored between:
2286  *
2287  *     <len> <classname>
2288  *     <index>
2289  *
2290  * and when the <index> form is used (classname already seen), the "large
2291  * classname" bit in <flags> indicates how large the <index> is.
2292  * 
2293  * The serialized string returned by the hook is of length <len2> and comes
2294  * next.  It is an opaque string for us.
2295  *
2296  * Those <len3> object IDs which are listed last represent the extra references
2297  * not directly serialized by the hook, but which are linked to the object.
2298  *
2299  * When recursion is mandated to resolve object-IDs not yet seen, we have
2300  * instead, with <header> being flags with bits set to indicate the object type
2301  * and that recursion was indeed needed:
2302  *
2303  *     SX_HOOK <header> <object> <header> <object> <flags>
2304  *
2305  * that same header being repeated between serialized objects obtained through
2306  * recursion, until we reach flags indicating no recursion, at which point
2307  * we know we've resynchronized with a single layout, after <flags>.
2308  *
2309  * When storing a blessed ref to a tied variable, the following format is
2310  * used:
2311  *
2312  *     SX_HOOK <flags> <extra> ... [<len3> <object-IDs>] <magic object>
2313  *
2314  * The first <flags> indication carries an object of type SHT_EXTRA, and the
2315  * real object type is held in the <extra> flag.  At the very end of the
2316  * serialization stream, the underlying magic object is serialized, just like
2317  * any other tied variable.
2318  */
2319 static int store_hook(
2320         stcxt_t *cxt,
2321         SV *sv,
2322         int type,
2323         HV *pkg,
2324         SV *hook)
2325 {
2326         I32 len;
2327         char *class;
2328         STRLEN len2;
2329         SV *ref;
2330         AV *av;
2331         SV **ary;
2332         int count;                              /* really len3 + 1 */
2333         unsigned char flags;
2334         char *pv;
2335         int i;
2336         int recursed = 0;               /* counts recursion */
2337         int obj_type;                   /* object type, on 2 bits */
2338         I32 classnum;
2339         int ret;
2340         int clone = cxt->optype & ST_CLONE;
2341         char mtype = '\0';                              /* for blessed ref to tied structures */
2342         unsigned char eflags = '\0';    /* used when object type is SHT_EXTRA */
2343
2344         TRACEME(("store_hook, class \"%s\", tagged #%d", HvNAME(pkg), cxt->tagnum));
2345
2346         /*
2347          * Determine object type on 2 bits.
2348          */
2349
2350         switch (type) {
2351         case svis_SCALAR:
2352                 obj_type = SHT_SCALAR;
2353                 break;
2354         case svis_ARRAY:
2355                 obj_type = SHT_ARRAY;
2356                 break;
2357         case svis_HASH:
2358                 obj_type = SHT_HASH;
2359                 break;
2360         case svis_TIED:
2361                 /*
2362                  * Produced by a blessed ref to a tied data structure, $o in the
2363                  * following Perl code.
2364                  *
2365                  *      my %h;
2366                  *  tie %h, 'FOO';
2367                  *      my $o = bless \%h, 'BAR';
2368                  *
2369                  * Signal the tie-ing magic by setting the object type as SHT_EXTRA
2370                  * (since we have only 2 bits in <flags> to store the type), and an
2371                  * <extra> byte flag will be emitted after the FIRST <flags> in the
2372                  * stream, carrying what we put in `eflags'.
2373                  */
2374                 obj_type = SHT_EXTRA;
2375                 switch (SvTYPE(sv)) {
2376                 case SVt_PVHV:
2377                         eflags = (unsigned char) SHT_THASH;
2378                         mtype = 'P';
2379                         break;
2380                 case SVt_PVAV:
2381                         eflags = (unsigned char) SHT_TARRAY;
2382                         mtype = 'P';
2383                         break;
2384                 default:
2385                         eflags = (unsigned char) SHT_TSCALAR;
2386                         mtype = 'q';
2387                         break;
2388                 }
2389                 break;
2390         default:
2391                 CROAK(("Unexpected object type (%d) in store_hook()", type));
2392         }
2393         flags = SHF_NEED_RECURSE | obj_type;
2394
2395         class = HvNAME(pkg);
2396         len = strlen(class);
2397
2398         /*
2399          * To call the hook, we need to fake a call like:
2400          *
2401          *    $object->STORABLE_freeze($cloning);
2402          *
2403          * but we don't have the $object here.  For instance, if $object is
2404          * a blessed array, what we have in `sv' is the array, and we can't
2405          * call a method on those.
2406          *
2407          * Therefore, we need to create a temporary reference to the object and
2408          * make the call on that reference.
2409          */
2410
2411         TRACEME(("about to call STORABLE_freeze on class %s", class));
2412
2413         ref = newRV_noinc(sv);                          /* Temporary reference */
2414         av = array_call(ref, hook, clone);      /* @a = $object->STORABLE_freeze($c) */
2415         SvRV(ref) = 0;
2416         SvREFCNT_dec(ref);                                      /* Reclaim temporary reference */
2417
2418         count = AvFILLp(av) + 1;
2419         TRACEME(("store_hook, array holds %d items", count));
2420
2421         /*
2422          * If they return an empty list, it means they wish to ignore the
2423          * hook for this class (and not just this instance -- that's for them
2424          * to handle if they so wish).
2425          *
2426          * Simply disable the cached entry for the hook (it won't be recomputed
2427          * since it's present in the cache) and recurse to store_blessed().
2428          */
2429
2430         if (!count) {
2431                 /*
2432                  * They must not change their mind in the middle of a serialization.
2433                  */
2434
2435                 if (hv_fetch(cxt->hclass, class, len, FALSE))
2436                         CROAK(("Too late to ignore hooks for %s class \"%s\"",
2437                                 (cxt->optype & ST_CLONE) ? "cloning" : "storing", class));
2438         
2439                 pkg_hide(cxt->hook, pkg, "STORABLE_freeze");
2440
2441                 ASSERT(!pkg_can(cxt->hook, pkg, "STORABLE_freeze"), ("hook invisible"));
2442                 TRACEME(("ignoring STORABLE_freeze in class \"%s\"", class));
2443
2444                 return store_blessed(cxt, sv, type, pkg);
2445         }
2446
2447         /*
2448          * Get frozen string.
2449          */
2450
2451         ary = AvARRAY(av);
2452         pv = SvPV(ary[0], len2);
2453
2454         /*
2455          * If they returned more than one item, we need to serialize some
2456          * extra references if not already done.
2457          *
2458          * Loop over the array, starting at postion #1, and for each item,
2459          * ensure it is a reference, serialize it if not already done, and
2460          * replace the entry with the tag ID of the corresponding serialized
2461          * object.
2462          *
2463          * We CHEAT by not calling av_fetch() and read directly within the
2464          * array, for speed.
2465          */
2466
2467         for (i = 1; i < count; i++) {
2468                 SV **svh;
2469                 SV *rsv = ary[i];
2470                 SV *xsv;
2471                 AV *av_hook = cxt->hook_seen;
2472
2473                 if (!SvROK(rsv))
2474                         CROAK(("Item #%d returned by STORABLE_freeze "
2475                                 "for %s is not a reference", i, class));
2476                 xsv = SvRV(rsv);                /* Follow ref to know what to look for */
2477
2478                 /*
2479                  * Look in hseen and see if we have a tag already.
2480                  * Serialize entry if not done already, and get its tag.
2481                  */
2482
2483                 if ((svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE)))
2484                         goto sv_seen;           /* Avoid moving code too far to the right */
2485
2486                 TRACEME(("listed object %d at 0x%"UVxf" is unknown", i-1, PTR2UV(xsv)));
2487
2488                 /*
2489                  * We need to recurse to store that object and get it to be known
2490                  * so that we can resolve the list of object-IDs at retrieve time.
2491                  *
2492                  * The first time we do this, we need to emit the proper header
2493                  * indicating that we recursed, and what the type of object is (the
2494                  * object we're storing via a user-hook).  Indeed, during retrieval,
2495                  * we'll have to create the object before recursing to retrieve the
2496                  * others, in case those would point back at that object.
2497                  */
2498
2499                 /* [SX_HOOK] <flags> [<extra>] <object>*/
2500                 if (!recursed++) {
2501                         PUTMARK(SX_HOOK);
2502                         PUTMARK(flags);
2503                         if (obj_type == SHT_EXTRA)
2504                                 PUTMARK(eflags);
2505                 } else
2506                         PUTMARK(flags);
2507
2508                 if ((ret = store(cxt, xsv)))    /* Given by hook for us to store */
2509                         return ret;
2510
2511                 svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE);
2512                 if (!svh)
2513                         CROAK(("Could not serialize item #%d from hook in %s", i, class));
2514
2515                 /*
2516                  * It was the first time we serialized `xsv'.
2517                  *
2518                  * Keep this SV alive until the end of the serialization: if we
2519                  * disposed of it right now by decrementing its refcount, and it was
2520                  * a temporary value, some next temporary value allocated during
2521                  * another STORABLE_freeze might take its place, and we'd wrongly
2522                  * assume that new SV was already serialized, based on its presence
2523                  * in cxt->hseen.
2524                  *
2525                  * Therefore, push it away in cxt->hook_seen.
2526                  */
2527
2528                 av_store(av_hook, AvFILLp(av_hook)+1, SvREFCNT_inc(xsv));
2529
2530         sv_seen:
2531                 /*
2532                  * Dispose of the REF they returned.  If we saved the `xsv' away
2533                  * in the array of returned SVs, that will not cause the underlying
2534                  * referenced SV to be reclaimed.
2535                  */
2536
2537                 ASSERT(SvREFCNT(xsv) > 1, ("SV will survive disposal of its REF"));
2538                 SvREFCNT_dec(rsv);                      /* Dispose of reference */
2539
2540                 /*
2541                  * Replace entry with its tag (not a real SV, so no refcnt increment)
2542                  */
2543
2544                 ary[i] = *svh;
2545                 TRACEME(("listed object %d at 0x%"UVxf" is tag #%"UVuf,
2546                          i-1, PTR2UV(xsv), PTR2UV(*svh)));
2547         }
2548
2549         /*
2550          * Allocate a class ID if not already done.
2551          *
2552          * This needs to be done after the recursion above, since at retrieval
2553          * time, we'll see the inner objects first.  Many thanks to
2554          * Salvador Ortiz Garcia <sog@msg.com.mx> who spot that bug and
2555          * proposed the right fix.  -- RAM, 15/09/2000
2556          */
2557
2558         if (!known_class(cxt, class, len, &classnum)) {
2559                 TRACEME(("first time we see class %s, ID = %d", class, classnum));
2560                 classnum = -1;                          /* Mark: we must store classname */
2561         } else {
2562                 TRACEME(("already seen class %s, ID = %d", class, classnum));
2563         }
2564
2565         /*
2566          * Compute leading flags.
2567          */
2568
2569         flags = obj_type;
2570         if (((classnum == -1) ? len : classnum) > LG_SCALAR)
2571                 flags |= SHF_LARGE_CLASSLEN;
2572         if (classnum != -1)
2573                 flags |= SHF_IDX_CLASSNAME;
2574         if (len2 > LG_SCALAR)
2575                 flags |= SHF_LARGE_STRLEN;
2576         if (count > 1)
2577                 flags |= SHF_HAS_LIST;
2578         if (count > (LG_SCALAR + 1))
2579                 flags |= SHF_LARGE_LISTLEN;
2580
2581         /* 
2582          * We're ready to emit either serialized form:
2583          *
2584          *   SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
2585          *   SX_HOOK <flags> <index>           <len2> <str> [<len3> <object-IDs>]
2586          *
2587          * If we recursed, the SX_HOOK has already been emitted.
2588          */
2589
2590         TRACEME(("SX_HOOK (recursed=%d) flags=0x%x "
2591                         "class=%"IVdf" len=%"IVdf" len2=%"IVdf" len3=%d",
2592                  recursed, flags, (IV)classnum, (IV)len, (IV)len2, count-1));
2593
2594         /* SX_HOOK <flags> [<extra>] */
2595         if (!recursed) {
2596                 PUTMARK(SX_HOOK);
2597                 PUTMARK(flags);
2598                 if (obj_type == SHT_EXTRA)
2599                         PUTMARK(eflags);
2600         } else
2601                 PUTMARK(flags);
2602
2603         /* <len> <classname> or <index> */
2604         if (flags & SHF_IDX_CLASSNAME) {
2605                 if (flags & SHF_LARGE_CLASSLEN)
2606                         WLEN(classnum);
2607                 else {
2608                         unsigned char cnum = (unsigned char) classnum;
2609                         PUTMARK(cnum);
2610                 }
2611         } else {
2612                 if (flags & SHF_LARGE_CLASSLEN)
2613                         WLEN(len);
2614                 else {
2615                         unsigned char clen = (unsigned char) len;
2616                         PUTMARK(clen);
2617                 }
2618                 WRITE(class, len);              /* Final \0 is omitted */
2619         }
2620
2621         /* <len2> <frozen-str> */
2622         if (flags & SHF_LARGE_STRLEN) {
2623                 I32 wlen2 = len2;               /* STRLEN might be 8 bytes */
2624                 WLEN(wlen2);                    /* Must write an I32 for 64-bit machines */
2625         } else {
2626                 unsigned char clen = (unsigned char) len2;
2627                 PUTMARK(clen);
2628         }
2629         if (len2)
2630                 WRITE(pv, len2);        /* Final \0 is omitted */
2631
2632         /* [<len3> <object-IDs>] */
2633         if (flags & SHF_HAS_LIST) {
2634                 int len3 = count - 1;
2635                 if (flags & SHF_LARGE_LISTLEN)
2636                         WLEN(len3);
2637                 else {
2638                         unsigned char clen = (unsigned char) len3;
2639                         PUTMARK(clen);
2640                 }
2641
2642                 /*
2643                  * NOTA BENE, for 64-bit machines: the ary[i] below does not yield a
2644                  * real pointer, rather a tag number, well under the 32-bit limit.
2645                  */
2646
2647                 for (i = 1; i < count; i++) {
2648                         I32 tagval = htonl(LOW_32BITS(ary[i]));
2649                         WRITE_I32(tagval);
2650                         TRACEME(("object %d, tag #%d", i-1, ntohl(tagval)));
2651                 }
2652         }
2653
2654         /*
2655          * Free the array.  We need extra care for indices after 0, since they
2656          * don't hold real SVs but integers cast.
2657          */
2658
2659         if (count > 1)
2660                 AvFILLp(av) = 0;        /* Cheat, nothing after 0 interests us */
2661         av_undef(av);
2662         sv_free((SV *) av);
2663
2664         /*
2665          * If object was tied, need to insert serialization of the magic object.
2666          */
2667
2668         if (obj_type == SHT_EXTRA) {
2669                 MAGIC *mg;
2670
2671                 if (!(mg = mg_find(sv, mtype))) {
2672                         int svt = SvTYPE(sv);
2673                         CROAK(("No magic '%c' found while storing ref to tied %s with hook",
2674                                 mtype, (svt == SVt_PVHV) ? "hash" :
2675                                         (svt == SVt_PVAV) ? "array" : "scalar"));
2676                 }
2677
2678                 TRACEME(("handling the magic object 0x%"UVxf" part of 0x%"UVxf,
2679                         PTR2UV(mg->mg_obj), PTR2UV(sv)));
2680
2681                 /*
2682                  * [<magic object>]
2683                  */
2684
2685                 if ((ret = store(cxt, mg->mg_obj)))     /* Extra () for -Wall, grr... */
2686                         return ret;
2687         }
2688
2689         return 0;
2690 }
2691
2692 /*
2693  * store_blessed        -- dispatched manually, not via sv_store[]
2694  *
2695  * Check whether there is a STORABLE_xxx hook defined in the class or in one
2696  * of its ancestors.  If there is, then redispatch to store_hook();
2697  *
2698  * Otherwise, the blessed SV is stored using the following layout:
2699  *
2700  *    SX_BLESS <flag> <len> <classname> <object>
2701  *
2702  * where <flag> indicates whether <len> is stored on 0 or 4 bytes, depending
2703  * on the high-order bit in flag: if 1, then length follows on 4 bytes.
2704  * Otherwise, the low order bits give the length, thereby giving a compact
2705  * representation for class names less than 127 chars long.
2706  *
2707  * Each <classname> seen is remembered and indexed, so that the next time
2708  * an object in the blessed in the same <classname> is stored, the following
2709  * will be emitted:
2710  *
2711  *    SX_IX_BLESS <flag> <index> <object>
2712  *
2713  * where <index> is the classname index, stored on 0 or 4 bytes depending
2714  * on the high-order bit in flag (same encoding as above for <len>).
2715  */
2716 static int store_blessed(
2717         stcxt_t *cxt,
2718         SV *sv,
2719         int type,
2720         HV *pkg)
2721 {
2722         SV *hook;
2723         I32 len;
2724         char *class;
2725         I32 classnum;
2726
2727         TRACEME(("store_blessed, type %d, class \"%s\"", type, HvNAME(pkg)));
2728
2729         /*
2730          * Look for a hook for this blessed SV and redirect to store_hook()
2731          * if needed.
2732          */
2733
2734         hook = pkg_can(cxt->hook, pkg, "STORABLE_freeze");
2735         if (hook)
2736                 return store_hook(cxt, sv, type, pkg, hook);
2737
2738         /*
2739          * This is a blessed SV without any serialization hook.
2740          */
2741
2742         class = HvNAME(pkg);
2743         len = strlen(class);
2744
2745         TRACEME(("blessed 0x%"UVxf" in %s, no hook: tagged #%d",
2746                  PTR2UV(sv), class, cxt->tagnum));
2747
2748         /*
2749          * Determine whether it is the first time we see that class name (in which
2750          * case it will be stored in the SX_BLESS form), or whether we already
2751          * saw that class name before (in which case the SX_IX_BLESS form will be
2752          * used).
2753          */
2754
2755         if (known_class(cxt, class, len, &classnum)) {
2756                 TRACEME(("already seen class %s, ID = %d", class, classnum));
2757                 PUTMARK(SX_IX_BLESS);
2758                 if (classnum <= LG_BLESS) {
2759                         unsigned char cnum = (unsigned char) classnum;
2760                         PUTMARK(cnum);
2761                 } else {
2762                         unsigned char flag = (unsigned char) 0x80;
2763                         PUTMARK(flag);
2764                         WLEN(classnum);
2765                 }
2766         } else {
2767                 TRACEME(("first time we see class %s, ID = %d", class, classnum));
2768                 PUTMARK(SX_BLESS);
2769                 if (len <= LG_BLESS) {
2770                         unsigned char clen = (unsigned char) len;
2771                         PUTMARK(clen);
2772                 } else {
2773                         unsigned char flag = (unsigned char) 0x80;
2774                         PUTMARK(flag);
2775                         WLEN(len);                                      /* Don't BER-encode, this should be rare */
2776                 }
2777                 WRITE(class, len);                              /* Final \0 is omitted */
2778         }
2779
2780         /*
2781          * Now emit the <object> part.
2782          */
2783
2784         return SV_STORE(type)(cxt, sv);
2785 }
2786
2787 /*
2788  * store_other
2789  *
2790  * We don't know how to store the item we reached, so return an error condition.
2791  * (it's probably a GLOB, some CODE reference, etc...)
2792  *
2793  * If they defined the `forgive_me' variable at the Perl level to some
2794  * true value, then don't croak, just warn, and store a placeholder string
2795  * instead.
2796  */
2797 static int store_other(stcxt_t *cxt, SV *sv)
2798 {
2799         I32 len;
2800         static char buf[80];
2801
2802         TRACEME(("store_other"));
2803
2804         /*
2805          * Fetch the value from perl only once per store() operation.
2806          */
2807
2808         if (
2809                 cxt->forgive_me == 0 ||
2810                 (cxt->forgive_me < 0 && !(cxt->forgive_me =
2811                         SvTRUE(perl_get_sv("Storable::forgive_me", TRUE)) ? 1 : 0))
2812         )
2813                 CROAK(("Can't store %s items", sv_reftype(sv, FALSE)));
2814
2815         warn("Can't store item %s(0x%"UVxf")",
2816                 sv_reftype(sv, FALSE), PTR2UV(sv));
2817
2818         /*
2819          * Store placeholder string as a scalar instead...
2820          */
2821
2822         (void) sprintf(buf, "You lost %s(0x%"UVxf")%c", sv_reftype(sv, FALSE),
2823                        PTR2UV(sv), (char) 0);
2824
2825         len = strlen(buf);
2826         STORE_SCALAR(buf, len);
2827         TRACEME(("ok (dummy \"%s\", length = %"IVdf")", buf, len));
2828
2829         return 0;
2830 }
2831
2832 /***
2833  *** Store driving routines
2834  ***/
2835
2836 /*
2837  * sv_type
2838  *
2839  * WARNING: partially duplicates Perl's sv_reftype for speed.
2840  *
2841  * Returns the type of the SV, identified by an integer. That integer
2842  * may then be used to index the dynamic routine dispatch table.
2843  */
2844 static int sv_type(SV *sv)
2845 {
2846         switch (SvTYPE(sv)) {
2847         case SVt_NULL:
2848         case SVt_IV:
2849         case SVt_NV:
2850                 /*
2851                  * No need to check for ROK, that can't be set here since there
2852                  * is no field capable of hodling the xrv_rv reference.
2853                  */
2854                 return svis_SCALAR;
2855         case SVt_PV:
2856         case SVt_RV:
2857         case SVt_PVIV:
2858         case SVt_PVNV:
2859                 /*
2860                  * Starting from SVt_PV, it is possible to have the ROK flag
2861                  * set, the pointer to the other SV being either stored in
2862                  * the xrv_rv (in the case of a pure SVt_RV), or as the
2863                  * xpv_pv field of an SVt_PV and its heirs.
2864                  *
2865                  * However, those SV cannot be magical or they would be an
2866                  * SVt_PVMG at least.
2867                  */
2868                 return SvROK(sv) ? svis_REF : svis_SCALAR;
2869         case SVt_PVMG:
2870         case SVt_PVLV:          /* Workaround for perl5.004_04 "LVALUE" bug */
2871                 if (SvRMAGICAL(sv) && (mg_find(sv, 'p')))
2872                         return svis_TIED_ITEM;
2873                 /* FALL THROUGH */
2874         case SVt_PVBM:
2875                 if (SvRMAGICAL(sv) && (mg_find(sv, 'q')))
2876                         return svis_TIED;
2877                 return SvROK(sv) ? svis_REF : svis_SCALAR;
2878         case SVt_PVAV:
2879                 if (SvRMAGICAL(sv) && (mg_find(sv, 'P')))
2880                         return svis_TIED;
2881                 return svis_ARRAY;
2882         case SVt_PVHV:
2883                 if (SvRMAGICAL(sv) && (mg_find(sv, 'P')))
2884                         return svis_TIED;
2885                 return svis_HASH;
2886         default:
2887                 break;
2888         }
2889
2890         return svis_OTHER;
2891 }
2892
2893 /*
2894  * store
2895  *
2896  * Recursively store objects pointed to by the sv to the specified file.
2897  *
2898  * Layout is <content> or SX_OBJECT <tagnum> if we reach an already stored
2899  * object (one for which storage has started -- it may not be over if we have
2900  * a self-referenced structure). This data set forms a stored <object>.
2901  */
2902 static int store(stcxt_t *cxt, SV *sv)
2903 {
2904         SV **svh;
2905         int ret;
2906         int type;
2907         HV *hseen = cxt->hseen;
2908
2909         TRACEME(("store (0x%"UVxf")", PTR2UV(sv)));
2910
2911         /*
2912          * If object has already been stored, do not duplicate data.
2913          * Simply emit the SX_OBJECT marker followed by its tag data.
2914          * The tag is always written in network order.
2915          *
2916          * NOTA BENE, for 64-bit machines: the "*svh" below does not yield a
2917          * real pointer, rather a tag number (watch the insertion code below).
2918          * That means it pobably safe to assume it is well under the 32-bit limit,
2919          * and makes the truncation safe.
2920          *              -- RAM, 14/09/1999
2921          */
2922
2923         svh = hv_fetch(hseen, (char *) &sv, sizeof(sv), FALSE);
2924         if (svh) {
2925                 I32 tagval = htonl(LOW_32BITS(*svh));
2926
2927                 TRACEME(("object 0x%"UVxf" seen as #%d", PTR2UV(sv), ntohl(tagval)));
2928
2929                 PUTMARK(SX_OBJECT);
2930                 WRITE_I32(tagval);
2931                 return 0;
2932         }
2933
2934         /*
2935          * Allocate a new tag and associate it with the address of the sv being
2936          * stored, before recursing...
2937          *
2938          * In order to avoid creating new SvIVs to hold the tagnum we just
2939          * cast the tagnum to an SV pointer and store that in the hash.  This
2940          * means that we must clean up the hash manually afterwards, but gives
2941          * us a 15% throughput increase.
2942          *
2943          */
2944
2945         cxt->tagnum++;
2946         if (!hv_store(hseen,
2947                         (char *) &sv, sizeof(sv), INT2PTR(SV*, cxt->tagnum), 0))
2948                 return -1;
2949
2950         /*
2951          * Store `sv' and everything beneath it, using appropriate routine.
2952          * Abort immediately if we get a non-zero status back.
2953          */
2954
2955         type = sv_type(sv);
2956
2957         TRACEME(("storing 0x%"UVxf" tag #%d, type %d...",
2958                  PTR2UV(sv), cxt->tagnum, type));
2959
2960         if (SvOBJECT(sv)) {
2961                 HV *pkg = SvSTASH(sv);
2962                 ret = store_blessed(cxt, sv, type, pkg);
2963         } else
2964                 ret = SV_STORE(type)(cxt, sv);
2965
2966         TRACEME(("%s (stored 0x%"UVxf", refcnt=%d, %s)",
2967                 ret ? "FAILED" : "ok", PTR2UV(sv),
2968                 SvREFCNT(sv), sv_reftype(sv, FALSE)));
2969
2970         return ret;
2971 }
2972
2973 /*
2974  * magic_write
2975  *
2976  * Write magic number and system information into the file.
2977  * Layout is <magic> <network> [<len> <byteorder> <sizeof int> <sizeof long>
2978  * <sizeof ptr>] where <len> is the length of the byteorder hexa string.
2979  * All size and lenghts are written as single characters here.
2980  *
2981  * Note that no byte ordering info is emitted when <network> is true, since
2982  * integers will be emitted in network order in that case.
2983  */
2984 static int magic_write(stcxt_t *cxt)
2985 {
2986         char buf[256];  /* Enough room for 256 hexa digits */
2987         unsigned char c;
2988         int use_network_order = cxt->netorder;
2989
2990         TRACEME(("magic_write on fd=%d", cxt->fio ? PerlIO_fileno(cxt->fio)
2991                  : -1));
2992
2993         if (cxt->fio)
2994                 WRITE(magicstr, strlen(magicstr));      /* Don't write final \0 */
2995
2996         /*
2997          * Starting with 0.6, the "use_network_order" byte flag is also used to
2998          * indicate the version number of the binary image, encoded in the upper
2999          * bits. The bit 0 is always used to indicate network order.
3000          */
3001
3002         c = (unsigned char)
3003                 ((use_network_order ? 0x1 : 0x0) | (STORABLE_BIN_MAJOR << 1));
3004         PUTMARK(c);
3005
3006         /*
3007          * Starting with 0.7, a full byte is dedicated to the minor version of
3008          * the binary format, which is incremented only when new markers are
3009          * introduced, for instance, but when backward compatibility is preserved.
3010          */
3011
3012         PUTMARK((unsigned char) STORABLE_BIN_MINOR);
3013
3014         if (use_network_order)
3015                 return 0;                                               /* Don't bother with byte ordering */
3016
3017         sprintf(buf, "%lx", (unsigned long) BYTEORDER);
3018         c = (unsigned char) strlen(buf);
3019         PUTMARK(c);
3020         WRITE(buf, (unsigned int) c);           /* Don't write final \0 */
3021         PUTMARK((unsigned char) sizeof(int));
3022         PUTMARK((unsigned char) sizeof(long));
3023         PUTMARK((unsigned char) sizeof(char *));
3024         PUTMARK((unsigned char) sizeof(NV));
3025
3026         TRACEME(("ok (magic_write byteorder = 0x%lx [%d], I%d L%d P%d D%d)",
3027                  (unsigned long) BYTEORDER, (int) c,
3028                  (int) sizeof(int), (int) sizeof(long),
3029                  (int) sizeof(char *), (int) sizeof(NV)));
3030
3031         return 0;
3032 }
3033
3034 /*
3035  * do_store
3036  *
3037  * Common code for store operations.
3038  *
3039  * When memory store is requested (f = NULL) and a non null SV* is given in
3040  * `res', it is filled with a new SV created out of the memory buffer.
3041  *
3042  * It is required to provide a non-null `res' when the operation type is not
3043  * dclone() and store() is performed to memory.
3044  */
3045 static int do_store(
3046         PerlIO *f,
3047         SV *sv,
3048         int optype,
3049         int network_order,
3050         SV **res)
3051 {
3052         dSTCXT;
3053         int status;
3054
3055         ASSERT(!(f == 0 && !(optype & ST_CLONE)) || res,
3056                 ("must supply result SV pointer for real recursion to memory"));
3057
3058         TRACEME(("do_store (optype=%d, netorder=%d)",
3059                 optype, network_order));
3060
3061         optype |= ST_STORE;
3062
3063         /*
3064          * Workaround for CROAK leak: if they enter with a "dirty" context,
3065          * free up memory for them now.
3066          */
3067
3068         if (cxt->s_dirty)
3069                 clean_context(cxt);
3070
3071         /*
3072          * Now that STORABLE_xxx hooks exist, it is possible that they try to
3073          * re-enter store() via the hooks.  We need to stack contexts.
3074          */
3075
3076         if (cxt->entry)
3077                 cxt = allocate_context(cxt);
3078
3079         cxt->entry++;
3080
3081         ASSERT(cxt->entry == 1, ("starting new recursion"));
3082         ASSERT(!cxt->s_dirty, ("clean context"));
3083
3084         /*
3085          * Ensure sv is actually a reference. From perl, we called something
3086          * like:
3087          *       pstore(FILE, \@array);
3088          * so we must get the scalar value behing that reference.
3089          */
3090
3091         if (!SvROK(sv))
3092                 CROAK(("Not a reference"));
3093         sv = SvRV(sv);                  /* So follow it to know what to store */
3094
3095         /* 
3096          * If we're going to store to memory, reset the buffer.
3097          */
3098
3099         if (!f)
3100                 MBUF_INIT(0);
3101
3102         /*
3103          * Prepare context and emit headers.
3104          */
3105
3106         init_store_context(cxt, f, optype, network_order);
3107
3108         if (-1 == magic_write(cxt))             /* Emit magic and ILP info */
3109                 return 0;                                       /* Error */
3110
3111         /*
3112          * Recursively store object...
3113          */
3114
3115         ASSERT(is_storing(), ("within store operation"));
3116
3117         status = store(cxt, sv);                /* Just do it! */
3118
3119         /*
3120          * If they asked for a memory store and they provided an SV pointer,
3121          * make an SV string out of the buffer and fill their pointer.
3122          *
3123          * When asking for ST_REAL, it's MANDATORY for the caller to provide
3124          * an SV, since context cleanup might free the buffer if we did recurse.
3125          * (unless caller is dclone(), which is aware of that).
3126          */
3127
3128         if (!cxt->fio && res)
3129                 *res = mbuf2sv();
3130
3131         /*
3132          * Final cleanup.
3133          *
3134          * The "root" context is never freed, since it is meant to be always
3135          * handy for the common case where no recursion occurs at all (i.e.
3136          * we enter store() outside of any Storable code and leave it, period).
3137          * We know it's the "root" context because there's nothing stacked
3138          * underneath it.
3139          *
3140          * OPTIMIZATION:
3141          *
3142          * When deep cloning, we don't free the context: doing so would force
3143          * us to copy the data in the memory buffer.  Sicne we know we're
3144          * about to enter do_retrieve...
3145          */
3146
3147         clean_store_context(cxt);
3148         if (cxt->prev && !(cxt->optype & ST_CLONE))
3149                 free_context(cxt);
3150
3151         TRACEME(("do_store returns %d", status));
3152
3153         return status == 0;
3154 }
3155
3156 /*
3157  * pstore
3158  *
3159  * Store the transitive data closure of given object to disk.
3160  * Returns 0 on error, a true value otherwise.
3161  */
3162 int pstore(PerlIO *f, SV *sv)
3163 {
3164         TRACEME(("pstore"));
3165         return do_store(f, sv, 0, FALSE, (SV**) 0);
3166
3167 }
3168
3169 /*
3170  * net_pstore
3171  *
3172  * Same as pstore(), but network order is used for integers and doubles are
3173  * emitted as strings.
3174  */
3175 int net_pstore(PerlIO *f, SV *sv)
3176 {
3177         TRACEME(("net_pstore"));
3178         return do_store(f, sv, 0, TRUE, (SV**) 0);
3179 }
3180
3181 /***
3182  *** Memory stores.
3183  ***/
3184
3185 /*
3186  * mbuf2sv
3187  *
3188  * Build a new SV out of the content of the internal memory buffer.
3189  */
3190 static SV *mbuf2sv(void)
3191 {
3192         dSTCXT;
3193
3194         return newSVpv(mbase, MBUF_SIZE());
3195 }
3196
3197 /*
3198  * mstore
3199  *
3200  * Store the transitive data closure of given object to memory.
3201  * Returns undef on error, a scalar value containing the data otherwise.
3202  */
3203 SV *mstore(SV *sv)
3204 {
3205         SV *out;
3206
3207         TRACEME(("mstore"));
3208
3209         if (!do_store((PerlIO*) 0, sv, 0, FALSE, &out))
3210                 return &PL_sv_undef;
3211
3212         return out;
3213 }
3214
3215 /*
3216  * net_mstore
3217  *
3218  * Same as mstore(), but network order is used for integers and doubles are
3219  * emitted as strings.
3220  */
3221 SV *net_mstore(SV *sv)
3222 {
3223         SV *out;
3224
3225         TRACEME(("net_mstore"));
3226
3227         if (!do_store((PerlIO*) 0, sv, 0, TRUE, &out))
3228                 return &PL_sv_undef;
3229
3230         return out;
3231 }
3232
3233 /***
3234  *** Specific retrieve callbacks.
3235  ***/
3236
3237 /*
3238  * retrieve_other
3239  *
3240  * Return an error via croak, since it is not possible that we get here
3241  * under normal conditions, when facing a file produced via pstore().
3242  */
3243 static SV *retrieve_other(stcxt_t *cxt, char *cname)
3244 {
3245         if (
3246                 cxt->ver_major != STORABLE_BIN_MAJOR &&
3247                 cxt->ver_minor != STORABLE_BIN_MINOR
3248         ) {
3249                 CROAK(("Corrupted storable %s (binary v%d.%d), current is v%d.%d",
3250                         cxt->fio ? "file" : "string",
3251                         cxt->ver_major, cxt->ver_minor,
3252                         STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR));
3253         } else {
3254                 CROAK(("Corrupted storable %s (binary v%d.%d)",
3255                         cxt->fio ? "file" : "string",
3256                         cxt->ver_major, cxt->ver_minor));
3257         }
3258
3259         return (SV *) 0;                /* Just in case */
3260 }
3261
3262 /*
3263  * retrieve_idx_blessed
3264  *
3265  * Layout is SX_IX_BLESS <index> <object> with SX_IX_BLESS already read.
3266  * <index> can be coded on either 1 or 5 bytes.
3267  */
3268 static SV *retrieve_idx_blessed(stcxt_t *cxt, char *cname)
3269 {
3270         I32 idx;
3271         char *class;
3272         SV **sva;
3273         SV *sv;
3274
3275         TRACEME(("retrieve_idx_blessed (#%d)", cxt->tagnum));
3276         ASSERT(!cname, ("no bless-into class given here, got %s", cname));
3277
3278         GETMARK(idx);                   /* Index coded on a single char? */
3279         if (idx & 0x80)
3280                 RLEN(idx);
3281
3282         /*
3283          * Fetch classname in `aclass'
3284          */
3285
3286         sva = av_fetch(cxt->aclass, idx, FALSE);
3287         if (!sva)
3288                 CROAK(("Class name #%"IVdf" should have been seen already", (IV) idx));
3289
3290         class = SvPVX(*sva);    /* We know it's a PV, by construction */
3291
3292         TRACEME(("class ID %d => %s", idx, class));
3293
3294         /*
3295          * Retrieve object and bless it.
3296          */
3297
3298         sv = retrieve(cxt, class);      /* First SV which is SEEN will be blessed */
3299
3300         return sv;
3301 }
3302
3303 /*
3304  * retrieve_blessed
3305  *
3306  * Layout is SX_BLESS <len> <classname> <object> with SX_BLESS already read.
3307  * <len> can be coded on either 1 or 5 bytes.
3308  */
3309 static SV *retrieve_blessed(stcxt_t *cxt, char *cname)
3310 {
3311         I32 len;
3312         SV *sv;
3313         char buf[LG_BLESS + 1];         /* Avoid malloc() if possible */
3314         char *class = buf;
3315
3316         TRACEME(("retrieve_blessed (#%d)", cxt->tagnum));
3317         ASSERT(!cname, ("no bless-into class given here, got %s", cname));
3318
3319         /*
3320          * Decode class name length and read that name.
3321          *
3322          * Short classnames have two advantages: their length is stored on one
3323          * single byte, and the string can be read on the stack.
3324          */
3325
3326         GETMARK(len);                   /* Length coded on a single char? */
3327         if (len & 0x80) {
3328                 RLEN(len);
3329                 TRACEME(("** allocating %d bytes for class name", len+1));
3330                 New(10003, class, len+1, char);
3331         }
3332         READ(class, len);
3333         class[len] = '\0';              /* Mark string end */
3334
3335         /*
3336          * It's a new classname, otherwise it would have been an SX_IX_BLESS.
3337          */
3338
3339         TRACEME(("new class name \"%s\" will bear ID = %d", class, cxt->classnum));
3340
3341         if (!av_store(cxt->aclass, cxt->classnum++, newSVpvn(class, len)))
3342                 return (SV *) 0;
3343
3344         /*
3345          * Retrieve object and bless it.
3346          */
3347
3348         sv = retrieve(cxt, class);      /* First SV which is SEEN will be blessed */
3349         if (class != buf)
3350                 Safefree(class);
3351
3352         return sv;
3353 }
3354
3355 /*
3356  * retrieve_hook
3357  *
3358  * Layout: SX_HOOK <flags> <len> <classname> <len2> <str> [<len3> <object-IDs>]
3359  * with leading mark already read, as usual.
3360  *
3361  * When recursion was involved during serialization of the object, there
3362  * is an unknown amount of serialized objects after the SX_HOOK mark.  Until
3363  * we reach a <flags> marker with the recursion bit cleared.
3364  *
3365  * If the first <flags> byte contains a type of SHT_EXTRA, then the real type
3366  * is held in the <extra> byte, and if the object is tied, the serialized
3367  * magic object comes at the very end:
3368  *
3369  *     SX_HOOK <flags> <extra> ... [<len3> <object-IDs>] <magic object>
3370  *
3371  * This means the STORABLE_thaw hook will NOT get a tied variable during its
3372  * processing (since we won't have seen the magic object by the time the hook
3373  * is called).  See comments below for why it was done that way.
3374  */
3375 static SV *retrieve_hook(stcxt_t *cxt, char *cname)
3376 {
3377         I32 len;
3378         char buf[LG_BLESS + 1];         /* Avoid malloc() if possible */
3379         char *class = buf;
3380         unsigned int flags;
3381         I32 len2;
3382         SV *frozen;
3383         I32 len3 = 0;
3384         AV *av = 0;
3385         SV *hook;
3386         SV *sv;
3387         SV *rv;
3388         int obj_type;
3389         int clone = cxt->optype & ST_CLONE;
3390         char mtype = '\0';
3391         unsigned int extra_type = 0;
3392
3393         TRACEME(("retrieve_hook (#%d)", cxt->tagnum));
3394         ASSERT(!cname, ("no bless-into class given here, got %s", cname));
3395
3396         /*
3397          * Read flags, which tell us about the type, and whether we need to recurse.
3398          */
3399
3400         GETMARK(flags);
3401
3402         /*
3403          * Create the (empty) object, and mark it as seen.
3404          *
3405          * This must be done now, because tags are incremented, and during
3406          * serialization, the object tag was affected before recursion could
3407          * take place.
3408          */
3409
3410         obj_type = flags & SHF_TYPE_MASK;
3411         switch (obj_type) {
3412         case SHT_SCALAR:
3413                 sv = newSV(0);
3414                 break;
3415         case SHT_ARRAY:
3416                 sv = (SV *) newAV();
3417                 break;
3418         case SHT_HASH:
3419                 sv = (SV *) newHV();
3420                 break;
3421         case SHT_EXTRA:
3422                 /*
3423                  * Read <extra> flag to know the type of the object.
3424                  * Record associated magic type for later.
3425                  */
3426                 GETMARK(extra_type);
3427                 switch (extra_type) {
3428                 case SHT_TSCALAR:
3429                         sv = newSV(0);
3430                         mtype = 'q';
3431                         break;
3432                 case SHT_TARRAY:
3433                         sv = (SV *) newAV();
3434                         mtype = 'P';
3435                         break;
3436                 case SHT_THASH:
3437                         sv = (SV *) newHV();
3438                         mtype = 'P';
3439                         break;
3440                 default:
3441                         return retrieve_other(cxt, 0);  /* Let it croak */
3442                 }
3443                 break;
3444         default:
3445                 return retrieve_other(cxt, 0);          /* Let it croak */
3446         }
3447         SEEN(sv, 0);                                                    /* Don't bless yet */
3448
3449         /*
3450          * Whilst flags tell us to recurse, do so.
3451          *
3452          * We don't need to remember the addresses returned by retrieval, because
3453          * all the references will be obtained through indirection via the object
3454          * tags in the object-ID list.
3455          */
3456
3457         while (flags & SHF_NEED_RECURSE) {
3458                 TRACEME(("retrieve_hook recursing..."));
3459                 rv = retrieve(cxt, 0);
3460                 if (!rv)
3461                         return (SV *) 0;
3462                 TRACEME(("retrieve_hook back with rv=0x%"UVxf,
3463                          PTR2UV(rv)));
3464                 GETMARK(flags);
3465         }
3466
3467         if (flags & SHF_IDX_CLASSNAME) {
3468                 SV **sva;
3469                 I32 idx;
3470
3471                 /*
3472                  * Fetch index from `aclass'
3473                  */
3474
3475                 if (flags & SHF_LARGE_CLASSLEN)
3476                         RLEN(idx);
3477                 else
3478                         GETMARK(idx);
3479
3480                 sva = av_fetch(cxt->aclass, idx, FALSE);
3481                 if (!sva)
3482                         CROAK(("Class name #%"IVdf" should have been seen already",
3483                                 (IV) idx));
3484
3485                 class = SvPVX(*sva);    /* We know it's a PV, by construction */
3486                 TRACEME(("class ID %d => %s", idx, class));
3487
3488         } else {
3489                 /*
3490                  * Decode class name length and read that name.
3491                  *
3492                  * NOTA BENE: even if the length is stored on one byte, we don't read
3493                  * on the stack.  Just like retrieve_blessed(), we limit the name to
3494                  * LG_BLESS bytes.  This is an arbitrary decision.
3495                  */
3496
3497                 if (flags & SHF_LARGE_CLASSLEN)
3498                         RLEN(len);
3499                 else
3500                         GETMARK(len);
3501
3502                 if (len > LG_BLESS) {
3503                         TRACEME(("** allocating %d bytes for class name", len+1));
3504                         New(10003, class, len+1, char);
3505                 }
3506
3507                 READ(class, len);
3508                 class[len] = '\0';              /* Mark string end */
3509
3510                 /*
3511                  * Record new classname.
3512                  */
3513
3514                 if (!av_store(cxt->aclass, cxt->classnum++, newSVpvn(class, len)))
3515                         return (SV *) 0;
3516         }
3517
3518         TRACEME(("class name: %s", class));
3519
3520         /*
3521          * Decode user-frozen string length and read it in an SV.
3522          *
3523          * For efficiency reasons, we read data directly into the SV buffer.
3524          * To understand that code, read retrieve_scalar()
3525          */
3526
3527         if (flags & SHF_LARGE_STRLEN)
3528                 RLEN(len2);
3529         else
3530                 GETMARK(len2);
3531
3532         frozen = NEWSV(10002, len2);
3533         if (len2) {
3534                 SAFEREAD(SvPVX(frozen), len2, frozen);
3535                 SvCUR_set(frozen, len2);
3536                 *SvEND(frozen) = '\0';
3537         }
3538         (void) SvPOK_only(frozen);              /* Validates string pointer */
3539         if (cxt->s_tainted)                             /* Is input source tainted? */
3540                 SvTAINT(frozen);
3541
3542         TRACEME(("frozen string: %d bytes", len2));
3543
3544         /*
3545          * Decode object-ID list length, if present.
3546          */
3547
3548         if (flags & SHF_HAS_LIST) {
3549                 if (flags & SHF_LARGE_LISTLEN)
3550                         RLEN(len3);
3551                 else
3552                         GETMARK(len3);
3553                 if (len3) {
3554                         av = newAV();
3555                         av_extend(av, len3 + 1);        /* Leave room for [0] */
3556                         AvFILLp(av) = len3;                     /* About to be filled anyway */
3557                 }
3558         }
3559
3560         TRACEME(("has %d object IDs to link", len3));
3561
3562         /*
3563          * Read object-ID list into array.
3564          * Because we pre-extended it, we can cheat and fill it manually.
3565          *
3566          * We read object tags and we can convert them into SV* on the fly
3567          * because we know all the references listed in there (as tags)
3568          * have been already serialized, hence we have a valid correspondance
3569          * between each of those tags and the recreated SV.
3570          */
3571
3572         if (av) {
3573                 SV **ary = AvARRAY(av);
3574                 int i;
3575                 for (i = 1; i <= len3; i++) {   /* We leave [0] alone */
3576                         I32 tag;
3577                         SV **svh;
3578                         SV *xsv;
3579
3580                         READ_I32(tag);
3581                         tag = ntohl(tag);
3582                         svh = av_fetch(cxt->aseen, tag, FALSE);
3583                         if (!svh)
3584                                 CROAK(("Object #%"IVdf" should have been retrieved already",
3585                                         (IV) tag));
3586                         xsv = *svh;
3587                         ary[i] = SvREFCNT_inc(xsv);
3588                 }
3589         }
3590
3591         /*
3592          * Bless the object and look up the STORABLE_thaw hook.
3593          */
3594
3595         BLESS(sv, class);
3596         hook = pkg_can(cxt->hook, SvSTASH(sv), "STORABLE_thaw");
3597         if (!hook) {
3598                 /*
3599                  * Hook not found.  Maybe they did not require the module where this
3600                  * hook is defined yet?
3601                  *
3602                  * If the require below succeeds, we'll be able to find the hook.
3603                  * Still, it only works reliably when each class is defined in a
3604                  * file of its own.
3605                  */
3606
3607                 SV *psv = newSVpvn("require ", 8);
3608                 sv_catpv(psv, class);
3609
3610                 TRACEME(("No STORABLE_thaw defined for objects of class %s", class));
3611                 TRACEME(("Going to require module '%s' with '%s'", class, SvPVX(psv)));
3612
3613                 perl_eval_sv(psv, G_DISCARD);
3614                 sv_free(psv);
3615
3616                 /*
3617                  * We cache results of pkg_can, so we need to uncache before attempting
3618                  * the lookup again.
3619                  */
3620
3621                 pkg_uncache(cxt->hook, SvSTASH(sv), "STORABLE_thaw");
3622                 hook = pkg_can(cxt->hook, SvSTASH(sv), "STORABLE_thaw");
3623
3624                 if (!hook)
3625                         CROAK(("No STORABLE_thaw defined for objects of class %s "
3626                                         "(even after a \"require %s;\")", class, class));
3627         }
3628
3629         /*
3630          * If we don't have an `av' yet, prepare one.
3631          * Then insert the frozen string as item [0].
3632          */
3633
3634         if (!av) {
3635                 av = newAV();
3636                 av_extend(av, 1);
3637                 AvFILLp(av) = 0;
3638         }
3639         AvARRAY(av)[0] = SvREFCNT_inc(frozen);
3640
3641         /*
3642          * Call the hook as:
3643          *
3644          *   $object->STORABLE_thaw($cloning, $frozen, @refs);
3645          * 
3646          * where $object is our blessed (empty) object, $cloning is a boolean
3647          * telling whether we're running a deep clone, $frozen is the frozen
3648          * string the user gave us in his serializing hook, and @refs, which may
3649          * be empty, is the list of extra references he returned along for us
3650          * to serialize.
3651          *
3652          * In effect, the hook is an alternate creation routine for the class,
3653          * the object itself being already created by the runtime.
3654          */
3655
3656         TRACEME(("calling STORABLE_thaw on %s at 0x%"UVxf" (%"IVdf" args)",
3657                  class, PTR2UV(sv), AvFILLp(av) + 1));
3658
3659         rv = newRV(sv);
3660         (void) scalar_call(rv, hook, clone, av, G_SCALAR|G_DISCARD);
3661         SvREFCNT_dec(rv);
3662
3663         /*
3664          * Final cleanup.
3665          */
3666
3667         SvREFCNT_dec(frozen);
3668         av_undef(av);
3669         sv_free((SV *) av);
3670         if (!(flags & SHF_IDX_CLASSNAME) && class != buf)
3671                 Safefree(class);
3672
3673         /*
3674          * If we had an <extra> type, then the object was not as simple, and
3675          * we need to restore extra magic now.
3676          */
3677
3678         if (!extra_type)
3679                 return sv;
3680
3681         TRACEME(("retrieving magic object for 0x%"UVxf"...", PTR2UV(sv)));
3682
3683         rv = retrieve(cxt, 0);          /* Retrieve <magic object> */
3684
3685         TRACEME(("restoring the magic object 0x%"UVxf" part of 0x%"UVxf,
3686                 PTR2UV(rv), PTR2UV(sv)));
3687
3688         switch (extra_type) {
3689         case SHT_TSCALAR:
3690                 sv_upgrade(sv, SVt_PVMG);
3691                 break;
3692         case SHT_TARRAY:
3693                 sv_upgrade(sv, SVt_PVAV);
3694                 AvREAL_off((AV *)sv);
3695                 break;
3696         case SHT_THASH:
3697                 sv_upgrade(sv, SVt_PVHV);
3698                 break;
3699         default:
3700                 CROAK(("Forgot to deal with extra type %d", extra_type));
3701                 break;
3702         }
3703
3704         /*
3705          * Adding the magic only now, well after the STORABLE_thaw hook was called
3706          * means the hook cannot know it deals with an object whose variable is
3707          * tied.  But this is happening when retrieving $o in the following case:
3708          *
3709          *      my %h;
3710          *  tie %h, 'FOO';
3711          *      my $o = bless \%h, 'BAR';
3712          *
3713          * The 'BAR' class is NOT the one where %h is tied into.  Therefore, as
3714          * far as the 'BAR' class is concerned, the fact that %h is not a REAL
3715          * hash but a tied one should not matter at all, and remain transparent.
3716          * This means the magic must be restored by Storable AFTER the hook is
3717          * called.
3718          *
3719          * That looks very reasonable to me, but then I've come up with this
3720          * after a bug report from David Nesting, who was trying to store such
3721          * an object and caused Storable to fail.  And unfortunately, it was
3722          * also the easiest way to retrofit support for blessed ref to tied objects
3723          * into the existing design.  -- RAM, 17/02/2001
3724          */
3725
3726         sv_magic(sv, rv, mtype, Nullch, 0);
3727         SvREFCNT_dec(rv);                       /* Undo refcnt inc from sv_magic() */
3728
3729         return sv;
3730 }
3731
3732 /*
3733  * retrieve_ref
3734  *
3735  * Retrieve reference to some other scalar.
3736  * Layout is SX_REF <object>, with SX_REF already read.
3737  */
3738 static SV *retrieve_ref(stcxt_t *cxt, char *cname)
3739 {
3740         SV *rv;
3741         SV *sv;
3742
3743         TRACEME(("retrieve_ref (#%d)", cxt->tagnum));
3744
3745         /*
3746          * We need to create the SV that holds the reference to the yet-to-retrieve
3747          * object now, so that we may record the address in the seen table.
3748          * Otherwise, if the object to retrieve references us, we won't be able
3749          * to resolve the SX_OBJECT we'll see at that point! Hence we cannot
3750          * do the retrieve first and use rv = newRV(sv) since it will be too late
3751          * for SEEN() recording.
3752          */
3753
3754         rv = NEWSV(10002, 0);
3755         SEEN(rv, cname);                /* Will return if rv is null */
3756         sv = retrieve(cxt, 0);  /* Retrieve <object> */
3757         if (!sv)
3758                 return (SV *) 0;        /* Failed */
3759
3760         /*
3761          * WARNING: breaks RV encapsulation.
3762          *
3763          * Now for the tricky part. We have to upgrade our existing SV, so that
3764          * it is now an RV on sv... Again, we cheat by duplicating the code
3765          * held in newSVrv(), since we already got our SV from retrieve().
3766          *
3767          * We don't say:
3768          *
3769          *              SvRV(rv) = SvREFCNT_inc(sv);
3770          *
3771          * here because the reference count we got from retrieve() above is
3772          * already correct: if the object was retrieved from the file, then
3773          * its reference count is one. Otherwise, if it was retrieved via
3774          * an SX_OBJECT indication, a ref count increment was done.
3775          */
3776
3777         sv_upgrade(rv, SVt_RV);
3778         SvRV(rv) = sv;                          /* $rv = \$sv */
3779         SvROK_on(rv);
3780
3781         TRACEME(("ok (retrieve_ref at 0x%"UVxf")", PTR2UV(rv)));
3782
3783         return rv;
3784 }
3785
3786 /*
3787  * retrieve_overloaded
3788  *
3789  * Retrieve reference to some other scalar with overloading.
3790  * Layout is SX_OVERLOAD <object>, with SX_OVERLOAD already read.
3791  */
3792 static SV *retrieve_overloaded(stcxt_t *cxt, char *cname)
3793 {
3794         SV *rv;
3795         SV *sv;
3796         HV *stash;
3797
3798         TRACEME(("retrieve_overloaded (#%d)", cxt->tagnum));
3799
3800         /*
3801          * Same code as retrieve_ref(), duplicated to avoid extra call.
3802          */
3803
3804         rv = NEWSV(10002, 0);
3805         SEEN(rv, cname);                /* Will return if rv is null */
3806         sv = retrieve(cxt, 0);  /* Retrieve <object> */
3807         if (!sv)
3808                 return (SV *) 0;        /* Failed */
3809
3810         /*
3811          * WARNING: breaks RV encapsulation.
3812          */
3813
3814         sv_upgrade(rv, SVt_RV);
3815         SvRV(rv) = sv;                          /* $rv = \$sv */
3816         SvROK_on(rv);
3817
3818         /*
3819          * Restore overloading magic.
3820          */
3821
3822         stash = (HV *) SvSTASH (sv);
3823         if (!stash || !Gv_AMG(stash))
3824                 CROAK(("Cannot restore overloading on %s(0x%"UVxf") (package %s)",
3825                        sv_reftype(sv, FALSE),
3826                        PTR2UV(sv),
3827                            stash ? HvNAME(stash) : "<unknown>"));
3828
3829         SvAMAGIC_on(rv);
3830
3831         TRACEME(("ok (retrieve_overloaded at 0x%"UVxf")", PTR2UV(rv)));
3832
3833         return rv;
3834 }
3835
3836 /*
3837  * retrieve_tied_array
3838  *
3839  * Retrieve tied array
3840  * Layout is SX_TIED_ARRAY <object>, with SX_TIED_ARRAY already read.
3841  */
3842 static SV *retrieve_tied_array(stcxt_t *cxt, char *cname)
3843 {
3844         SV *tv;
3845         SV *sv;
3846
3847         TRACEME(("retrieve_tied_array (#%d)", cxt->tagnum));
3848
3849         tv = NEWSV(10002, 0);
3850         SEEN(tv, cname);                        /* Will return if tv is null */
3851         sv = retrieve(cxt, 0);          /* Retrieve <object> */
3852         if (!sv)
3853                 return (SV *) 0;                /* Failed */
3854
3855         sv_upgrade(tv, SVt_PVAV);
3856         AvREAL_off((AV *)tv);
3857         sv_magic(tv, sv, 'P', Nullch, 0);
3858         SvREFCNT_dec(sv);                       /* Undo refcnt inc from sv_magic() */
3859
3860         TRACEME(("ok (retrieve_tied_array at 0x%"UVxf")", PTR2UV(tv)));
3861
3862         return tv;
3863 }
3864
3865 /*
3866  * retrieve_tied_hash
3867  *
3868  * Retrieve tied hash
3869  * Layout is SX_TIED_HASH <object>, with SX_TIED_HASH already read.
3870  */
3871 static SV *retrieve_tied_hash(stcxt_t *cxt, char *cname)
3872 {
3873         SV *tv;
3874         SV *sv;
3875
3876         TRACEME(("retrieve_tied_hash (#%d)", cxt->tagnum));
3877
3878         tv = NEWSV(10002, 0);
3879         SEEN(tv, cname);                        /* Will return if tv is null */
3880         sv = retrieve(cxt, 0);          /* Retrieve <object> */
3881         if (!sv)
3882                 return (SV *) 0;                /* Failed */
3883
3884         sv_upgrade(tv, SVt_PVHV);
3885         sv_magic(tv, sv, 'P', Nullch, 0);
3886         SvREFCNT_dec(sv);                       /* Undo refcnt inc from sv_magic() */
3887
3888         TRACEME(("ok (retrieve_tied_hash at 0x%"UVxf")", PTR2UV(tv)));
3889
3890         return tv;
3891 }
3892
3893 /*
3894  * retrieve_tied_scalar
3895  *
3896  * Retrieve tied scalar
3897  * Layout is SX_TIED_SCALAR <object>, with SX_TIED_SCALAR already read.
3898  */
3899 static SV *retrieve_tied_scalar(stcxt_t *cxt, char *cname)
3900 {
3901         SV *tv;
3902         SV *sv;
3903
3904         TRACEME(("retrieve_tied_scalar (#%d)", cxt->tagnum));
3905
3906         tv = NEWSV(10002, 0);
3907         SEEN(tv, cname);                        /* Will return if rv is null */
3908         sv = retrieve(cxt, 0);          /* Retrieve <object> */
3909         if (!sv)
3910                 return (SV *) 0;                /* Failed */
3911
3912         sv_upgrade(tv, SVt_PVMG);
3913         sv_magic(tv, sv, 'q', Nullch, 0);
3914         SvREFCNT_dec(sv);                       /* Undo refcnt inc from sv_magic() */
3915
3916         TRACEME(("ok (retrieve_tied_scalar at 0x%"UVxf")", PTR2UV(tv)));
3917
3918         return tv;
3919 }
3920
3921 /*
3922  * retrieve_tied_key
3923  *
3924  * Retrieve reference to value in a tied hash.
3925  * Layout is SX_TIED_KEY <object> <key>, with SX_TIED_KEY already read.
3926  */
3927 static SV *retrieve_tied_key(stcxt_t *cxt, char *cname)
3928 {
3929         SV *tv;
3930         SV *sv;
3931         SV *key;
3932
3933         TRACEME(("retrieve_tied_key (#%d)", cxt->tagnum));
3934
3935         tv = NEWSV(10002, 0);
3936         SEEN(tv, cname);                        /* Will return if tv is null */
3937         sv = retrieve(cxt, 0);          /* Retrieve <object> */
3938         if (!sv)
3939                 return (SV *) 0;                /* Failed */
3940
3941         key = retrieve(cxt, 0);         /* Retrieve <key> */
3942         if (!key)
3943                 return (SV *) 0;                /* Failed */
3944
3945         sv_upgrade(tv, SVt_PVMG);
3946         sv_magic(tv, sv, 'p', (char *)key, HEf_SVKEY);
3947         SvREFCNT_dec(key);                      /* Undo refcnt inc from sv_magic() */
3948         SvREFCNT_dec(sv);                       /* Undo refcnt inc from sv_magic() */
3949
3950         return tv;
3951 }
3952
3953 /*
3954  * retrieve_tied_idx
3955  *
3956  * Retrieve reference to value in a tied array.
3957  * Layout is SX_TIED_IDX <object> <idx>, with SX_TIED_IDX already read.
3958  */
3959 static SV *retrieve_tied_idx(stcxt_t *cxt, char *cname)
3960 {
3961         SV *tv;
3962         SV *sv;
3963         I32 idx;
3964
3965         TRACEME(("retrieve_tied_idx (#%d)", cxt->tagnum));
3966
3967         tv = NEWSV(10002, 0);
3968         SEEN(tv, cname);                        /* Will return if tv is null */
3969         sv = retrieve(cxt, 0);          /* Retrieve <object> */
3970         if (!sv)
3971                 return (SV *) 0;                /* Failed */
3972
3973         RLEN(idx);                                      /* Retrieve <idx> */
3974
3975         sv_upgrade(tv, SVt_PVMG);
3976         sv_magic(tv, sv, 'p', Nullch, idx);
3977         SvREFCNT_dec(sv);                       /* Undo refcnt inc from sv_magic() */
3978
3979         return tv;
3980 }
3981
3982
3983 /*
3984  * retrieve_lscalar
3985  *
3986  * Retrieve defined long (string) scalar.
3987  *
3988  * Layout is SX_LSCALAR <length> <data>, with SX_LSCALAR already read.
3989  * The scalar is "long" in that <length> is larger than LG_SCALAR so it
3990  * was not stored on a single byte.
3991  */
3992 static SV *retrieve_lscalar(stcxt_t *cxt, char *cname)
3993 {
3994         I32 len;
3995         SV *sv;
3996
3997         RLEN(len);
3998         TRACEME(("retrieve_lscalar (#%d), len = %"IVdf, cxt->tagnum, len));
3999
4000         /*
4001          * Allocate an empty scalar of the suitable length.
4002          */
4003
4004         sv = NEWSV(10002, len);
4005         SEEN(sv, cname);        /* Associate this new scalar with tag "tagnum" */
4006
4007         /*
4008          * WARNING: duplicates parts of sv_setpv and breaks SV data encapsulation.
4009          *
4010          * Now, for efficiency reasons, read data directly inside the SV buffer,
4011          * and perform the SV final settings directly by duplicating the final
4012          * work done by sv_setpv. Since we're going to allocate lots of scalars
4013          * this way, it's worth the hassle and risk.
4014          */
4015
4016         SAFEREAD(SvPVX(sv), len, sv);
4017         SvCUR_set(sv, len);                             /* Record C string length */
4018         *SvEND(sv) = '\0';                              /* Ensure it's null terminated anyway */
4019         (void) SvPOK_only(sv);                  /* Validate string pointer */
4020         if (cxt->s_tainted)                             /* Is input source tainted? */
4021                 SvTAINT(sv);                            /* External data cannot be trusted */
4022
4023         TRACEME(("large scalar len %"IVdf" '%s'", len, SvPVX(sv)));
4024         TRACEME(("ok (retrieve_lscalar at 0x%"UVxf")", PTR2UV(sv)));
4025
4026         return sv;
4027 }
4028
4029 /*
4030  * retrieve_scalar
4031  *
4032  * Retrieve defined short (string) scalar.
4033  *
4034  * Layout is SX_SCALAR <length> <data>, with SX_SCALAR already read.
4035  * The scalar is "short" so <length> is single byte. If it is 0, there
4036  * is no <data> section.
4037  */
4038 static SV *retrieve_scalar(stcxt_t *cxt, char *cname)
4039 {
4040         int len;
4041         SV *sv;
4042
4043         GETMARK(len);
4044         TRACEME(("retrieve_scalar (#%d), len = %d", cxt->tagnum, len));
4045
4046         /*
4047          * Allocate an empty scalar of the suitable length.
4048          */
4049
4050         sv = NEWSV(10002, len);
4051         SEEN(sv, cname);        /* Associate this new scalar with tag "tagnum" */
4052
4053         /*
4054          * WARNING: duplicates parts of sv_setpv and breaks SV data encapsulation.
4055          */
4056
4057         if (len == 0) {
4058                 /*
4059                  * newSV did not upgrade to SVt_PV so the scalar is undefined.
4060                  * To make it defined with an empty length, upgrade it now...
4061                  * Don't upgrade to a PV if the original type contains more
4062                  * information than a scalar.
4063                  */
4064                 if (SvTYPE(sv) <= SVt_PV) {
4065                         sv_upgrade(sv, SVt_PV);
4066                 }
4067                 SvGROW(sv, 1);
4068                 *SvEND(sv) = '\0';                      /* Ensure it's null terminated anyway */
4069                 TRACEME(("ok (retrieve_scalar empty at 0x%"UVxf")", PTR2UV(sv)));
4070         } else {
4071                 /*
4072                  * Now, for efficiency reasons, read data directly inside the SV buffer,
4073                  * and perform the SV final settings directly by duplicating the final
4074                  * work done by sv_setpv. Since we're going to allocate lots of scalars
4075                  * this way, it's worth the hassle and risk.
4076                  */
4077                 SAFEREAD(SvPVX(sv), len, sv);
4078                 SvCUR_set(sv, len);                     /* Record C string length */
4079                 *SvEND(sv) = '\0';                      /* Ensure it's null terminated anyway */
4080                 TRACEME(("small scalar len %d '%s'", len, SvPVX(sv)));
4081         }
4082
4083         (void) SvPOK_only(sv);                  /* Validate string pointer */
4084         if (cxt->s_tainted)                             /* Is input source tainted? */
4085                 SvTAINT(sv);                            /* External data cannot be trusted */
4086
4087         TRACEME(("ok (retrieve_scalar at 0x%"UVxf")", PTR2UV(sv)));
4088         return sv;
4089 }
4090
4091 /*
4092  * retrieve_utf8str
4093  *
4094  * Like retrieve_scalar(), but tag result as utf8.
4095  * If we're retrieving UTF8 data in a non-UTF8 perl, croaks.
4096  */
4097 static SV *retrieve_utf8str(stcxt_t *cxt, char *cname)
4098 {
4099         SV *sv;
4100
4101         TRACEME(("retrieve_utf8str"));
4102
4103         sv = retrieve_scalar(cxt, cname);
4104         if (sv)
4105                 SvUTF8_on(sv);
4106
4107         return sv;
4108 }
4109
4110 /*
4111  * retrieve_lutf8str
4112  *
4113  * Like retrieve_lscalar(), but tag result as utf8.
4114  * If we're retrieving UTF8 data in a non-UTF8 perl, croaks.
4115  */
4116 static SV *retrieve_lutf8str(stcxt_t *cxt, char *cname)
4117 {
4118         SV *sv;
4119
4120         TRACEME(("retrieve_lutf8str"));
4121
4122         sv = retrieve_lscalar(cxt, cname);
4123         if (sv)
4124                 SvUTF8_on(sv);
4125
4126         return sv;
4127 }
4128
4129 /*
4130  * retrieve_integer
4131  *
4132  * Retrieve defined integer.
4133  * Layout is SX_INTEGER <data>, whith SX_INTEGER already read.
4134  */
4135 static SV *retrieve_integer(stcxt_t *cxt, char *cname)
4136 {
4137         SV *sv;
4138         IV iv;
4139
4140         TRACEME(("retrieve_integer (#%d)", cxt->tagnum));
4141
4142         READ(&iv, sizeof(iv));
4143         sv = newSViv(iv);
4144         SEEN(sv, cname);        /* Associate this new scalar with tag "tagnum" */
4145
4146         TRACEME(("integer %"IVdf, iv));
4147         TRACEME(("ok (retrieve_integer at 0x%"UVxf")", PTR2UV(sv)));
4148
4149         return sv;
4150 }
4151
4152 /*
4153  * retrieve_netint
4154  *
4155  * Retrieve defined integer in network order.
4156  * Layout is SX_NETINT <data>, whith SX_NETINT already read.
4157  */
4158 static SV *retrieve_netint(stcxt_t *cxt, char *cname)
4159 {
4160         SV *sv;
4161         I32 iv;
4162
4163         TRACEME(("retrieve_netint (#%d)", cxt->tagnum));
4164
4165         READ_I32(iv);
4166 #ifdef HAS_NTOHL
4167         sv = newSViv((int) ntohl(iv));
4168         TRACEME(("network integer %d", (int) ntohl(iv)));
4169 #else
4170         sv = newSViv(iv);
4171         TRACEME(("network integer (as-is) %d", iv));
4172 #endif
4173         SEEN(sv, cname);        /* Associate this new scalar with tag "tagnum" */
4174
4175         TRACEME(("ok (retrieve_netint at 0x%"UVxf")", PTR2UV(sv)));
4176
4177         return sv;
4178 }
4179
4180 /*
4181  * retrieve_double
4182  *
4183  * Retrieve defined double.
4184  * Layout is SX_DOUBLE <data>, whith SX_DOUBLE already read.
4185  */
4186 static SV *retrieve_double(stcxt_t *cxt, char *cname)
4187 {
4188         SV *sv;
4189         NV nv;
4190
4191         TRACEME(("retrieve_double (#%d)", cxt->tagnum));
4192
4193         READ(&nv, sizeof(nv));
4194         sv = newSVnv(nv);
4195         SEEN(sv, cname);        /* Associate this new scalar with tag "tagnum" */
4196
4197         TRACEME(("double %"NVff, nv));
4198         TRACEME(("ok (retrieve_double at 0x%"UVxf")", PTR2UV(sv)));
4199
4200         return sv;
4201 }
4202
4203 /*
4204  * retrieve_byte
4205  *
4206  * Retrieve defined byte (small integer within the [-128, +127] range).
4207  * Layout is SX_BYTE <data>, whith SX_BYTE already read.
4208  */
4209 static SV *retrieve_byte(stcxt_t *cxt, char *cname)
4210 {
4211         SV *sv;
4212         int siv;
4213         signed char tmp;        /* Workaround for AIX cc bug --H.Merijn Brand */
4214
4215         TRACEME(("retrieve_byte (#%d)", cxt->tagnum));
4216
4217         GETMARK(siv);
4218         TRACEME(("small integer read as %d", (unsigned char) siv));
4219         tmp = (unsigned char) siv - 128;
4220         sv = newSViv(tmp);
4221         SEEN(sv, cname);        /* Associate this new scalar with tag "tagnum" */
4222
4223         TRACEME(("byte %d", tmp));
4224         TRACEME(("ok (retrieve_byte at 0x%"UVxf")", PTR2UV(sv)));
4225
4226         return sv;
4227 }
4228
4229 /*
4230  * retrieve_undef
4231  *
4232  * Return the undefined value.
4233  */
4234 static SV *retrieve_undef(stcxt_t *cxt, char *cname)
4235 {
4236         SV* sv;
4237
4238         TRACEME(("retrieve_undef"));
4239
4240         sv = newSV(0);
4241         SEEN(sv, cname);
4242
4243         return sv;
4244 }
4245
4246 /*
4247  * retrieve_sv_undef
4248  *
4249  * Return the immortal undefined value.
4250  */
4251 static SV *retrieve_sv_undef(stcxt_t *cxt, char *cname)
4252 {
4253         SV *sv = &PL_sv_undef;
4254
4255         TRACEME(("retrieve_sv_undef"));
4256
4257         SEEN(sv, cname);
4258         return sv;
4259 }
4260
4261 /*
4262  * retrieve_sv_yes
4263  *
4264  * Return the immortal yes value.
4265  */
4266 static SV *retrieve_sv_yes(stcxt_t *cxt, char *cname)
4267 {
4268         SV *sv = &PL_sv_yes;
4269
4270         TRACEME(("retrieve_sv_yes"));
4271
4272         SEEN(sv, cname);
4273         return sv;
4274 }
4275
4276 /*
4277  * retrieve_sv_no
4278  *
4279  * Return the immortal no value.
4280  */
4281 static SV *retrieve_sv_no(stcxt_t *cxt, char *cname)
4282 {
4283         SV *sv = &PL_sv_no;
4284
4285         TRACEME(("retrieve_sv_no"));
4286
4287         SEEN(sv, cname);
4288         return sv;
4289 }
4290
4291 /*
4292  * retrieve_array
4293  *
4294  * Retrieve a whole array.
4295  * Layout is SX_ARRAY <size> followed by each item, in increading index order.
4296  * Each item is stored as <object>.
4297  *
4298  * When we come here, SX_ARRAY has been read already.
4299  */
4300 static SV *retrieve_array(stcxt_t *cxt, char *cname)
4301 {
4302         I32 len;
4303         I32 i;
4304         AV *av;
4305         SV *sv;
4306
4307         TRACEME(("retrieve_array (#%d)", cxt->tagnum));
4308
4309         /*
4310          * Read length, and allocate array, then pre-extend it.
4311          */
4312
4313         RLEN(len);
4314         TRACEME(("size = %d", len));
4315         av = newAV();
4316         SEEN(av, cname);                        /* Will return if array not allocated nicely */
4317         if (len)
4318                 av_extend(av, len);
4319         else
4320                 return (SV *) av;               /* No data follow if array is empty */
4321
4322         /*
4323          * Now get each item in turn...
4324          */
4325
4326         for (i = 0; i < len; i++) {
4327                 TRACEME(("(#%d) item", i));
4328                 sv = retrieve(cxt, 0);                  /* Retrieve item */
4329                 if (!sv)
4330                         return (SV *) 0;
4331                 if (av_store(av, i, sv) == 0)
4332                         return (SV *) 0;
4333         }
4334
4335         TRACEME(("ok (retrieve_array at 0x%"UVxf")", PTR2UV(av)));
4336
4337         return (SV *) av;
4338 }
4339
4340 /*
4341  * retrieve_hash
4342  *
4343  * Retrieve a whole hash table.
4344  * Layout is SX_HASH <size> followed by each key/value pair, in random order.
4345  * Keys are stored as <length> <data>, the <data> section being omitted
4346  * if length is 0.
4347  * Values are stored as <object>.
4348  *
4349  * When we come here, SX_HASH has been read already.
4350  */
4351 static SV *retrieve_hash(stcxt_t *cxt, char *cname)
4352 {
4353         I32 len;
4354         I32 size;
4355         I32 i;
4356         HV *hv;
4357         SV *sv;
4358
4359         TRACEME(("retrieve_hash (#%d)", cxt->tagnum));
4360
4361         /*
4362          * Read length, allocate table.
4363          */
4364
4365         RLEN(len);
4366         TRACEME(("size = %d", len));
4367         hv = newHV();
4368         SEEN(hv, cname);                /* Will return if table not allocated properly */
4369         if (len == 0)
4370                 return (SV *) hv;       /* No data follow if table empty */
4371         hv_ksplit(hv, len);             /* pre-extend hash to save multiple splits */
4372
4373         /*
4374          * Now get each key/value pair in turn...
4375          */
4376
4377         for (i = 0; i < len; i++) {
4378                 /*
4379                  * Get value first.
4380                  */
4381
4382                 TRACEME(("(#%d) value", i));
4383                 sv = retrieve(cxt, 0);
4384                 if (!sv)
4385                         return (SV *) 0;
4386
4387                 /*
4388                  * Get key.
4389                  * Since we're reading into kbuf, we must ensure we're not
4390                  * recursing between the read and the hv_store() where it's used.
4391                  * Hence the key comes after the value.
4392                  */
4393
4394                 RLEN(size);                                             /* Get key size */
4395                 KBUFCHK(size);                                  /* Grow hash key read pool if needed */
4396                 if (size)
4397                         READ(kbuf, size);
4398                 kbuf[size] = '\0';                              /* Mark string end, just in case */
4399                 TRACEME(("(#%d) key '%s'", i, kbuf));
4400
4401                 /*
4402                  * Enter key/value pair into hash table.
4403                  */
4404
4405                 if (hv_store(hv, kbuf, (U32) size, sv, 0) == 0)
4406                         return (SV *) 0;
4407         }
4408
4409         TRACEME(("ok (retrieve_hash at 0x%"UVxf")", PTR2UV(hv)));
4410
4411         return (SV *) hv;
4412 }
4413
4414 /*
4415  * retrieve_hash
4416  *
4417  * Retrieve a whole hash table.
4418  * Layout is SX_HASH <size> followed by each key/value pair, in random order.
4419  * Keys are stored as <length> <data>, the <data> section being omitted
4420  * if length is 0.
4421  * Values are stored as <object>.
4422  *
4423  * When we come here, SX_HASH has been read already.
4424  */
4425 static SV *retrieve_flag_hash(stcxt_t *cxt, char *cname)
4426 {
4427     I32 len;
4428     I32 size;
4429     I32 i;
4430     HV *hv;
4431     SV *sv;
4432     int hash_flags;
4433
4434     GETMARK(hash_flags);
4435         TRACEME(("retrieve_flag_hash (#%d)", cxt->tagnum));
4436     /*
4437      * Read length, allocate table.
4438      */
4439
4440     RLEN(len);
4441     TRACEME(("size = %d, flags = %d", len, hash_flags));
4442     hv = newHV();
4443     SEEN(hv, cname);            /* Will return if table not allocated properly */
4444     if (len == 0)
4445         return (SV *) hv;       /* No data follow if table empty */
4446     hv_ksplit(hv, len);         /* pre-extend hash to save multiple splits */
4447
4448     /*
4449      * Now get each key/value pair in turn...
4450      */
4451
4452     for (i = 0; i < len; i++) {
4453         int flags;
4454         int store_flags = 0;
4455         /*
4456          * Get value first.
4457          */
4458
4459         TRACEME(("(#%d) value", i));
4460         sv = retrieve(cxt, 0);
4461         if (!sv)
4462             return (SV *) 0;
4463
4464         GETMARK(flags);
4465         if ((hash_flags & SHV_RESTRICTED) && (flags & SHV_K_LOCKED))
4466             SvREADONLY_on(sv);
4467
4468         if (flags & SHV_K_ISSV) {
4469             /* XXX you can't set a placeholder with an SV key.
4470                Then again, you can't get an SV key.
4471                Without messing around beyond what the API is supposed to do.
4472             */
4473             SV *keysv;
4474             TRACEME(("(#%d) keysv, flags=%d", i, flags));
4475             keysv = retrieve(cxt, 0);
4476             if (!keysv)
4477                 return (SV *) 0;
4478
4479             if (!hv_store_ent(hv, keysv, sv, 0))
4480                 return (SV *) 0;
4481         } else {
4482             /*
4483              * Get key.
4484              * Since we're reading into kbuf, we must ensure we're not
4485              * recursing between the read and the hv_store() where it's used.
4486              * Hence the key comes after the value.
4487              */
4488
4489             if (flags & SHV_K_PLACEHOLDER) {
4490                 SvREFCNT_dec (sv);
4491                 sv = &PL_sv_undef;
4492                 store_flags |= HVhek_PLACEHOLD;
4493             }
4494             if (flags & SHV_K_UTF8)
4495                 store_flags |= HVhek_UTF8;
4496             if (flags & SHV_K_WASUTF8)
4497                 store_flags |= HVhek_WASUTF8;
4498
4499             RLEN(size);                                         /* Get key size */
4500             KBUFCHK(size);                                      /* Grow hash key read pool if needed */
4501             if (size)
4502                 READ(kbuf, size);
4503             kbuf[size] = '\0';                          /* Mark string end, just in case */
4504             TRACEME(("(#%d) key '%s' flags %X store_flags %X", i, kbuf,
4505                      flags, store_flags));
4506
4507             /*
4508              * Enter key/value pair into hash table.
4509              */
4510
4511             if (hv_store_flags(hv, kbuf, size, sv, 0, flags) == 0)
4512                 return (SV *) 0;
4513         }
4514     }
4515     if (hash_flags & SHV_RESTRICTED)
4516         SvREADONLY_on(hv);
4517
4518     TRACEME(("ok (retrieve_hash at 0x%"UVxf")", PTR2UV(hv)));
4519
4520     return (SV *) hv;
4521 }
4522
4523 /*
4524  * old_retrieve_array
4525  *
4526  * Retrieve a whole array in pre-0.6 binary format.
4527  *
4528  * Layout is SX_ARRAY <size> followed by each item, in increading index order.
4529  * Each item is stored as SX_ITEM <object> or SX_IT_UNDEF for "holes".
4530  *
4531  * When we come here, SX_ARRAY has been read already.
4532  */
4533 static SV *old_retrieve_array(stcxt_t *cxt, char *cname)
4534 {
4535         I32 len;
4536         I32 i;
4537         AV *av;
4538         SV *sv;
4539         int c;
4540
4541         TRACEME(("old_retrieve_array (#%d)", cxt->tagnum));
4542
4543         /*
4544          * Read length, and allocate array, then pre-extend it.
4545          */
4546
4547         RLEN(len);
4548         TRACEME(("size = %d", len));
4549         av = newAV();
4550         SEEN(av, 0);                            /* Will return if array not allocated nicely */
4551         if (len)
4552                 av_extend(av, len);
4553         else
4554                 return (SV *) av;               /* No data follow if array is empty */
4555
4556         /*
4557          * Now get each item in turn...
4558          */
4559
4560         for (i = 0; i < len; i++) {
4561                 GETMARK(c);
4562                 if (c == SX_IT_UNDEF) {
4563                         TRACEME(("(#%d) undef item", i));
4564                         continue;                       /* av_extend() already filled us with undef */
4565                 }
4566                 if (c != SX_ITEM)
4567                         (void) retrieve_other((stcxt_t *) 0, 0);        /* Will croak out */
4568                 TRACEME(("(#%d) item", i));
4569                 sv = retrieve(cxt, 0);                                          /* Retrieve item */
4570                 if (!sv)
4571                         return (SV *) 0;
4572                 if (av_store(av, i, sv) == 0)
4573                         return (SV *) 0;
4574         }
4575
4576         TRACEME(("ok (old_retrieve_array at 0x%"UVxf")", PTR2UV(av)));
4577
4578         return (SV *) av;
4579 }
4580
4581 /*
4582  * old_retrieve_hash
4583  *
4584  * Retrieve a whole hash table in pre-0.6 binary format.
4585  *
4586  * Layout is SX_HASH <size> followed by each key/value pair, in random order.
4587  * Keys are stored as SX_KEY <length> <data>, the <data> section being omitted
4588  * if length is 0.
4589  * Values are stored as SX_VALUE <object> or SX_VL_UNDEF for "holes".
4590  *
4591  * When we come here, SX_HASH has been read already.
4592  */
4593 static SV *old_retrieve_hash(stcxt_t *cxt, char *cname)
4594 {
4595         I32 len;
4596         I32 size;
4597         I32 i;
4598         HV *hv;
4599         SV *sv = (SV *) 0;
4600         int c;
4601         static SV *sv_h_undef = (SV *) 0;               /* hv_store() bug */
4602
4603         TRACEME(("old_retrieve_hash (#%d)", cxt->tagnum));
4604
4605         /*
4606          * Read length, allocate table.
4607          */
4608
4609         RLEN(len);
4610         TRACEME(("size = %d", len));
4611         hv = newHV();
4612         SEEN(hv, 0);                    /* Will return if table not allocated properly */
4613         if (len == 0)
4614                 return (SV *) hv;       /* No data follow if table empty */
4615         hv_ksplit(hv, len);             /* pre-extend hash to save multiple splits */
4616
4617         /*
4618          * Now get each key/value pair in turn...
4619          */
4620
4621         for (i = 0; i < len; i++) {
4622                 /*
4623                  * Get value first.
4624                  */
4625
4626                 GETMARK(c);
4627                 if (c == SX_VL_UNDEF) {
4628                         TRACEME(("(#%d) undef value", i));
4629                         /*
4630                          * Due to a bug in hv_store(), it's not possible to pass
4631                          * &PL_sv_undef to hv_store() as a value, otherwise the
4632                          * associated key will not be creatable any more. -- RAM, 14/01/97
4633                          */
4634                         if (!sv_h_undef)
4635                                 sv_h_undef = newSVsv(&PL_sv_undef);
4636                         sv = SvREFCNT_inc(sv_h_undef);
4637                 } else if (c == SX_VALUE) {
4638                         TRACEME(("(#%d) value", i));
4639                         sv = retrieve(cxt, 0);
4640                         if (!sv)
4641                                 return (SV *) 0;
4642                 } else
4643                         (void) retrieve_other((stcxt_t *) 0, 0);        /* Will croak out */
4644
4645                 /*
4646                  * Get key.
4647                  * Since we're reading into kbuf, we must ensure we're not
4648                  * recursing between the read and the hv_store() where it's used.
4649                  * Hence the key comes after the value.
4650                  */
4651
4652                 GETMARK(c);
4653                 if (c != SX_KEY)
4654                         (void) retrieve_other((stcxt_t *) 0, 0);        /* Will croak out */
4655                 RLEN(size);                                             /* Get key size */
4656                 KBUFCHK(size);                                  /* Grow hash key read pool if needed */
4657                 if (size)
4658                         READ(kbuf, size);
4659                 kbuf[size] = '\0';                              /* Mark string end, just in case */
4660                 TRACEME(("(#%d) key '%s'", i, kbuf));
4661
4662                 /*
4663                  * Enter key/value pair into hash table.
4664                  */
4665
4666                 if (hv_store(hv, kbuf, (U32) size, sv, 0) == 0)
4667                         return (SV *) 0;
4668         }
4669
4670         TRACEME(("ok (retrieve_hash at 0x%"UVxf")", PTR2UV(hv)));
4671
4672         return (SV *) hv;
4673 }
4674
4675 /***
4676  *** Retrieval engine.
4677  ***/
4678
4679 /*
4680  * magic_check
4681  *
4682  * Make sure the stored data we're trying to retrieve has been produced
4683  * on an ILP compatible system with the same byteorder. It croaks out in
4684  * case an error is detected. [ILP = integer-long-pointer sizes]
4685  * Returns null if error is detected, &PL_sv_undef otherwise.
4686  *
4687  * Note that there's no byte ordering info emitted when network order was
4688  * used at store time.
4689  */
4690 static SV *magic_check(stcxt_t *cxt)
4691 {
4692         char buf[256];
4693         char byteorder[256];
4694         int c;
4695         int use_network_order;
4696         int version_major;
4697         int version_minor = 0;
4698
4699         TRACEME(("magic_check"));
4700
4701         /*
4702          * The "magic number" is only for files, not when freezing in memory.
4703          */
4704
4705         if (cxt->fio) {
4706                 STRLEN len = sizeof(magicstr) - 1;
4707                 STRLEN old_len;
4708
4709                 READ(buf, len);                                 /* Not null-terminated */
4710                 buf[len] = '\0';                                /* Is now */
4711
4712                 if (0 == strcmp(buf, magicstr))
4713                         goto magic_ok;
4714
4715                 /*
4716                  * Try to read more bytes to check for the old magic number, which
4717                  * was longer.
4718                  */
4719
4720                 old_len = sizeof(old_magicstr) - 1;
4721                 READ(&buf[len], old_len - len);
4722                 buf[old_len] = '\0';                    /* Is now null-terminated */
4723
4724                 if (strcmp(buf, old_magicstr))
4725                         CROAK(("File is not a perl storable"));
4726         }
4727
4728 magic_ok:
4729         /*
4730          * Starting with 0.6, the "use_network_order" byte flag is also used to
4731          * indicate the version number of the binary, and therefore governs the
4732          * setting of sv_retrieve_vtbl. See magic_write().
4733          */
4734
4735         GETMARK(use_network_order);
4736         version_major = use_network_order >> 1;
4737         cxt->retrieve_vtbl = version_major ? sv_retrieve : sv_old_retrieve;
4738
4739         TRACEME(("magic_check: netorder = 0x%x", use_network_order));
4740
4741
4742         /*
4743          * Starting with 0.7 (binary major 2), a full byte is dedicated to the
4744          * minor version of the protocol.  See magic_write().
4745          */
4746
4747         if (version_major > 1)
4748                 GETMARK(version_minor);
4749
4750         cxt->ver_major = version_major;
4751         cxt->ver_minor = version_minor;
4752
4753         TRACEME(("binary image version is %d.%d", version_major, version_minor));
4754
4755         /*
4756          * Inter-operability sanity check: we can't retrieve something stored
4757          * using a format more recent than ours, because we have no way to
4758          * know what has changed, and letting retrieval go would mean a probable
4759          * failure reporting a "corrupted" storable file.
4760          */
4761
4762         if (
4763                 version_major > STORABLE_BIN_MAJOR ||
4764                         (version_major == STORABLE_BIN_MAJOR &&
4765                         version_minor > STORABLE_BIN_MINOR)
4766         )
4767                 CROAK(("Storable binary image v%d.%d more recent than I am (v%d.%d)",
4768                         version_major, version_minor,
4769                         STORABLE_BIN_MAJOR, STORABLE_BIN_MINOR));
4770
4771         /*
4772          * If they stored using network order, there's no byte ordering
4773          * information to check.
4774          */
4775
4776         if ((cxt->netorder = (use_network_order & 0x1)))        /* Extra () for -Wall */
4777                 return &PL_sv_undef;                    /* No byte ordering info */
4778
4779         sprintf(byteorder, "%lx", (unsigned long) BYTEORDER);
4780         GETMARK(c);
4781         READ(buf, c);                                           /* Not null-terminated */
4782         buf[c] = '\0';                                          /* Is now */
4783
4784         if (strcmp(buf, byteorder))
4785                 CROAK(("Byte order is not compatible"));
4786         
4787         GETMARK(c);             /* sizeof(int) */
4788         if ((int) c != sizeof(int))
4789                 CROAK(("Integer size is not compatible"));
4790
4791         GETMARK(c);             /* sizeof(long) */
4792         if ((int) c != sizeof(long))
4793                 CROAK(("Long integer size is not compatible"));
4794
4795         GETMARK(c);             /* sizeof(char *) */
4796         if ((int) c != sizeof(char *))
4797                 CROAK(("Pointer integer size is not compatible"));
4798
4799         if (version_major >= 2 && version_minor >= 2) {
4800                 GETMARK(c);             /* sizeof(NV) */
4801                 if ((int) c != sizeof(NV))
4802                         CROAK(("Double size is not compatible"));
4803         }
4804
4805         return &PL_sv_undef;    /* OK */
4806 }
4807
4808 /*
4809  * retrieve
4810  *
4811  * Recursively retrieve objects from the specified file and return their
4812  * root SV (which may be an AV or an HV for what we care).
4813  * Returns null if there is a problem.
4814  */
4815 static SV *retrieve(stcxt_t *cxt, char *cname)
4816 {
4817         int type;
4818         SV **svh;
4819         SV *sv;
4820
4821         TRACEME(("retrieve"));
4822
4823         /*
4824          * Grab address tag which identifies the object if we are retrieving
4825          * an older format. Since the new binary format counts objects and no
4826          * longer explicitely tags them, we must keep track of the correspondance
4827          * ourselves.
4828          *
4829          * The following section will disappear one day when the old format is
4830          * no longer supported, hence the final "goto" in the "if" block.
4831          */
4832
4833         if (cxt->hseen) {                                               /* Retrieving old binary */
4834                 stag_t tag;
4835                 if (cxt->netorder) {
4836                         I32 nettag;
4837                         READ(&nettag, sizeof(I32));             /* Ordered sequence of I32 */
4838                         tag = (stag_t) nettag;
4839                 } else
4840                         READ(&tag, sizeof(stag_t));             /* Original address of the SV */
4841
4842                 GETMARK(type);
4843                 if (type == SX_OBJECT) {
4844                         I32 tagn;
4845                         svh = hv_fetch(cxt->hseen, (char *) &tag, sizeof(tag), FALSE);
4846                         if (!svh)
4847                                 CROAK(("Old tag 0x%"UVxf" should have been mapped already",
4848                                         (UV) tag));
4849                         tagn = SvIV(*svh);      /* Mapped tag number computed earlier below */
4850
4851                         /*
4852                          * The following code is common with the SX_OBJECT case below.
4853                          */
4854
4855                         svh = av_fetch(cxt->aseen, tagn, FALSE);
4856                         if (!svh)
4857                                 CROAK(("Object #%"IVdf" should have been retrieved already",
4858                                         (IV) tagn));
4859                         sv = *svh;
4860                         TRACEME(("has retrieved #%d at 0x%"UVxf, tagn, PTR2UV(sv)));
4861                         SvREFCNT_inc(sv);       /* One more reference to this same sv */
4862                         return sv;                      /* The SV pointer where object was retrieved */
4863                 }
4864
4865                 /*
4866                  * Map new object, but don't increase tagnum. This will be done
4867                  * by each of the retrieve_* functions when they call SEEN().
4868                  *
4869                  * The mapping associates the "tag" initially present with a unique
4870                  * tag number. See test for SX_OBJECT above to see how this is perused.
4871                  */
4872
4873                 if (!hv_store(cxt->hseen, (char *) &tag, sizeof(tag),
4874                                 newSViv(cxt->tagnum), 0))
4875                         return (SV *) 0;
4876
4877                 goto first_time;
4878         }
4879
4880         /*
4881          * Regular post-0.6 binary format.
4882          */
4883
4884         GETMARK(type);
4885
4886         TRACEME(("retrieve type = %d", type));
4887
4888         /*
4889          * Are we dealing with an object we should have already retrieved?
4890          */
4891
4892         if (type == SX_OBJECT) {
4893                 I32 tag;
4894                 READ_I32(tag);
4895                 tag = ntohl(tag);
4896                 svh = av_fetch(cxt->aseen, tag, FALSE);
4897                 if (!svh)
4898                         CROAK(("Object #%"IVdf" should have been retrieved already",
4899                                 (IV) tag));
4900                 sv = *svh;
4901                 TRACEME(("had retrieved #%d at 0x%"UVxf, tag, PTR2UV(sv)));
4902                 SvREFCNT_inc(sv);       /* One more reference to this same sv */
4903                 return sv;                      /* The SV pointer where object was retrieved */
4904         }
4905
4906 first_time:             /* Will disappear when support for old format is dropped */
4907
4908         /*
4909          * Okay, first time through for this one.
4910          */
4911
4912         sv = RETRIEVE(cxt, type)(cxt, cname);
4913         if (!sv)
4914                 return (SV *) 0;                        /* Failed */
4915
4916         /*
4917          * Old binary formats (pre-0.7).
4918          *
4919          * Final notifications, ended by SX_STORED may now follow.
4920          * Currently, the only pertinent notification to apply on the
4921          * freshly retrieved object is either:
4922          *    SX_CLASS <char-len> <classname> for short classnames.
4923          *    SX_LG_CLASS <int-len> <classname> for larger one (rare!).
4924          * Class name is then read into the key buffer pool used by
4925          * hash table key retrieval.
4926          */
4927
4928         if (cxt->ver_major < 2) {
4929                 while ((type = GETCHAR()) != SX_STORED) {
4930                         I32 len;
4931                         switch (type) {
4932                         case SX_CLASS:
4933                                 GETMARK(len);                   /* Length coded on a single char */
4934                                 break;
4935                         case SX_LG_CLASS:                       /* Length coded on a regular integer */
4936                                 RLEN(len);
4937                                 break;
4938                         case EOF:
4939                         default:
4940                                 return (SV *) 0;                /* Failed */
4941                         }
4942                         KBUFCHK(len);                           /* Grow buffer as necessary */
4943                         if (len)
4944                                 READ(kbuf, len);
4945                         kbuf[len] = '\0';                       /* Mark string end */
4946                         BLESS(sv, kbuf);
4947                 }
4948         }
4949
4950         TRACEME(("ok (retrieved 0x%"UVxf", refcnt=%d, %s)", PTR2UV(sv),
4951                 SvREFCNT(sv) - 1, sv_reftype(sv, FALSE)));
4952
4953         return sv;      /* Ok */
4954 }
4955
4956 /*
4957  * do_retrieve
4958  *
4959  * Retrieve data held in file and return the root object.
4960  * Common routine for pretrieve and mretrieve.
4961  */
4962 static SV *do_retrieve(
4963         PerlIO *f,
4964         SV *in,
4965         int optype)
4966 {
4967         dSTCXT;
4968         SV *sv;
4969         int is_tainted;                         /* Is input source tainted? */
4970         int pre_06_fmt = 0;                     /* True with pre Storable 0.6 formats */
4971
4972         TRACEME(("do_retrieve (optype = 0x%x)", optype));
4973
4974         optype |= ST_RETRIEVE;
4975
4976         /*
4977          * Sanity assertions for retrieve dispatch tables.
4978          */
4979
4980         ASSERT(sizeof(sv_old_retrieve) == sizeof(sv_retrieve),
4981                 ("old and new retrieve dispatch table have same size"));
4982         ASSERT(sv_old_retrieve[SX_ERROR] == retrieve_other,
4983                 ("SX_ERROR entry correctly initialized in old dispatch table"));
4984         ASSERT(sv_retrieve[SX_ERROR] == retrieve_other,
4985                 ("SX_ERROR entry correctly initialized in new dispatch table"));
4986
4987         /*
4988          * Workaround for CROAK leak: if they enter with a "dirty" context,
4989          * free up memory for them now.
4990          */
4991
4992         if (cxt->s_dirty)
4993                 clean_context(cxt);
4994
4995         /*
4996          * Now that STORABLE_xxx hooks exist, it is possible that they try to
4997          * re-enter retrieve() via the hooks.
4998          */
4999
5000         if (cxt->entry)
5001                 cxt = allocate_context(cxt);
5002
5003         cxt->entry++;
5004
5005         ASSERT(cxt->entry == 1, ("starting new recursion"));
5006         ASSERT(!cxt->s_dirty, ("clean context"));
5007
5008         /*
5009          * Prepare context.
5010          *
5011          * Data is loaded into the memory buffer when f is NULL, unless `in' is
5012          * also NULL, in which case we're expecting the data to already lie
5013          * in the buffer (dclone case).
5014          */
5015
5016         KBUFINIT();                                     /* Allocate hash key reading pool once */
5017
5018         if (!f && in)
5019                 MBUF_SAVE_AND_LOAD(in);
5020
5021         /*
5022          * Magic number verifications.
5023          *
5024          * This needs to be done before calling init_retrieve_context()
5025          * since the format indication in the file are necessary to conduct
5026          * some of the initializations.
5027          */
5028
5029         cxt->fio = f;                           /* Where I/O are performed */
5030
5031         if (!magic_check(cxt))
5032                 CROAK(("Magic number checking on storable %s failed",
5033                         cxt->fio ? "file" : "string"));
5034
5035         TRACEME(("data stored in %s format",
5036                 cxt->netorder ? "net order" : "native"));
5037
5038         /*
5039          * Check whether input source is tainted, so that we don't wrongly
5040          * taint perfectly good values...
5041          *
5042          * We assume file input is always tainted.  If both `f' and `in' are
5043          * NULL, then we come from dclone, and tainted is already filled in
5044          * the context.  That's a kludge, but the whole dclone() thing is
5045          * already quite a kludge anyway! -- RAM, 15/09/2000.
5046          */
5047
5048         is_tainted = f ? 1 : (in ? SvTAINTED(in) : cxt->s_tainted);
5049         TRACEME(("input source is %s", is_tainted ? "tainted" : "trusted"));
5050         init_retrieve_context(cxt, optype, is_tainted);
5051
5052         ASSERT(is_retrieving(), ("within retrieve operation"));
5053
5054         sv = retrieve(cxt, 0);          /* Recursively retrieve object, get root SV */
5055
5056         /*
5057          * Final cleanup.
5058          */
5059
5060         if (!f && in)
5061                 MBUF_RESTORE();
5062
5063         pre_06_fmt = cxt->hseen != NULL;        /* Before we clean context */
5064
5065         /*
5066          * The "root" context is never freed.
5067          */
5068
5069         clean_retrieve_context(cxt);
5070         if (cxt->prev)                          /* This context was stacked */
5071                 free_context(cxt);              /* It was not the "root" context */
5072
5073         /*
5074          * Prepare returned value.
5075          */
5076
5077         if (!sv) {
5078                 TRACEME(("retrieve ERROR"));
5079                 return &PL_sv_undef;            /* Something went wrong, return undef */
5080         }
5081
5082         TRACEME(("retrieve got %s(0x%"UVxf")",
5083                 sv_reftype(sv, FALSE), PTR2UV(sv)));
5084
5085         /*
5086          * Backward compatibility with Storable-0.5@9 (which we know we
5087          * are retrieving if hseen is non-null): don't create an extra RV
5088          * for objects since we special-cased it at store time.
5089          *
5090          * Build a reference to the SV returned by pretrieve even if it is
5091          * already one and not a scalar, for consistency reasons.
5092          */
5093
5094         if (pre_06_fmt) {                       /* Was not handling overloading by then */
5095                 SV *rv;
5096                 TRACEME(("fixing for old formats -- pre 0.6"));
5097                 if (sv_type(sv) == svis_REF && (rv = SvRV(sv)) && SvOBJECT(rv)) {
5098                         TRACEME(("ended do_retrieve() with an object -- pre 0.6"));
5099                         return sv;
5100                 }
5101         }
5102
5103         /*
5104          * If reference is overloaded, restore behaviour.
5105          *
5106          * NB: minor glitch here: normally, overloaded refs are stored specially
5107          * so that we can croak when behaviour cannot be re-installed, and also
5108          * avoid testing for overloading magic at each reference retrieval.
5109          *
5110          * Unfortunately, the root reference is implicitely stored, so we must
5111          * check for possible overloading now.  Furthermore, if we don't restore
5112          * overloading, we cannot croak as if the original ref was, because we
5113          * have no way to determine whether it was an overloaded ref or not in
5114          * the first place.
5115          *
5116          * It's a pity that overloading magic is attached to the rv, and not to
5117          * the underlying sv as blessing is.
5118          */
5119
5120         if (SvOBJECT(sv)) {
5121                 HV *stash = (HV *) SvSTASH(sv);
5122                 SV *rv = newRV_noinc(sv);
5123                 if (stash && Gv_AMG(stash)) {
5124                         SvAMAGIC_on(rv);
5125                         TRACEME(("restored overloading on root reference"));
5126                 }
5127                 TRACEME(("ended do_retrieve() with an object"));
5128                 return rv;
5129         }
5130
5131         TRACEME(("regular do_retrieve() end"));
5132
5133         return newRV_noinc(sv);
5134 }
5135
5136 /*
5137  * pretrieve
5138  *
5139  * Retrieve data held in file and return the root object, undef on error.
5140  */
5141 SV *pretrieve(PerlIO *f)
5142 {
5143         TRACEME(("pretrieve"));
5144         return do_retrieve(f, Nullsv, 0);
5145 }
5146
5147 /*
5148  * mretrieve
5149  *
5150  * Retrieve data held in scalar and return the root object, undef on error.
5151  */
5152 SV *mretrieve(SV *sv)
5153 {
5154         TRACEME(("mretrieve"));
5155         return do_retrieve((PerlIO*) 0, sv, 0);
5156 }
5157
5158 /***
5159  *** Deep cloning
5160  ***/
5161
5162 /*
5163  * dclone
5164  *
5165  * Deep clone: returns a fresh copy of the original referenced SV tree.
5166  *
5167  * This is achieved by storing the object in memory and restoring from
5168  * there. Not that efficient, but it should be faster than doing it from
5169  * pure perl anyway.
5170  */
5171 SV *dclone(SV *sv)
5172 {
5173         dSTCXT;
5174         int size;
5175         stcxt_t *real_context;
5176         SV *out;
5177
5178         TRACEME(("dclone"));
5179
5180         /*
5181          * Workaround for CROAK leak: if they enter with a "dirty" context,
5182          * free up memory for them now.
5183          */
5184
5185         if (cxt->s_dirty)
5186                 clean_context(cxt);
5187
5188         /*
5189          * do_store() optimizes for dclone by not freeing its context, should
5190          * we need to allocate one because we're deep cloning from a hook.
5191          */
5192
5193         if (!do_store((PerlIO*) 0, sv, ST_CLONE, FALSE, (SV**) 0))
5194                 return &PL_sv_undef;                            /* Error during store */
5195
5196         /*
5197          * Because of the above optimization, we have to refresh the context,
5198          * since a new one could have been allocated and stacked by do_store().
5199          */
5200
5201         { dSTCXT; real_context = cxt; }         /* Sub-block needed for macro */
5202         cxt = real_context;                                     /* And we need this temporary... */
5203
5204         /*
5205          * Now, `cxt' may refer to a new context.
5206          */
5207
5208         ASSERT(!cxt->s_dirty, ("clean context"));
5209         ASSERT(!cxt->entry, ("entry will not cause new context allocation"));
5210
5211         size = MBUF_SIZE();
5212         TRACEME(("dclone stored %d bytes", size));
5213         MBUF_INIT(size);
5214
5215         /*
5216          * Since we're passing do_retrieve() both a NULL file and sv, we need
5217          * to pre-compute the taintedness of the input by setting cxt->tainted
5218          * to whatever state our own input string was.  -- RAM, 15/09/2000
5219          *
5220          * do_retrieve() will free non-root context.
5221          */
5222
5223         cxt->s_tainted = SvTAINTED(sv);
5224         out = do_retrieve((PerlIO*) 0, Nullsv, ST_CLONE);
5225
5226         TRACEME(("dclone returns 0x%"UVxf, PTR2UV(out)));
5227
5228         return out;
5229 }
5230
5231 /***
5232  *** Glue with perl.
5233  ***/
5234
5235 /*
5236  * The Perl IO GV object distinguishes between input and output for sockets
5237  * but not for plain files. To allow Storable to transparently work on
5238  * plain files and sockets transparently, we have to ask xsubpp to fetch the
5239  * right object for us. Hence the OutputStream and InputStream declarations.
5240  *
5241  * Before perl 5.004_05, those entries in the standard typemap are not
5242  * defined in perl include files, so we do that here.
5243  */
5244
5245 #ifndef OutputStream
5246 #define OutputStream    PerlIO *
5247 #define InputStream             PerlIO *
5248 #endif  /* !OutputStream */
5249
5250 MODULE = Storable       PACKAGE = Storable
5251
5252 PROTOTYPES: ENABLE
5253
5254 BOOT:
5255     init_perinterp();
5256
5257 int
5258 pstore(f,obj)
5259 OutputStream    f
5260 SV *    obj
5261
5262 int
5263 net_pstore(f,obj)
5264 OutputStream    f
5265 SV *    obj
5266
5267 SV *
5268 mstore(obj)
5269 SV *    obj
5270
5271 SV *
5272 net_mstore(obj)
5273 SV *    obj
5274
5275 SV *
5276 pretrieve(f)
5277 InputStream     f
5278
5279 SV *
5280 mretrieve(sv)
5281 SV *    sv
5282
5283 SV *
5284 dclone(sv)
5285 SV *    sv
5286
5287 int
5288 last_op_in_netorder()
5289
5290 int
5291 is_storing()
5292
5293 int
5294 is_retrieving()
5295