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