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