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