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