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

8#include "patchlevel.h" 8#include "patchlevel.h"
9#if (PATCHLEVEL == 4) || ((PATCHLEVEL == 5) && (SUBVERSION < 55)) 9#if (PATCHLEVEL == 4) || ((PATCHLEVEL == 5) && (SUBVERSION < 55))
10static STRLEN nolen_na; 10static STRLEN nolen_na;
11# define SvPV_nolen(sv) SvPV ((sv), nolen_na) 11# define SvPV_nolen(sv) SvPV ((sv), nolen_na)
12#endif 12#endif
13#if PATCHLEVEL < 6
14# define call_sv perl_call_sv
15#endif
13 16
14#include "lzf_c.c" 17#include "lzf_c.c"
15#include "lzf_d.c" 18#include "lzf_d.c"
16 19
17/* we re-use the storable header for our purposes */ 20/* we re-use the storable header for our purposes */
18#define MAGIC_LO 0 21#define MAGIC_LO 0
19#define MAGIC_U 0 /* uncompressed data follows */ 22#define MAGIC_U 0 /* uncompressed data follows */
20#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 */
21#define MAGIC_R 4 /* storable (reference, freeze) */ 26#define MAGIC_R 4 /* storable (reference, freeze) */
22#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) */
23#define MAGIC_HI 7 /* room for one higher storable major */ 29#define MAGIC_HI 7 /* room for one higher storable major */
24 30
25#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))
26 32
27static CV *storable_mstore, *storable_mretrieve; 33static CV *storable_mstore, *storable_mretrieve;
185} 191}
186 192
187static void 193static void
188need_storable(void) 194need_storable(void)
189{ 195{
196#if PATCHLEVEL < 6
197 perl_eval_pv ("require Storable;", 1);
198#else
190 load_module (PERL_LOADMOD_NOIMPORT, newSVpv ("Storable", 0), Nullsv); 199 load_module (PERL_LOADMOD_NOIMPORT, newSVpv ("Storable", 0), Nullsv);
200#endif
191 201
192 storable_mstore = GvCV (gv_fetchpv ("Storable::mstore" , TRUE, SVt_PVCV)); 202 storable_mstore = GvCV (gv_fetchpv ("Storable::mstore" , TRUE, SVt_PVCV));
193 storable_mretrieve = GvCV (gv_fetchpv ("Storable::mretrieve", TRUE, SVt_PVCV)); 203 storable_mretrieve = GvCV (gv_fetchpv ("Storable::mretrieve", TRUE, SVt_PVCV));
194} 204}
195 205
216 sfreeze_cr = 1 226 sfreeze_cr = 1
217 sfreeze_c = 2 227 sfreeze_c = 2
218 PROTOTYPE: $ 228 PROTOTYPE: $
219 PPCODE: 229 PPCODE:
220 230
221 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 */
222 { 236 {
237 int deref = !SvROK (sv);
238
223 if (!storable_mstore) 239 if (!storable_mstore)
224 need_storable (); 240 need_storable ();
241
242 if (deref)
243 sv = newRV_noinc (sv);
225 244
226 PUSHMARK (SP); 245 PUSHMARK (SP);
227 XPUSHs (sv); 246 XPUSHs (sv);
228 PUTBACK; 247 PUTBACK;
229 248
236 255
237 if (SvPVX (sv)[0] != MAGIC_R) 256 if (SvPVX (sv)[0] != MAGIC_R)
238 croak ("Storable format changed, need newer version of Compress::LZF"); 257 croak ("Storable format changed, need newer version of Compress::LZF");
239 258
240 if (ix) /* compress */ 259 if (ix) /* compress */
241 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_CR, 0))); 260 XPUSHs (sv_2mortal (compress_sv (sv, deref ? MAGIC_CR_deref : MAGIC_CR, 0)));
242 else 261 else
262 {
263 if (deref)
264 SvPVX (sv)[0] = MAGIC_R_deref;
265
243 XPUSHs (sv); 266 XPUSHs (sv);
267 }
244 } 268 }
245 else if (IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI)) 269 else if (sv && IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI))
246 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 */
247 else if (ix == 2) /* compress always */ 271 else if (ix == 2) /* compress always */
248 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 0))); 272 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, 0)));
249 else /* don't compress */ 273 else /* don't compress */
250 XPUSHs (sv_2mortal (SvREFCNT_inc (sv))); 274 XPUSHs (sv_2mortal (SvREFCNT_inc (sv)));
252void 276void
253sthaw(sv) 277sthaw(sv)
254 SV * sv 278 SV * sv
255 PROTOTYPE: $ 279 PROTOTYPE: $
256 PPCODE: 280 PPCODE:
257 char hdr = SvPVX (sv)[0];
258 281
259 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))
260 { 286 {
261 switch (SvPVX (sv)[0]) 287 switch (SvPVX (sv)[0])
262 { 288 {
289 case MAGIC_undef:
290 XPUSHs (sv_2mortal (NEWSV (0, 0)));
291 break;
292
263 case MAGIC_U: 293 case MAGIC_U:
264 XPUSHs (sv_2mortal (decompress_sv (sv, 0))); 294 XPUSHs (sv_2mortal (decompress_sv (sv, 0)));
265 break; 295 break;
266 296
267 case MAGIC_C: 297 case MAGIC_C:
268 XPUSHs (sv_2mortal (decompress_sv (sv, 1))); 298 XPUSHs (sv_2mortal (decompress_sv (sv, 1)));
269 break; 299 break;
270 300
301 case MAGIC_CR_deref:
302 deref = 1;
271 case MAGIC_CR: 303 case MAGIC_CR:
272 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 }
273 case MAGIC_R: 311 case MAGIC_R:
274 if (!storable_mstore) 312 if (!storable_mstore)
275 need_storable (); 313 need_storable ();
276 314
277 PUSHMARK (SP); 315 PUSHMARK (SP);
281 if (1 != call_sv ((SV *)storable_mretrieve, G_SCALAR)) 319 if (1 != call_sv ((SV *)storable_mretrieve, G_SCALAR))
282 croak ("Storable::mstore didn't return a single scalar"); 320 croak ("Storable::mstore didn't return a single scalar");
283 321
284 SPAGAIN; 322 SPAGAIN;
285 323
286 /*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 */
287 336
288 break; 337 break;
289 338
290 default: 339 default:
291 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