This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[patch] rid local_patches warnings
[perl5.git] / ext / Storable / Storable.xs
CommitLineData
7a6a85bf
RG
1/*
2 * Store and retrieve mechanism.
3 */
4
5/*
8be2b38b 6 * $Id: Storable.xs,v 1.0.1.8 2001/03/15 00:20:55 ram Exp $
7a6a85bf
RG
7 *
8 * Copyright (c) 1995-2000, Raphael Manfredi
9 *
9e21b3d0
JH
10 * You may redistribute only under the same terms as Perl 5, as specified
11 * in the README file that comes with the distribution.
7a6a85bf
RG
12 *
13 * $Log: Storable.xs,v $
8be2b38b
JH
14 * Revision 1.0.1.8 2001/03/15 00:20:55 ram
15 * patch11: last version was wrongly compiling with assertions on
16 *
b12202d0
JH
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 *
862382c7
JH
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 *
90826881
JH
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 *
212e9bde
JH
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 *
8be2b38b
JH
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 *
9e21b3d0
JH
43 * Revision 1.0 2000/09/01 19:40:41 ram
44 * Baseline for first official release.
7a6a85bf
RG
45 *
46 */
47
48#include <EXTERN.h>
49#include <perl.h>
7a6a85bf
RG
50#include <XSUB.h>
51
9e21b3d0
JH
52#if 0
53#define DEBUGME /* Debug mode, turns assertions on as well */
54#define DASSERT /* Assertion mode */
55#endif
7a6a85bf
RG
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 */
f0ffaed8
JH
75
76#ifndef PERL_VERSION /* For perls < 5.6 */
92731555 77#include <patchlevel.h>
f0ffaed8 78#define PERL_VERSION PATCHLEVEL
7a6a85bf
RG
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
dd19458b
JH
86#if (SUBVERSION <= 4) /* 5.004_04 has been reported to lack newSVpvn */
87#define newSVpvn newSVpv
7a6a85bf 88#endif
dd19458b 89#endif /* PATCHLEVEL <= 4 */
7a6a85bf
RG
90#ifndef HvSHAREKEYS_off
91#define HvSHAREKEYS_off(hv) /* Ignore */
92#endif
f0ffaed8
JH
93#ifndef AvFILLp /* Older perls (<=5.003) lack AvFILLp */
94#define AvFILLp AvFILL
95#endif
96typedef double NV; /* Older perls lack the NV type */
cc964657
JH
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)
f0ffaed8 103#endif /* PERL_VERSION -- perls < 5.6 */
7a6a85bf 104
cc964657 105#ifndef NVef /* The following were not part of perl 5.6 */
9e21b3d0
JH
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
cc964657
JH
112#define NVef "e"
113#define NVff "f"
114#define NVgf "g"
115#endif
116#endif
117
7a6a85bf 118#ifdef DEBUGME
8be2b38b
JH
119
120#ifndef DASSERT
121#define DASSERT
122#endif
123
90826881
JH
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)
7a6a85bf
RG
132#else
133#define TRACEME(x)
8be2b38b 134#endif /* DEBUGME */
7a6a85bf
RG
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 */
dd19458b 155#define SX_LSCALAR C(1) /* Scalar (large binary) follows (length, data) */
7a6a85bf
RG
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 */
dd19458b 164#define SX_SCALAR C(10) /* Scalar (binary, small) follows (length, data) */
7a6a85bf
RG
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 */
dd19458b
JH
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 */
7a6a85bf
RG
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 */
224struct 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
247typedef 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
dd19458b
JH
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
7a6a85bf
RG
272typedef struct stcxt {
273 int entry; /* flags recursion */
274 int optype; /* type of traversal operation */
275 HV *hseen; /* which objects have been seen, store time */
90826881 276 AV *hook_seen; /* which SVs were returned by STORABLE_freeze() */
7a6a85bf
RG
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 */
425d70b4
JH
281 IV tagnum; /* incremented at store time for each seen object */
282 IV classnum; /* incremented at store time for each seen classname */
7a6a85bf 283 int netorder; /* true if network order used */
dd19458b 284 int s_tainted; /* true if input source is tainted, at retrieve time */
7a6a85bf
RG
285 int forgive_me; /* whether to be forgiving... */
286 int canonical; /* whether to store hashes sorted by key */
dd19458b 287 int s_dirty; /* context is dirty due to CROAK() -- can be cleaned */
7a6a85bf
RG
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) \
9e21b3d0
JH
309 T name = (perinterp_sv && SvIOK(perinterp_sv) \
310 ? INT2PTR(T, SvIVX(perinterp_sv)) : (T) 0)
7a6a85bf
RG
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); \
43d061fe 318 sv_setiv(perinterp_sv, PTR2IV(cxt))
7a6a85bf
RG
319
320#define SET_STCXT(x) do { \
321 dSTCXT_SV; \
43d061fe 322 sv_setiv(perinterp_sv, PTR2IV(x)); \
7a6a85bf
RG
323} while (0)
324
325#else /* !MULTIPLICITY && !PERL_OBJECT && !PERL_CAPI */
326
327static stcxt_t Context;
328static 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
dd19458b 352#define CROAK(x) do { cxt->s_dirty = 1; croak x; } while (0)
7a6a85bf
RG
353
354/*
355 * End of "thread-safe" related definitions.
356 */
357
358/*
9e21b3d0
JH
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/*
7a6a85bf
RG
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
9e21b3d0
JH
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
7a6a85bf
RG
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)
9e21b3d0 496#endif
7a6a85bf
RG
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
9e21b3d0
JH
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
7a6a85bf
RG
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)
9e21b3d0 540#endif
7a6a85bf
RG
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/*
7a6a85bf
RG
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/*
b12202d0 573 * Types for SX_HOOK (last 2 bits in flags).
7a6a85bf
RG
574 */
575
576#define SHT_SCALAR 0
577#define SHT_ARRAY 1
578#define SHT_HASH 2
b12202d0
JH
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 */
7a6a85bf
RG
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 */
605static char old_magicstr[] = "perl-store"; /* Magic number before 0.6 */
606static char magicstr[] = "pst0"; /* Used as a magic number */
607
608#define STORABLE_BIN_MAJOR 2 /* Binary major "version" */
b12202d0 609#define STORABLE_BIN_MINOR 4 /* Binary minor "version" */
7a6a85bf
RG
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
9e21b3d0
JH
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
7a6a85bf
RG
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); \
9e21b3d0 636 else if (PerlIO_write(cxt->fio,oI(&y),oS(sizeof(y))) != oS(sizeof(y))) \
7a6a85bf
RG
637 return -1; \
638 } else { \
639 if (!cxt->fio) \
640 MBUF_PUTINT(x); \
9e21b3d0 641 else if (PerlIO_write(cxt->fio,oI(&x),oS(sizeof(x))) != oS(sizeof(x))) \
7a6a85bf
RG
642 return -1; \
643 } \
644} while (0)
645#else
9e21b3d0 646#define WLEN(x) WRITE_I32(x)
7a6a85bf
RG
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
dd19458b 656#define STORE_PV_LEN(pv, len, small, large) do { \
7a6a85bf
RG
657 if (len <= LG_SCALAR) { \
658 unsigned char clen = (unsigned char) len; \
dd19458b 659 PUTMARK(small); \
7a6a85bf
RG
660 PUTMARK(clen); \
661 if (len) \
662 WRITE(pv, len); \
663 } else { \
dd19458b 664 PUTMARK(large); \
7a6a85bf
RG
665 WLEN(len); \
666 WRITE(pv, len); \
667 } \
668} while (0)
669
dd19458b
JH
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
7a6a85bf
RG
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); \
76df4757 703 else if ((int) (x = PerlIO_getc(cxt->fio)) == EOF) \
7a6a85bf
RG
704 return (SV *) 0; \
705} while (0)
706
9e21b3d0
JH
707#define READ_I32(x) do { \
708 ASSERT(sizeof(x) == sizeof(I32), ("reading an I32")); \
709 oC(x); \
7a6a85bf
RG
710 if (!cxt->fio) \
711 MBUF_GETINT(x); \
9e21b3d0 712 else if (PerlIO_read(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
7a6a85bf 713 return (SV *) 0; \
7a6a85bf 714} while (0)
9e21b3d0
JH
715
716#ifdef HAS_NTOHL
7a6a85bf 717#define RLEN(x) do { \
9e21b3d0 718 oC(x); \
7a6a85bf
RG
719 if (!cxt->fio) \
720 MBUF_GETINT(x); \
9e21b3d0 721 else if (PerlIO_read(cxt->fio, oI(&x), oS(sizeof(x))) != oS(sizeof(x))) \
7a6a85bf 722 return (SV *) 0; \
9e21b3d0
JH
723 if (cxt->netorder) \
724 x = (int) ntohl(x); \
7a6a85bf 725} while (0)
9e21b3d0
JH
726#else
727#define RLEN(x) READ_I32(x)
7a6a85bf
RG
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.
b12202d0
JH
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.
7a6a85bf 763 */
b12202d0 764#define SEEN(y,c) do { \
7a6a85bf
RG
765 if (!y) \
766 return (SV *) 0; \
767 if (av_store(cxt->aseen, cxt->tagnum++, SvREFCNT_inc(y)) == 0) \
768 return (SV *) 0; \
43d061fe 769 TRACEME(("aseen(#%d) = 0x%"UVxf" (refcnt=%d)", cxt->tagnum-1, \
b12202d0
JH
770 PTR2UV(y), SvREFCNT(y)-1)); \
771 if (c) \
772 BLESS((SV *) (y), c); \
7a6a85bf
RG
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; \
43d061fe 781 TRACEME(("blessing 0x%"UVxf" in %s", PTR2UV(s), (p))); \
7a6a85bf
RG
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
789static int store();
b12202d0 790static SV *retrieve(stcxt_t *cxt, char *cname);
7a6a85bf
RG
791
792/*
793 * Dynamic dispatching table for SV store.
794 */
795
796static int store_ref(stcxt_t *cxt, SV *sv);
797static int store_scalar(stcxt_t *cxt, SV *sv);
798static int store_array(stcxt_t *cxt, AV *av);
799static int store_hash(stcxt_t *cxt, HV *hv);
800static int store_tied(stcxt_t *cxt, SV *sv);
801static int store_tied_item(stcxt_t *cxt, SV *sv);
802static int store_other(stcxt_t *cxt, SV *sv);
f0ffaed8 803static int store_blessed(stcxt_t *cxt, SV *sv, int type, HV *pkg);
7a6a85bf 804
862382c7
JH
805static 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 */
7a6a85bf
RG
813};
814
815#define SV_STORE(x) (*sv_store[x])
816
817/*
818 * Dynamic dispatching tables for SV retrieval.
819 */
820
b12202d0
JH
821static SV *retrieve_lscalar(stcxt_t *cxt, char *cname);
822static SV *retrieve_lutf8str(stcxt_t *cxt, char *cname);
823static SV *old_retrieve_array(stcxt_t *cxt, char *cname);
824static SV *old_retrieve_hash(stcxt_t *cxt, char *cname);
825static SV *retrieve_ref(stcxt_t *cxt, char *cname);
826static SV *retrieve_undef(stcxt_t *cxt, char *cname);
827static SV *retrieve_integer(stcxt_t *cxt, char *cname);
828static SV *retrieve_double(stcxt_t *cxt, char *cname);
829static SV *retrieve_byte(stcxt_t *cxt, char *cname);
830static SV *retrieve_netint(stcxt_t *cxt, char *cname);
831static SV *retrieve_scalar(stcxt_t *cxt, char *cname);
832static SV *retrieve_utf8str(stcxt_t *cxt, char *cname);
833static SV *retrieve_tied_array(stcxt_t *cxt, char *cname);
834static SV *retrieve_tied_hash(stcxt_t *cxt, char *cname);
835static SV *retrieve_tied_scalar(stcxt_t *cxt, char *cname);
836static SV *retrieve_other(stcxt_t *cxt, char *cname);
837
838static SV *(*sv_old_retrieve[])(stcxt_t *cxt, char *cname) = {
7a6a85bf
RG
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 */
dd19458b
JH
862 retrieve_other, /* SX_UTF8STR not supported */
863 retrieve_other, /* SX_LUTF8STR not supported */
7a6a85bf
RG
864 retrieve_other, /* SX_ERROR */
865};
866
b12202d0
JH
867static SV *retrieve_array(stcxt_t *cxt, char *cname);
868static SV *retrieve_hash(stcxt_t *cxt, char *cname);
869static SV *retrieve_sv_undef(stcxt_t *cxt, char *cname);
870static SV *retrieve_sv_yes(stcxt_t *cxt, char *cname);
871static SV *retrieve_sv_no(stcxt_t *cxt, char *cname);
872static SV *retrieve_blessed(stcxt_t *cxt, char *cname);
873static SV *retrieve_idx_blessed(stcxt_t *cxt, char *cname);
874static SV *retrieve_hook(stcxt_t *cxt, char *cname);
875static SV *retrieve_overloaded(stcxt_t *cxt, char *cname);
876static SV *retrieve_tied_key(stcxt_t *cxt, char *cname);
877static SV *retrieve_tied_idx(stcxt_t *cxt, char *cname);
878
879static SV *(*sv_retrieve[])(stcxt_t *cxt, char *cname) = {
7a6a85bf
RG
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 */
dd19458b
JH
903 retrieve_utf8str, /* SX_UTF8STR */
904 retrieve_lutf8str, /* SX_LUTF8STR */
7a6a85bf
RG
905 retrieve_other, /* SX_ERROR */
906};
907
908#define RETRIEVE(c,x) (*(c)->retrieve_vtbl[(x) >= SX_ERROR ? SX_ERROR : (x)])
909
f0ffaed8 910static SV *mbuf2sv(void);
7a6a85bf
RG
911
912/***
913 *** Context management.
914 ***/
915
916/*
917 * init_perinterp
918 *
919 * Called once per "thread" (interpreter) to initialize some global context.
920 */
f0ffaed8
JH
921static void init_perinterp(void)
922{
7a6a85bf
RG
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 */
f0ffaed8
JH
934static void init_store_context(
935 stcxt_t *cxt,
936 PerlIO *f,
937 int optype,
938 int network_order)
7a6a85bf
RG
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 */
f0ffaed8 982#if PERL_VERSION >= 5
7a6a85bf
RG
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
f0ffaed8 997#if PERL_VERSION >= 5
7a6a85bf
RG
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 */
90826881
JH
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 */
7a6a85bf
RG
1020}
1021
1022/*
1023 * clean_store_context
1024 *
1025 * Clean store context by
1026 */
f0ffaed8 1027static void clean_store_context(stcxt_t *cxt)
7a6a85bf
RG
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);
13689cfe 1040 while ((he = hv_iternext(cxt->hseen)))
7a6a85bf
RG
1041 HeVAL(he) = &PL_sv_undef;
1042
1043 hv_iterinit(cxt->hclass);
13689cfe 1044 while ((he = hv_iternext(cxt->hclass)))
7a6a85bf
RG
1045 HeVAL(he) = &PL_sv_undef;
1046
1047 /*
1048 * And now dispose of them...
862382c7
JH
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 }
7a6a85bf 1063
862382c7
JH
1064 if (cxt->hclass) {
1065 HV *hclass = cxt->hclass;
1066 cxt->hclass = 0;
1067 hv_undef(hclass);
1068 sv_free((SV *) hclass);
1069 }
7a6a85bf 1070
862382c7
JH
1071 if (cxt->hook) {
1072 HV *hook = cxt->hook;
1073 cxt->hook = 0;
1074 hv_undef(hook);
1075 sv_free((SV *) hook);
1076 }
7a6a85bf 1077
862382c7
JH
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 }
90826881 1084
7a6a85bf 1085 cxt->entry = 0;
dd19458b 1086 cxt->s_dirty = 0;
7a6a85bf
RG
1087}
1088
1089/*
1090 * init_retrieve_context
1091 *
1092 * Initialize a new retrieve context for real recursion.
1093 */
dd19458b 1094static void init_retrieve_context(stcxt_t *cxt, int optype, int is_tainted)
7a6a85bf
RG
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;
dd19458b 1123 cxt->s_tainted = is_tainted;
7a6a85bf
RG
1124 cxt->entry = 1; /* No recursion yet */
1125}
1126
1127/*
1128 * clean_retrieve_context
1129 *
1130 * Clean retrieve context by
1131 */
dd19458b 1132static void clean_retrieve_context(stcxt_t *cxt)
7a6a85bf
RG
1133{
1134 TRACEME(("clean_retrieve_context"));
1135
1136 ASSERT(cxt->optype & ST_RETRIEVE, ("was performing a retrieve()"));
1137
862382c7
JH
1138 if (cxt->aseen) {
1139 AV *aseen = cxt->aseen;
1140 cxt->aseen = 0;
1141 av_undef(aseen);
1142 sv_free((SV *) aseen);
1143 }
7a6a85bf 1144
862382c7
JH
1145 if (cxt->aclass) {
1146 AV *aclass = cxt->aclass;
1147 cxt->aclass = 0;
1148 av_undef(aclass);
1149 sv_free((SV *) aclass);
1150 }
7a6a85bf 1151
862382c7
JH
1152 if (cxt->hook) {
1153 HV *hook = cxt->hook;
1154 cxt->hook = 0;
1155 hv_undef(hook);
1156 sv_free((SV *) hook);
1157 }
7a6a85bf 1158
862382c7
JH
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 }
7a6a85bf
RG
1165
1166 cxt->entry = 0;
dd19458b 1167 cxt->s_dirty = 0;
7a6a85bf
RG
1168}
1169
1170/*
1171 * clean_context
1172 *
1173 * A workaround for the CROAK bug: cleanup the last context.
1174 */
1175static void clean_context(cxt)
1176stcxt_t *cxt;
1177{
1178 TRACEME(("clean_context"));
1179
dd19458b 1180 ASSERT(cxt->s_dirty, ("dirty context"));
7a6a85bf
RG
1181
1182 if (cxt->optype & ST_RETRIEVE)
1183 clean_retrieve_context(cxt);
1184 else
1185 clean_store_context(cxt);
862382c7
JH
1186
1187 ASSERT(!cxt->s_dirty, ("context is clean"));
7a6a85bf
RG
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 */
1196static stcxt_t *allocate_context(parent_cxt)
1197stcxt_t *parent_cxt;
1198{
1199 stcxt_t *cxt;
1200
1201 TRACEME(("allocate_context"));
1202
dd19458b 1203 ASSERT(!parent_cxt->s_dirty, ("parent context clean"));
7a6a85bf
RG
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 */
1218static void free_context(cxt)
1219stcxt_t *cxt;
1220{
1221 stcxt_t *prev = cxt->prev;
1222
1223 TRACEME(("free_context"));
1224
dd19458b 1225 ASSERT(!cxt->s_dirty, ("clean context"));
7a6a85bf
RG
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 */
f0ffaed8 1246int is_storing(void)
7a6a85bf
RG
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 */
f0ffaed8 1258int is_retrieving(void)
7a6a85bf
RG
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 */
f0ffaed8 1273int last_op_in_netorder(void)
7a6a85bf
RG
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 */
f0ffaed8
JH
1292static SV *pkg_fetchmeth(
1293 HV *cache,
1294 HV *pkg,
1295 char *method)
7a6a85bf
RG
1296{
1297 GV *gv;
1298 SV *sv;
7a6a85bf
RG
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));
9e21b3d0 1308 TRACEME(("%s->%s: 0x%"UVxf, HvNAME(pkg), method, PTR2UV(sv)));
7a6a85bf
RG
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 */
f0ffaed8
JH
1329static void pkg_hide(
1330 HV *cache,
1331 HV *pkg,
1332 char *method)
7a6a85bf
RG
1333{
1334 (void) hv_store(cache,
1335 HvNAME(pkg), strlen(HvNAME(pkg)), newSVsv(&PL_sv_undef), 0);
1336}
1337
1338/*
212e9bde
JH
1339 * pkg_uncache
1340 *
1341 * Discard cached value: a whole fetch loop will be retried at next lookup.
1342 */
1343static 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/*
7a6a85bf
RG
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 */
f0ffaed8
JH
1359static SV *pkg_can(
1360 HV *cache,
1361 HV *pkg,
1362 char *method)
7a6a85bf
RG
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 {
43d061fe 1384 TRACEME(("cached %s->%s: 0x%"UVxf,
9e21b3d0 1385 HvNAME(pkg), method, PTR2UV(sv)));
7a6a85bf
RG
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 */
f0ffaed8
JH
1400static SV *scalar_call(
1401 SV *obj,
1402 SV *hook,
1403 int cloning,
1404 AV *av,
1405 I32 flags)
7a6a85bf
RG
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++) {
43d061fe
JH
1425 TRACEME(("pushing arg #%d (0x%"UVxf")...",
1426 i, PTR2UV(ary[i])));
7a6a85bf
RG
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 *
f9a1036d 1453 * Call routine obj->hook(cloning) in list context.
7a6a85bf
RG
1454 * Returns the list of returned values in an array.
1455 */
f0ffaed8
JH
1456static AV *array_call(
1457 SV *obj,
1458 SV *hook,
1459 int cloning)
7a6a85bf
RG
1460{
1461 dSP;
1462 int count;
1463 AV *av;
1464 int i;
1465
f0ffaed8 1466 TRACEME(("array_call (cloning=%d)", cloning));
7a6a85bf
RG
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 */
f0ffaed8
JH
1501static int known_class(
1502 stcxt_t *cxt,
1503 char *name, /* Class name */
1504 int len, /* Name length */
1505 I32 *classnum)
7a6a85bf
RG
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.
7a6a85bf
RG
1525 */
1526
1527 cxt->classnum++;
3341c981 1528 if (!hv_store(hclass, name, len, INT2PTR(SV*, cxt->classnum), 0))
7a6a85bf
RG
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 */
f0ffaed8 1545static int store_ref(stcxt_t *cxt, SV *sv)
7a6a85bf 1546{
43d061fe 1547 TRACEME(("store_ref (0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
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)) {
9e21b3d0 1558 TRACEME(("ref (0x%"UVxf") is overloaded", PTR2UV(sv)));
7a6a85bf
RG
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 */
f0ffaed8 1579static int store_scalar(stcxt_t *cxt, SV *sv)
7a6a85bf
RG
1580{
1581 IV iv;
1582 char *pv;
1583 STRLEN len;
1584 U32 flags = SvFLAGS(sv); /* "cc -O" may put it in register */
1585
43d061fe 1586 TRACEME(("store_scalar (0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
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 {
86bbd6dc 1599 TRACEME(("undef at 0x%"UVxf, PTR2UV(sv)));
7a6a85bf
RG
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 */
cc964657 1648 I32 wlen; /* For 64-bit machines */
7a6a85bf
RG
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
9e21b3d0 1658 wlen = (I32) len; /* WLEN via STORE_SCALAR expects I32 */
dd19458b
JH
1659 if (SvUTF8 (sv))
1660 STORE_UTF8STR(pv, wlen);
1661 else
1662 STORE_SCALAR(pv, wlen);
d67b2c17 1663 TRACEME(("ok (scalar 0x%"UVxf" '%s', length = %"IVdf")",
5f56cddd 1664 PTR2UV(sv), SvPVX(sv), (IV)len));
7a6a85bf
RG
1665
1666 } else if (flags & SVp_NOK) { /* SvNOKp(sv) => double */
f27e1f0a 1667 NV nv = SvNV(sv);
7a6a85bf
RG
1668
1669 /*
1670 * Watch for number being an integer in disguise.
1671 */
f27e1f0a 1672 if (nv == (NV) (iv = I_V(nv))) {
9e21b3d0 1673 TRACEME(("double %"NVff" is actually integer %"IVdf, nv, iv));
7a6a85bf
RG
1674 goto integer; /* Share code below */
1675 }
1676
1677 if (cxt->netorder) {
43d061fe 1678 TRACEME(("double %"NVff" stored as string", nv));
7a6a85bf
RG
1679 pv = SvPV(sv, len);
1680 goto string; /* Share code above */
1681 }
1682
1683 PUTMARK(SX_DOUBLE);
1684 WRITE(&nv, sizeof(nv));
1685
9e21b3d0 1686 TRACEME(("ok (double 0x%"UVxf", value = %"NVff")", PTR2UV(sv), nv));
7a6a85bf
RG
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) {
9e21b3d0 1707 I32 niv;
7a6a85bf 1708#ifdef HAS_HTONL
9e21b3d0 1709 niv = (I32) htonl(iv);
7a6a85bf
RG
1710 TRACEME(("using network order"));
1711#else
9e21b3d0 1712 niv = (I32) iv;
7a6a85bf
RG
1713 TRACEME(("as-is for network order"));
1714#endif
1715 PUTMARK(SX_NETINT);
9e21b3d0 1716 WRITE_I32(niv);
7a6a85bf
RG
1717 } else {
1718 PUTMARK(SX_INTEGER);
1719 WRITE(&iv, sizeof(iv));
1720 }
1721
9e21b3d0 1722 TRACEME(("ok (integer 0x%"UVxf", value = %"IVdf")", PTR2UV(sv), iv));
7a6a85bf
RG
1723
1724 } else
43d061fe
JH
1725 CROAK(("Can't determine type of %s(0x%"UVxf")",
1726 sv_reftype(sv, FALSE),
1727 PTR2UV(sv)));
7a6a85bf
RG
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 */
f0ffaed8 1740static int store_array(stcxt_t *cxt, AV *av)
7a6a85bf
RG
1741{
1742 SV **sav;
1743 I32 len = av_len(av) + 1;
1744 I32 i;
1745 int ret;
1746
43d061fe 1747 TRACEME(("store_array (0x%"UVxf")", PTR2UV(av)));
7a6a85bf
RG
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));
13689cfe 1769 if ((ret = store(cxt, *sav)))
7a6a85bf
RG
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 */
1784static int
f0ffaed8 1785sortcmp(const void *a, const void *b)
7a6a85bf
RG
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 */
f0ffaed8 1801static int store_hash(stcxt_t *cxt, HV *hv)
7a6a85bf
RG
1802{
1803 I32 len = HvKEYS(hv);
1804 I32 i;
1805 int ret = 0;
1806 I32 riter;
1807 HE *eiter;
1808
43d061fe 1809 TRACEME(("store_hash (0x%"UVxf")", PTR2UV(hv)));
7a6a85bf
RG
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
9e21b3d0 1875 TRACEME(("(#%d) value 0x%"UVxf, i, PTR2UV(val)));
7a6a85bf 1876
13689cfe 1877 if ((ret = store(cxt, val)))
7a6a85bf
RG
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
9e21b3d0 1921 TRACEME(("(#%d) value 0x%"UVxf, i, PTR2UV(val)));
7a6a85bf 1922
13689cfe 1923 if ((ret = store(cxt, val)))
7a6a85bf
RG
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
43d061fe 1941 TRACEME(("ok (hash 0x%"UVxf")", PTR2UV(hv)));
7a6a85bf
RG
1942
1943out:
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 */
f0ffaed8 1958static int store_tied(stcxt_t *cxt, SV *sv)
7a6a85bf
RG
1959{
1960 MAGIC *mg;
1961 int ret = 0;
1962 int svt = SvTYPE(sv);
1963 char mtype = 'P';
1964
43d061fe 1965 TRACEME(("store_tied (0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
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
13689cfe 2006 if ((ret = store(cxt, mg->mg_obj)))
7a6a85bf
RG
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 */
f0ffaed8 2026static int store_tied_item(stcxt_t *cxt, SV *sv)
7a6a85bf
RG
2027{
2028 MAGIC *mg;
2029 int ret;
2030
43d061fe 2031 TRACEME(("store_tied_item (0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
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);
9e21b3d0 2043 TRACEME(("store_tied_item: storing OBJ 0x%"UVxf, PTR2UV(mg->mg_obj)));
7a6a85bf 2044
13689cfe 2045 if ((ret = store(cxt, mg->mg_obj)))
7a6a85bf
RG
2046 return ret;
2047
9e21b3d0 2048 TRACEME(("store_tied_item: storing PTR 0x%"UVxf, PTR2UV(mg->mg_ptr)));
7a6a85bf 2049
13689cfe 2050 if ((ret = store(cxt, (SV *) mg->mg_ptr)))
7a6a85bf
RG
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);
9e21b3d0 2057 TRACEME(("store_tied_item: storing OBJ 0x%"UVxf, PTR2UV(mg->mg_obj)));
7a6a85bf 2058
13689cfe 2059 if ((ret = store(cxt, mg->mg_obj)))
7a6a85bf
RG
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>.
b12202d0
JH
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.
7a6a85bf 2116 */
f0ffaed8
JH
2117static int store_hook(
2118 stcxt_t *cxt,
2119 SV *sv,
2120 int type,
2121 HV *pkg,
2122 SV *hook)
7a6a85bf
RG
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;
8fa7f367
JH
2139 char mtype = 0; /* for blessed ref to tied structures */
2140 unsigned char eflags = 0; /* used when object type is SHT_EXTRA */
7a6a85bf
RG
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;
b12202d0
JH
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;
7a6a85bf
RG
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"));
cc964657 2240 TRACEME(("ignoring STORABLE_freeze in class \"%s\"", class));
7a6a85bf
RG
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 /*
7a6a85bf
RG
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;
90826881
JH
2267 SV *rsv = ary[i];
2268 SV *xsv;
2269 AV *av_hook = cxt->hook_seen;
7a6a85bf 2270
90826881
JH
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 */
7a6a85bf
RG
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
13689cfe 2281 if ((svh = hv_fetch(cxt->hseen, (char *) &xsv, sizeof(xsv), FALSE)))
7a6a85bf
RG
2282 goto sv_seen; /* Avoid moving code too far to the right */
2283
9e21b3d0 2284 TRACEME(("listed object %d at 0x%"UVxf" is unknown", i-1, PTR2UV(xsv)));
7a6a85bf
RG
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
b12202d0
JH
2297 /* [SX_HOOK] <flags> [<extra>] <object>*/
2298 if (!recursed++) {
7a6a85bf 2299 PUTMARK(SX_HOOK);
b12202d0
JH
2300 PUTMARK(flags);
2301 if (obj_type == SHT_EXTRA)
2302 PUTMARK(eflags);
2303 } else
2304 PUTMARK(flags);
7a6a85bf 2305
13689cfe 2306 if ((ret = store(cxt, xsv))) /* Given by hook for us to store */
7a6a85bf
RG
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 /*
90826881
JH
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.
7a6a85bf
RG
2324 */
2325
90826881
JH
2326 av_store(av_hook, AvFILLp(av_hook)+1, SvREFCNT_inc(xsv));
2327
7a6a85bf 2328 sv_seen:
90826881
JH
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
7a6a85bf 2342 ary[i] = *svh;
76edffbb 2343 TRACEME(("listed object %d at 0x%"UVxf" is tag #%"UVuf,
d67b2c17 2344 i-1, PTR2UV(xsv), PTR2UV(*svh)));
7a6a85bf
RG
2345 }
2346
2347 /*
dd19458b
JH
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 /*
7a6a85bf
RG
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
9e21b3d0
JH
2388 TRACEME(("SX_HOOK (recursed=%d) flags=0x%x "
2389 "class=%"IVdf" len=%"IVdf" len2=%"IVdf" len3=%d",
d67b2c17 2390 recursed, flags, (IV)classnum, (IV)len, (IV)len2, count-1));
7a6a85bf 2391
b12202d0
JH
2392 /* SX_HOOK <flags> [<extra>] */
2393 if (!recursed) {
7a6a85bf 2394 PUTMARK(SX_HOOK);
b12202d0
JH
2395 PUTMARK(flags);
2396 if (obj_type == SHT_EXTRA)
2397 PUTMARK(eflags);
2398 } else
2399 PUTMARK(flags);
7a6a85bf
RG
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> */
cc964657
JH
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 {
7a6a85bf
RG
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]));
9e21b3d0 2447 WRITE_I32(tagval);
7a6a85bf
RG
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
b12202d0
JH
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
13689cfe 2483 if ((ret = store(cxt, mg->mg_obj)))
b12202d0
JH
2484 return ret;
2485 }
2486
7a6a85bf
RG
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 */
f0ffaed8
JH
2514static int store_blessed(
2515 stcxt_t *cxt,
2516 SV *sv,
2517 int type,
2518 HV *pkg)
7a6a85bf
RG
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
43d061fe
JH
2543 TRACEME(("blessed 0x%"UVxf" in %s, no hook: tagged #%d",
2544 PTR2UV(sv), class, cxt->tagnum));
7a6a85bf
RG
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 */
f0ffaed8 2595static int store_other(stcxt_t *cxt, SV *sv)
7a6a85bf 2596{
cc964657 2597 I32 len;
7a6a85bf
RG
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
43d061fe
JH
2613 warn("Can't store item %s(0x%"UVxf")",
2614 sv_reftype(sv, FALSE), PTR2UV(sv));
7a6a85bf
RG
2615
2616 /*
2617 * Store placeholder string as a scalar instead...
2618 */
2619
13689cfe
AMS
2620 (void) sprintf(buf, "You lost %s(0x%"UVxf")%c", sv_reftype(sv, FALSE),
2621 PTR2UV(sv), (char)0);
7a6a85bf
RG
2622
2623 len = strlen(buf);
2624 STORE_SCALAR(buf, len);
86bbd6dc 2625 TRACEME(("ok (dummy \"%s\", length = %"IVdf")", buf, len));
7a6a85bf
RG
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 */
f0ffaed8 2642static int sv_type(SV *sv)
7a6a85bf
RG
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 */
f0ffaed8 2700static int store(stcxt_t *cxt, SV *sv)
7a6a85bf
RG
2701{
2702 SV **svh;
2703 int ret;
7a6a85bf 2704 int type;
43d061fe 2705 HV *hseen = cxt->hseen;
7a6a85bf 2706
43d061fe 2707 TRACEME(("store (0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
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
9e21b3d0 2725 TRACEME(("object 0x%"UVxf" seen as #%d", PTR2UV(sv), ntohl(tagval)));
7a6a85bf
RG
2726
2727 PUTMARK(SX_OBJECT);
9e21b3d0 2728 WRITE_I32(tagval);
7a6a85bf
RG
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 *
7a6a85bf
RG
2741 */
2742
2743 cxt->tagnum++;
2744 if (!hv_store(hseen,
3341c981 2745 (char *) &sv, sizeof(sv), INT2PTR(SV*, cxt->tagnum), 0))
7a6a85bf
RG
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
43d061fe
JH
2755 TRACEME(("storing 0x%"UVxf" tag #%d, type %d...",
2756 PTR2UV(sv), cxt->tagnum, type));
7a6a85bf
RG
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
43d061fe
JH
2764 TRACEME(("%s (stored 0x%"UVxf", refcnt=%d, %s)",
2765 ret ? "FAILED" : "ok", PTR2UV(sv),
7a6a85bf
RG
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 */
f0ffaed8 2782static int magic_write(stcxt_t *cxt)
7a6a85bf
RG
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 *));
9e21b3d0 2821 PUTMARK((unsigned char) sizeof(NV));
7a6a85bf 2822
9e21b3d0 2823 TRACEME(("ok (magic_write byteorder = 0x%lx [%d], I%d L%d P%d D%d)",
43d061fe 2824 (unsigned long) BYTEORDER, (int) c,
9e21b3d0
JH
2825 (int) sizeof(int), (int) sizeof(long),
2826 (int) sizeof(char *), (int) sizeof(NV)));
7a6a85bf
RG
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 */
f0ffaed8
JH
2842static int do_store(
2843 PerlIO *f,
2844 SV *sv,
2845 int optype,
2846 int network_order,
2847 SV **res)
7a6a85bf
RG
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
dd19458b 2865 if (cxt->s_dirty)
7a6a85bf
RG
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"));
dd19458b 2879 ASSERT(!cxt->s_dirty, ("clean context"));
7a6a85bf
RG
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 */
f0ffaed8 2959int pstore(PerlIO *f, SV *sv)
7a6a85bf
RG
2960{
2961 TRACEME(("pstore"));
f0ffaed8 2962 return do_store(f, sv, 0, FALSE, (SV**) 0);
7a6a85bf
RG
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 */
f0ffaed8 2972int net_pstore(PerlIO *f, SV *sv)
7a6a85bf
RG
2973{
2974 TRACEME(("net_pstore"));
f0ffaed8 2975 return do_store(f, sv, 0, TRUE, (SV**) 0);
7a6a85bf
RG
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 */
f0ffaed8 2987static SV *mbuf2sv(void)
7a6a85bf
RG
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 */
f0ffaed8 3000SV *mstore(SV *sv)
7a6a85bf
RG
3001{
3002 dSTCXT;
3003 SV *out;
3004
3005 TRACEME(("mstore"));
3006
f0ffaed8 3007 if (!do_store((PerlIO*) 0, sv, 0, FALSE, &out))
7a6a85bf
RG
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 */
f0ffaed8 3019SV *net_mstore(SV *sv)
7a6a85bf
RG
3020{
3021 dSTCXT;
3022 SV *out;
3023
3024 TRACEME(("net_mstore"));
3025
f0ffaed8 3026 if (!do_store((PerlIO*) 0, sv, 0, TRUE, &out))
7a6a85bf
RG
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 */
b12202d0 3042static SV *retrieve_other(stcxt_t *cxt, char *cname)
7a6a85bf
RG
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 */
b12202d0 3067static SV *retrieve_idx_blessed(stcxt_t *cxt, char *cname)
7a6a85bf
RG
3068{
3069 I32 idx;
3070 char *class;
3071 SV **sva;
3072 SV *sv;
3073
3074 TRACEME(("retrieve_idx_blessed (#%d)", cxt->tagnum));
b12202d0 3075 ASSERT(!cname, ("no bless-into class given here, got %s", cname));
7a6a85bf
RG
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)
29b291f7
RB
3087 CROAK(("Class name #%"IVdf" should have been seen already",
3088 (IV)idx));
7a6a85bf
RG
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
b12202d0 3098 sv = retrieve(cxt, class); /* First SV which is SEEN will be blessed */
7a6a85bf
RG
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 */
b12202d0 3109static SV *retrieve_blessed(stcxt_t *cxt, char *cname)
7a6a85bf
RG
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));
b12202d0 3117 ASSERT(!cname, ("no bless-into class given here, got %s", cname));
7a6a85bf
RG
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
b12202d0
JH
3139 TRACEME(("new class name \"%s\" will bear ID = %d", class, cxt->classnum));
3140
7a6a85bf
RG
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
b12202d0
JH
3148 sv = retrieve(cxt, class); /* First SV which is SEEN will be blessed */
3149 if (class != buf)
3150 Safefree(class);
7a6a85bf
RG
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.
b12202d0
JH
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.
7a6a85bf 3174 */
b12202d0 3175static SV *retrieve_hook(stcxt_t *cxt, char *cname)
7a6a85bf
RG
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;
7a6a85bf 3189 int clone = cxt->optype & ST_CLONE;
b12202d0
JH
3190 char mtype = '\0';
3191 unsigned int extra_type = 0;
7a6a85bf
RG
3192
3193 TRACEME(("retrieve_hook (#%d)", cxt->tagnum));
b12202d0 3194 ASSERT(!cname, ("no bless-into class given here, got %s", cname));
7a6a85bf
RG
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;
b12202d0
JH
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;
7a6a85bf 3244 default:
b12202d0 3245 return retrieve_other(cxt, 0); /* Let it croak */
7a6a85bf 3246 }
b12202d0 3247 SEEN(sv, 0); /* Don't bless yet */
7a6a85bf
RG
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..."));
b12202d0 3259 rv = retrieve(cxt, 0);
7a6a85bf
RG
3260 if (!rv)
3261 return (SV *) 0;
43d061fe
JH
3262 TRACEME(("retrieve_hook back with rv=0x%"UVxf,
3263 PTR2UV(rv)));
7a6a85bf
RG
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)
29b291f7
RB
3282 CROAK(("Class name #%"IVdf" should have been seen already",
3283 (IV)idx));
7a6a85bf
RG
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 */
dd19458b
JH
3339 if (cxt->s_tainted) /* Is input source tainted? */
3340 SvTAINT(frozen);
7a6a85bf
RG
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
9e21b3d0 3380 READ_I32(tag);
7a6a85bf
RG
3381 tag = ntohl(tag);
3382 svh = av_fetch(cxt->aseen, tag, FALSE);
3383 if (!svh)
29b291f7 3384 CROAK(("Object #%"IVdf" should have been retrieved already", (IV)tag));
7a6a85bf
RG
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");
212e9bde
JH
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 }
7a6a85bf
RG
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
86bbd6dc 3455 TRACEME(("calling STORABLE_thaw on %s at 0x%"UVxf" (%"IVdf" args)",
43d061fe 3456 class, PTR2UV(sv), AvFILLp(av) + 1));
7a6a85bf
RG
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
b12202d0
JH
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
7a6a85bf
RG
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 */
b12202d0 3537static SV *retrieve_ref(stcxt_t *cxt, char *cname)
7a6a85bf
RG
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);
b12202d0
JH
3554 SEEN(rv, cname); /* Will return if rv is null */
3555 sv = retrieve(cxt, 0); /* Retrieve <object> */
7a6a85bf
RG
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
43d061fe 3580 TRACEME(("ok (retrieve_ref at 0x%"UVxf")", PTR2UV(rv)));
7a6a85bf
RG
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 */
b12202d0 3591static SV *retrieve_overloaded(stcxt_t *cxt, char *cname)
7a6a85bf
RG
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);
b12202d0
JH
3604 SEEN(rv, cname); /* Will return if rv is null */
3605 sv = retrieve(cxt, 0); /* Retrieve <object> */
7a6a85bf
RG
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))
862382c7 3623 CROAK(("Cannot restore overloading on %s(0x%"UVxf") (package %s)",
43d061fe 3624 sv_reftype(sv, FALSE),
862382c7
JH
3625 PTR2UV(sv),
3626 stash ? HvNAME(stash) : "<unknown>"));
7a6a85bf
RG
3627
3628 SvAMAGIC_on(rv);
3629
43d061fe 3630 TRACEME(("ok (retrieve_overloaded at 0x%"UVxf")", PTR2UV(rv)));
7a6a85bf
RG
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 */
b12202d0 3641static SV *retrieve_tied_array(stcxt_t *cxt, char *cname)
7a6a85bf
RG
3642{
3643 SV *tv;
3644 SV *sv;
3645
3646 TRACEME(("retrieve_tied_array (#%d)", cxt->tagnum));
3647
3648 tv = NEWSV(10002, 0);
b12202d0
JH
3649 SEEN(tv, cname); /* Will return if tv is null */
3650 sv = retrieve(cxt, 0); /* Retrieve <object> */
7a6a85bf
RG
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
43d061fe 3659 TRACEME(("ok (retrieve_tied_array at 0x%"UVxf")", PTR2UV(tv)));
7a6a85bf
RG
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 */
b12202d0 3670static SV *retrieve_tied_hash(stcxt_t *cxt, char *cname)
7a6a85bf
RG
3671{
3672 SV *tv;
3673 SV *sv;
3674
3675 TRACEME(("retrieve_tied_hash (#%d)", cxt->tagnum));
3676
3677 tv = NEWSV(10002, 0);
b12202d0
JH
3678 SEEN(tv, cname); /* Will return if tv is null */
3679 sv = retrieve(cxt, 0); /* Retrieve <object> */
7a6a85bf
RG
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
43d061fe 3687 TRACEME(("ok (retrieve_tied_hash at 0x%"UVxf")", PTR2UV(tv)));
7a6a85bf
RG
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 */
b12202d0 3698static SV *retrieve_tied_scalar(stcxt_t *cxt, char *cname)
7a6a85bf
RG
3699{
3700 SV *tv;
3701 SV *sv;
3702
3703 TRACEME(("retrieve_tied_scalar (#%d)", cxt->tagnum));
3704
3705 tv = NEWSV(10002, 0);
b12202d0
JH
3706 SEEN(tv, cname); /* Will return if rv is null */
3707 sv = retrieve(cxt, 0); /* Retrieve <object> */
7a6a85bf
RG
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
43d061fe 3715 TRACEME(("ok (retrieve_tied_scalar at 0x%"UVxf")", PTR2UV(tv)));
7a6a85bf
RG
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 */
b12202d0 3726static SV *retrieve_tied_key(stcxt_t *cxt, char *cname)
7a6a85bf
RG
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);
b12202d0
JH
3735 SEEN(tv, cname); /* Will return if tv is null */
3736 sv = retrieve(cxt, 0); /* Retrieve <object> */
7a6a85bf
RG
3737 if (!sv)
3738 return (SV *) 0; /* Failed */
3739
b12202d0 3740 key = retrieve(cxt, 0); /* Retrieve <key> */
7a6a85bf
RG
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 */
b12202d0 3758static SV *retrieve_tied_idx(stcxt_t *cxt, char *cname)
7a6a85bf
RG
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);
b12202d0
JH
3767 SEEN(tv, cname); /* Will return if tv is null */
3768 sv = retrieve(cxt, 0); /* Retrieve <object> */
7a6a85bf
RG
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 */
b12202d0 3791static SV *retrieve_lscalar(stcxt_t *cxt, char *cname)
7a6a85bf 3792{
9e21b3d0 3793 I32 len;
7a6a85bf
RG
3794 SV *sv;
3795
3796 RLEN(len);
86bbd6dc 3797 TRACEME(("retrieve_lscalar (#%d), len = %"IVdf, cxt->tagnum, len));
7a6a85bf
RG
3798
3799 /*
3800 * Allocate an empty scalar of the suitable length.
3801 */
3802
3803 sv = NEWSV(10002, len);
b12202d0 3804 SEEN(sv, cname); /* Associate this new scalar with tag "tagnum" */
7a6a85bf
RG
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 */
dd19458b
JH
3819 if (cxt->s_tainted) /* Is input source tainted? */
3820 SvTAINT(sv); /* External data cannot be trusted */
7a6a85bf 3821
86bbd6dc 3822 TRACEME(("large scalar len %"IVdf" '%s'", len, SvPVX(sv)));
43d061fe 3823 TRACEME(("ok (retrieve_lscalar at 0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
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 */
b12202d0 3837static SV *retrieve_scalar(stcxt_t *cxt, char *cname)
7a6a85bf
RG
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);
b12202d0 3850 SEEN(sv, cname); /* Associate this new scalar with tag "tagnum" */
7a6a85bf
RG
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 */
43d061fe 3864 TRACEME(("ok (retrieve_scalar empty at 0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
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 */
dd19458b
JH
3879 if (cxt->s_tainted) /* Is input source tainted? */
3880 SvTAINT(sv); /* External data cannot be trusted */
7a6a85bf 3881
43d061fe 3882 TRACEME(("ok (retrieve_scalar at 0x%"UVxf")", PTR2UV(sv)));
7a6a85bf
RG
3883 return sv;
3884}
3885
3886/*
dd19458b
JH
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 */
b12202d0 3892static SV *retrieve_utf8str(stcxt_t *cxt, char *cname)
dd19458b
JH
3893{
3894 SV *sv;
3895
3896 TRACEME(("retrieve_utf8str"));
3897
b12202d0 3898 sv = retrieve_scalar(cxt, cname);
dd19458b
JH
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 */
b12202d0 3911static SV *retrieve_lutf8str(stcxt_t *cxt, char *cname)
dd19458b
JH
3912{
3913 SV *sv;
3914
3915 TRACEME(("retrieve_lutf8str"));
3916
b12202d0 3917 sv = retrieve_lscalar(cxt, cname);
dd19458b
JH
3918 if (sv)
3919 SvUTF8_on(sv);