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.2 by root, Thu Sep 27 19:24:55 2001 UTC vs.
Revision 1.9 by root, Sun Mar 3 04:45:18 2002 UTC

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
30static CV *storable_mstore, *storable_mretrieve; 33static 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
228 if (SvROK (sv)) /* mstore */ 231 if (!SvOK (sv))
232 XPUSHs (sv_2mortal (newSVpvn ("\02", 1))); /* 02 == MAGIC_undef */
233 else if (SvTYPE(sv) != SVt_IV
234 && SvTYPE(sv) != SVt_NV
235 && SvTYPE(sv) != SVt_PV) /* mstore */
229 { 236 {
237 int deref = !SvROK (sv);
238
230 if (!storable_mstore) 239 if (!storable_mstore)
231 need_storable (); 240 need_storable ();
241
242 if (deref)
243 sv = newRV_noinc (sv);
232 244
233 PUSHMARK (SP); 245 PUSHMARK (SP);
234 XPUSHs (sv); 246 XPUSHs (sv);
235 PUTBACK; 247 PUTBACK;
236 248
243 255
244 if (SvPVX (sv)[0] != MAGIC_R) 256 if (SvPVX (sv)[0] != MAGIC_R)
245 croak ("Storable format changed, need newer version of Compress::LZF"); 257 croak ("Storable format changed, need newer version of Compress::LZF");
246 258
247 if (ix) /* compress */ 259 if (ix) /* compress */
248 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_CR, 0))); 260 XPUSHs (sv_2mortal (compress_sv (sv, deref ? MAGIC_CR_deref : MAGIC_CR, 0)));
249 else 261 else
262 {
263 if (deref)
264 SvPVX (sv)[0] = MAGIC_R_deref;
265
250 XPUSHs (sv); 266 XPUSHs (sv);
267 }
251 } 268 }
252 else if (IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI)) 269 else if (sv && IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI))
253 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 */
254 else if (ix == 2) /* compress always */ 271 else if (ix == 2) /* compress always */
255 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 0))); 272 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 0)));
256 else /* don't compress */ 273 else /* don't compress */
257 XPUSHs (sv_2mortal (SvREFCNT_inc (sv))); 274 XPUSHs (sv_2mortal (SvREFCNT_inc (sv)));
259void 276void
260sthaw(sv) 277sthaw(sv)
261 SV * sv 278 SV * sv
262 PROTOTYPE: $ 279 PROTOTYPE: $
263 PPCODE: 280 PPCODE:
264 char hdr = SvPVX (sv)[0];
265 281
266 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))
267 { 286 {
268 switch (SvPVX (sv)[0]) 287 switch (SvPVX (sv)[0])
269 { 288 {
289 case MAGIC_undef:
290 XPUSHs (sv_2mortal (NEWSV (0, 0)));
291 break;
292
270 case MAGIC_U: 293 case MAGIC_U:
271 XPUSHs (sv_2mortal (decompress_sv (sv, 0))); 294 XPUSHs (sv_2mortal (decompress_sv (sv, 0)));
272 break; 295 break;
273 296
274 case MAGIC_C: 297 case MAGIC_C:
275 XPUSHs (sv_2mortal (decompress_sv (sv, 1))); 298 XPUSHs (sv_2mortal (decompress_sv (sv, 1)));
276 break; 299 break;
277 300
301 case MAGIC_CR_deref:
302 deref = 1;
278 case MAGIC_CR: 303 case MAGIC_CR:
279 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 }
280 case MAGIC_R: 311 case MAGIC_R:
281 if (!storable_mstore) 312 if (!storable_mstore)
282 need_storable (); 313 need_storable ();
283 314
284 PUSHMARK (SP); 315 PUSHMARK (SP);
288 if (1 != call_sv ((SV *)storable_mretrieve, G_SCALAR)) 319 if (1 != call_sv ((SV *)storable_mretrieve, G_SCALAR))
289 croak ("Storable::mstore didn't return a single scalar"); 320 croak ("Storable::mstore didn't return a single scalar");
290 321
291 SPAGAIN; 322 SPAGAIN;
292 323
293 /*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 */
294 336
295 break; 337 break;
296 338
297 default: 339 default:
298 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