ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Compress-LZF/LZF.xs
(Generate patch)

Comparing Compress-LZF/LZF.xs (file contents):
Revision 1.8 by root, Wed Feb 27 20:51:21 2002 UTC vs.
Revision 1.9 by root, Sun Mar 3 04:45:18 2002 UTC

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 */ 24#define MAGIC_undef 2 /* the special value undef */
25#define MAGIC_CR 3 /* storable (reference, freeze), compressed */ 25#define MAGIC_CR 3 /* storable (reference, freeze), compressed */
26#define MAGIC_R 4 /* storable (reference, freeze) */ 26#define MAGIC_R 4 /* storable (reference, freeze) */
27#define MAGIC_CR_deref 5 /* storable (NO reference, freeze), compressed */
28#define MAGIC_R_deref 6 /* storable NO (reference, freeze) */
27#define MAGIC_HI 7 /* room for one higher storable major */ 29#define MAGIC_HI 7 /* room for one higher storable major */
28 30
29#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))
30 32
31static CV *storable_mstore, *storable_mretrieve; 33static CV *storable_mstore, *storable_mretrieve;
228 230
229 if (!SvOK (sv)) 231 if (!SvOK (sv))
230 XPUSHs (sv_2mortal (newSVpvn ("\02", 1))); /* 02 == MAGIC_undef */ 232 XPUSHs (sv_2mortal (newSVpvn ("\02", 1))); /* 02 == MAGIC_undef */
231 else if (SvTYPE(sv) != SVt_IV 233 else if (SvTYPE(sv) != SVt_IV
232 && SvTYPE(sv) != SVt_NV 234 && SvTYPE(sv) != SVt_NV
233 && SvTYPE(sv) != SVt_PV
234 && SvTYPE(sv) != SVt_PVMG) /* mstore */ 235 && SvTYPE(sv) != SVt_PV) /* mstore */
235 { 236 {
237 int deref = !SvROK (sv);
238
236 if (!storable_mstore) 239 if (!storable_mstore)
237 need_storable (); 240 need_storable ();
241
242 if (deref)
243 sv = newRV_noinc (sv);
238 244
239 PUSHMARK (SP); 245 PUSHMARK (SP);
240 XPUSHs (sv); 246 XPUSHs (sv);
241 PUTBACK; 247 PUTBACK;
242 248
249 255
250 if (SvPVX (sv)[0] != MAGIC_R) 256 if (SvPVX (sv)[0] != MAGIC_R)
251 croak ("Storable format changed, need newer version of Compress::LZF"); 257 croak ("Storable format changed, need newer version of Compress::LZF");
252 258
253 if (ix) /* compress */ 259 if (ix) /* compress */
254 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_CR, 0))); 260 XPUSHs (sv_2mortal (compress_sv (sv, deref ? MAGIC_CR_deref : MAGIC_CR, 0)));
255 else 261 else
262 {
263 if (deref)
264 SvPVX (sv)[0] = MAGIC_R_deref;
265
256 XPUSHs (sv); 266 XPUSHs (sv);
267 }
257 } 268 }
258 else if (sv && IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI)) 269 else if (sv && IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI))
259 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 */
260 else if (ix == 2) /* compress always */ 271 else if (ix == 2) /* compress always */
261 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 0))); 272 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 0)));
265void 276void
266sthaw(sv) 277sthaw(sv)
267 SV * sv 278 SV * sv
268 PROTOTYPE: $ 279 PROTOTYPE: $
269 PPCODE: 280 PPCODE:
281
282 int deref = 0;
270 283
271 SvGETMAGIC (sv); 284 SvGETMAGIC (sv);
272 if (SvPOK (sv) && IN_RANGE (SvPV_nolen (sv)[0], MAGIC_LO, MAGIC_HI)) 285 if (SvPOK (sv) && IN_RANGE (SvPV_nolen (sv)[0], MAGIC_LO, MAGIC_HI))
273 { 286 {
274 switch (SvPVX (sv)[0]) 287 switch (SvPVX (sv)[0])
283 296
284 case MAGIC_C: 297 case MAGIC_C:
285 XPUSHs (sv_2mortal (decompress_sv (sv, 1))); 298 XPUSHs (sv_2mortal (decompress_sv (sv, 1)));
286 break; 299 break;
287 300
301 case MAGIC_CR_deref:
302 deref = 1;
288 case MAGIC_CR: 303 case MAGIC_CR:
289 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 }
290 case MAGIC_R: 311 case MAGIC_R:
291 if (!storable_mstore) 312 if (!storable_mstore)
292 need_storable (); 313 need_storable ();
293 314
294 PUSHMARK (SP); 315 PUSHMARK (SP);
298 if (1 != call_sv ((SV *)storable_mretrieve, G_SCALAR)) 319 if (1 != call_sv ((SV *)storable_mretrieve, G_SCALAR))
299 croak ("Storable::mstore didn't return a single scalar"); 320 croak ("Storable::mstore didn't return a single scalar");
300 321
301 SPAGAIN; 322 SPAGAIN;
302 323
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
303 /*XPUSHs (POPs); this is a nop, hopefully */ 335 XPUSHs (POPs); /* this is a nop, hopefully */
304 336
305 break; 337 break;
306 338
307 default: 339 default:
308 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?");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines