… | |
… | |
19 | |
19 | |
20 | /* we re-use the storable header for our purposes */ |
20 | /* we re-use the storable header for our purposes */ |
21 | #define MAGIC_LO 0 |
21 | #define MAGIC_LO 0 |
22 | #define MAGIC_U 0 /* uncompressed data follows */ |
22 | #define MAGIC_U 0 /* uncompressed data follows */ |
23 | #define MAGIC_C 1 /* compressed data follows */ |
23 | #define MAGIC_C 1 /* compressed data follows */ |
|
|
24 | #define MAGIC_undef 2 /* the special value undef */ |
|
|
25 | #define MAGIC_CR 3 /* storable (reference, freeze), compressed */ |
24 | #define MAGIC_R 4 /* storable (reference, freeze) */ |
26 | #define MAGIC_R 4 /* storable (reference, freeze) */ |
25 | #define MAGIC_CR 3 /* storable (reference, freeze), compressed */ |
27 | #define MAGIC_CR_deref 5 /* storable (NO reference, freeze), compressed */ |
|
|
28 | #define MAGIC_R_deref 6 /* storable NO (reference, freeze) */ |
26 | #define MAGIC_HI 7 /* room for one higher storable major */ |
29 | #define MAGIC_HI 7 /* room for one higher storable major */ |
27 | |
30 | |
28 | #define IN_RANGE(v,l,h) ((unsigned int)((unsigned)(v) - (unsigned)(l)) <= (unsigned)(h) - (unsigned)(l)) |
31 | #define IN_RANGE(v,l,h) ((unsigned int)((unsigned)(v) - (unsigned)(l)) <= (unsigned)(h) - (unsigned)(l)) |
29 | |
32 | |
30 | static CV *storable_mstore, *storable_mretrieve; |
33 | static CV *storable_mstore, *storable_mretrieve; |
… | |
… | |
223 | sfreeze_cr = 1 |
226 | sfreeze_cr = 1 |
224 | sfreeze_c = 2 |
227 | sfreeze_c = 2 |
225 | PROTOTYPE: $ |
228 | PROTOTYPE: $ |
226 | PPCODE: |
229 | PPCODE: |
227 | |
230 | |
|
|
231 | if (!SvOK (sv)) |
|
|
232 | XPUSHs (sv_2mortal (newSVpvn ("\02", 1))); /* 02 == MAGIC_undef */ |
228 | if (SvTYPE(sv) != SVt_IV |
233 | else if (SvTYPE(sv) != SVt_IV |
229 | && SvTYPE(sv) != SVt_NV |
234 | && SvTYPE(sv) != SVt_NV |
230 | && SvTYPE(sv) != SVt_PV) /* mstore */ |
235 | && SvTYPE(sv) != SVt_PV) /* mstore */ |
231 | { |
236 | { |
|
|
237 | int deref = !SvROK (sv); |
|
|
238 | |
232 | if (!storable_mstore) |
239 | if (!storable_mstore) |
233 | need_storable (); |
240 | need_storable (); |
|
|
241 | |
|
|
242 | if (deref) |
|
|
243 | sv = newRV_noinc (sv); |
234 | |
244 | |
235 | PUSHMARK (SP); |
245 | PUSHMARK (SP); |
236 | XPUSHs (sv); |
246 | XPUSHs (sv); |
237 | PUTBACK; |
247 | PUTBACK; |
238 | |
248 | |
… | |
… | |
245 | |
255 | |
246 | if (SvPVX (sv)[0] != MAGIC_R) |
256 | if (SvPVX (sv)[0] != MAGIC_R) |
247 | croak ("Storable format changed, need newer version of Compress::LZF"); |
257 | croak ("Storable format changed, need newer version of Compress::LZF"); |
248 | |
258 | |
249 | if (ix) /* compress */ |
259 | if (ix) /* compress */ |
250 | XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_CR, 0))); |
260 | XPUSHs (sv_2mortal (compress_sv (sv, deref ? MAGIC_CR_deref : MAGIC_CR, 0))); |
251 | else |
261 | else |
|
|
262 | { |
|
|
263 | if (deref) |
|
|
264 | SvPVX (sv)[0] = MAGIC_R_deref; |
|
|
265 | |
252 | XPUSHs (sv); |
266 | XPUSHs (sv); |
|
|
267 | } |
253 | } |
268 | } |
254 | else if (IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI)) |
269 | else if (sv && IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI)) |
255 | XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 1))); /* need to prefix only */ |
270 | XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 1))); /* need to prefix only */ |
256 | else if (ix == 2) /* compress always */ |
271 | else if (ix == 2) /* compress always */ |
257 | XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 0))); |
272 | XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 0))); |
258 | else /* don't compress */ |
273 | else /* don't compress */ |
259 | XPUSHs (sv_2mortal (SvREFCNT_inc (sv))); |
274 | XPUSHs (sv_2mortal (SvREFCNT_inc (sv))); |
… | |
… | |
261 | void |
276 | void |
262 | sthaw(sv) |
277 | sthaw(sv) |
263 | SV * sv |
278 | SV * sv |
264 | PROTOTYPE: $ |
279 | PROTOTYPE: $ |
265 | PPCODE: |
280 | PPCODE: |
266 | char hdr = SvPVX (sv)[0]; |
|
|
267 | |
281 | |
268 | if (IN_RANGE (hdr, MAGIC_LO, MAGIC_HI)) |
282 | int deref = 0; |
|
|
283 | |
|
|
284 | SvGETMAGIC (sv); |
|
|
285 | if (SvPOK (sv) && IN_RANGE (SvPV_nolen (sv)[0], MAGIC_LO, MAGIC_HI)) |
269 | { |
286 | { |
270 | switch (SvPVX (sv)[0]) |
287 | switch (SvPVX (sv)[0]) |
271 | { |
288 | { |
|
|
289 | case MAGIC_undef: |
|
|
290 | XPUSHs (sv_2mortal (NEWSV (0, 0))); |
|
|
291 | break; |
|
|
292 | |
272 | case MAGIC_U: |
293 | case MAGIC_U: |
273 | XPUSHs (sv_2mortal (decompress_sv (sv, 0))); |
294 | XPUSHs (sv_2mortal (decompress_sv (sv, 0))); |
274 | break; |
295 | break; |
275 | |
296 | |
276 | case MAGIC_C: |
297 | case MAGIC_C: |
277 | XPUSHs (sv_2mortal (decompress_sv (sv, 1))); |
298 | XPUSHs (sv_2mortal (decompress_sv (sv, 1))); |
278 | break; |
299 | break; |
279 | |
300 | |
|
|
301 | case MAGIC_CR_deref: |
|
|
302 | deref = 1; |
280 | case MAGIC_CR: |
303 | case MAGIC_CR: |
281 | sv = sv_2mortal (decompress_sv (sv, 1)); /* mortal could be optimized */ |
304 | sv = sv_2mortal (decompress_sv (sv, 1)); /* mortal could be optimized */ |
|
|
305 | case MAGIC_R_deref: |
|
|
306 | if (SvPVX (sv)[0] == MAGIC_R_deref) |
|
|
307 | { |
|
|
308 | deref = 1; |
|
|
309 | SvPVX (sv)[0] = MAGIC_R; |
|
|
310 | } |
282 | case MAGIC_R: |
311 | case MAGIC_R: |
283 | if (!storable_mstore) |
312 | if (!storable_mstore) |
284 | need_storable (); |
313 | need_storable (); |
285 | |
314 | |
286 | PUSHMARK (SP); |
315 | PUSHMARK (SP); |
… | |
… | |
290 | if (1 != call_sv ((SV *)storable_mretrieve, G_SCALAR)) |
319 | if (1 != call_sv ((SV *)storable_mretrieve, G_SCALAR)) |
291 | croak ("Storable::mstore didn't return a single scalar"); |
320 | croak ("Storable::mstore didn't return a single scalar"); |
292 | |
321 | |
293 | SPAGAIN; |
322 | SPAGAIN; |
294 | |
323 | |
295 | /*XPUSHs (POPs);*/ |
324 | if (deref) |
|
|
325 | { |
|
|
326 | SV *ref = SvREFCNT_inc (SvRV (TOPs)); |
|
|
327 | |
|
|
328 | SvREFCNT_dec (TOPs); /* destroy superfluous ref */ |
|
|
329 | SETs (ref); |
|
|
330 | |
|
|
331 | if (SvPVX (sv)[0] == MAGIC_R) |
|
|
332 | SvPVX (sv)[0] = MAGIC_R; |
|
|
333 | } |
|
|
334 | |
|
|
335 | XPUSHs (POPs); /* this is a nop, hopefully */ |
296 | |
336 | |
297 | break; |
337 | break; |
298 | |
338 | |
299 | default: |
339 | default: |
300 | croak ("Compress::LZF::sthaw(): invalid data, maybe you need a newer version of Compress::LZF?"); |
340 | croak ("Compress::LZF::sthaw(): invalid data, maybe you need a newer version of Compress::LZF?"); |