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.28 by root, Wed Dec 17 00:04:56 2008 UTC vs.
Revision 1.33 by root, Sun Jun 28 18:18:03 2015 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include "perlmulticore.h"
6
5#define LZF_STANDALONE 1 7#define LZF_STANDALONE 1
6#define LZF_STATE_ARG 1 8#define LZF_STATE_ARG 1
7 9
8#include "lzf_c.c" 10#include "lzf_c.c"
9#include "lzf_d.c" 11#include "lzf_d.c"
12#include "lzf_c_best.c"
10 13
11/* we re-use the storable header for our purposes */ 14/* we re-use the storable header for our purposes */
12#define MAGIC_LO 0 15#define MAGIC_LO 0
13#define MAGIC_U 0 /* uncompressed data follows */ 16#define MAGIC_U 0 /* uncompressed data follows */
14#define MAGIC_C 1 /* compressed data follows */ 17#define MAGIC_C 1 /* compressed data follows */
30#else 33#else
31# define MAX_LENGTH ((Size_t) 0x8000000L) 34# define MAX_LENGTH ((Size_t) 0x8000000L)
32#endif 35#endif
33 36
34static SV * 37static SV *
35compress_sv (SV *data, char cprepend, int uprepend) 38compress_sv (SV *data, char cprepend, int uprepend, int best)
36{ 39{
37 LZF_STATE *state; 40 LZF_STATE *state;
38 STRLEN usize, csize; 41 STRLEN usize, csize;
39 char *src = (char *)SvPVbyte (data, usize); 42 char *src = (char *)SvPVbyte (data, usize);
40 43
52 55
53 if (usize <= 0x7f) 56 if (usize <= 0x7f)
54 { 57 {
55 dst[skip++] = usize; 58 dst[skip++] = usize;
56 } 59 }
57 else if (usize <= 0x7ff) 60 else if (usize <= 0x7ff)
58 { 61 {
59 dst[skip++] = (( usize >> 6) | 0xc0); 62 dst[skip++] = (( usize >> 6) | 0xc0);
60 dst[skip++] = (( usize & 0x3f) | 0x80); 63 dst[skip++] = (( usize & 0x3f) | 0x80);
61 } 64 }
62 else if (usize <= 0xffff) 65 else if (usize <= 0xffff)
63 { 66 {
64 dst[skip++] = (( usize >> 12) | 0xe0); 67 dst[skip++] = (( usize >> 12) | 0xe0);
65 dst[skip++] = (((usize >> 6) & 0x3f) | 0x80); 68 dst[skip++] = (((usize >> 6) & 0x3f) | 0x80);
66 dst[skip++] = (( usize & 0x3f) | 0x80); 69 dst[skip++] = (( usize & 0x3f) | 0x80);
67 } 70 }
68 else if (usize <= 0x1fffff) 71 else if (usize <= 0x1fffff)
69 { 72 {
70 dst[skip++] = (( usize >> 18) | 0xf0); 73 dst[skip++] = (( usize >> 18) | 0xf0);
71 dst[skip++] = (((usize >> 12) & 0x3f) | 0x80); 74 dst[skip++] = (((usize >> 12) & 0x3f) | 0x80);
72 dst[skip++] = (((usize >> 6) & 0x3f) | 0x80); 75 dst[skip++] = (((usize >> 6) & 0x3f) | 0x80);
73 dst[skip++] = (( usize & 0x3f) | 0x80); 76 dst[skip++] = (( usize & 0x3f) | 0x80);
74 } 77 }
75 else if (usize <= 0x3ffffff) 78 else if (usize <= 0x3ffffff)
76 { 79 {
77 dst[skip++] = (( usize >> 24) | 0xf8); 80 dst[skip++] = (( usize >> 24) | 0xf8);
78 dst[skip++] = (((usize >> 18) & 0x3f) | 0x80); 81 dst[skip++] = (((usize >> 18) & 0x3f) | 0x80);
79 dst[skip++] = (((usize >> 12) & 0x3f) | 0x80); 82 dst[skip++] = (((usize >> 12) & 0x3f) | 0x80);
80 dst[skip++] = (((usize >> 6) & 0x3f) | 0x80); 83 dst[skip++] = (((usize >> 6) & 0x3f) | 0x80);
81 dst[skip++] = (( usize & 0x3f) | 0x80); 84 dst[skip++] = (( usize & 0x3f) | 0x80);
82 } 85 }
83 else if (usize <= 0x7fffffff) 86 else if (usize <= 0x7fffffff)
84 { 87 {
85 dst[skip++] = (( usize >> 30) | 0xfc); 88 dst[skip++] = (( usize >> 30) | 0xfc);
86 dst[skip++] = (((usize >> 24) & 0x3f) | 0x80); 89 dst[skip++] = (((usize >> 24) & 0x3f) | 0x80);
87 dst[skip++] = (((usize >> 18) & 0x3f) | 0x80); 90 dst[skip++] = (((usize >> 18) & 0x3f) | 0x80);
88 dst[skip++] = (((usize >> 12) & 0x3f) | 0x80); 91 dst[skip++] = (((usize >> 12) & 0x3f) | 0x80);
94 97
95 New (0, state, 1, LZF_STATE); 98 New (0, state, 1, LZF_STATE);
96 if (!state) 99 if (!state)
97 croak ("Compress::LZF unable to allocate memory for compression state"); 100 croak ("Compress::LZF unable to allocate memory for compression state");
98 101
102 if (usize > 2000) perlinterp_release ();
99 /* 11 bytes is the smallest compressible string */ 103 /* 11 bytes is the smallest compressible string */
100 csize = usize < 11 ? 0 : 104 csize = usize < 11 ? 0 :
105 (best ? lzf_compress_best (src, usize, dst + skip, usize - skip)
101 lzf_compress (src, usize, dst + skip, usize - skip, *state); 106 : lzf_compress (src, usize, dst + skip, usize - skip, *state));
107 if (usize > 2000) perlinterp_acquire ();
102 108
103 Safefree (state); 109 Safefree (state);
104 110
105 if (csize) 111 if (csize)
106 { 112 {
134 140
135 if (csize) 141 if (csize)
136 { 142 {
137 void *dst; 143 void *dst;
138 SV *ret; 144 SV *ret;
145 int res;
139 146
140 csize -= skip; 147 csize -= skip;
141 148
142 if (src[0]) 149 if (src[0])
143 { 150 {
189 else 196 else
190 croak ("compressed data corrupted (invalid length)"); 197 croak ("compressed data corrupted (invalid length)");
191 198
192 if (!usize) 199 if (!usize)
193 croak ("compressed data corrupted (invalid length)"); 200 croak ("compressed data corrupted (invalid length)");
194 201
195 ret = NEWSV (0, usize); 202 ret = NEWSV (0, usize);
196 SvPOK_only (ret); 203 SvPOK_only (ret);
197 dst = SvPVX (ret); 204 dst = SvPVX (ret);
198 205
206 if (usize > 4000) perlinterp_release ();
199 if (lzf_decompress (src, csize, dst, usize) != usize) 207 res = lzf_decompress (src, csize, dst, usize) != usize;
208 if (usize > 4000) perlinterp_acquire ();
209
210 if (res)
200 { 211 {
201 SvREFCNT_dec (ret); 212 SvREFCNT_dec (ret);
202 croak ("compressed data corrupted (size mismatch)", csize, skip, usize); 213 croak ("compressed data corrupted (size mismatch)", csize, skip, usize);
203 } 214 }
204 } 215 }
220} 231}
221 232
222static void 233static void
223need_storable (void) 234need_storable (void)
224{ 235{
225 require_pv (SvPVbyte_nolen (serializer_package)); 236 eval_sv (sv_2mortal (newSVpvf ("require %s", SvPVbyte_nolen (serializer_package))), G_VOID | G_DISCARD);
226 237
227 storable_mstore = (CV *)SvREFCNT_inc (GvCV (gv_fetchpv (SvPVbyte_nolen (serializer_mstore ), TRUE, SVt_PVCV))); 238 storable_mstore = (CV *)SvREFCNT_inc (GvCV (gv_fetchpv (SvPVbyte_nolen (serializer_mstore ), TRUE, SVt_PVCV)));
228 storable_mretrieve = (CV *)SvREFCNT_inc (GvCV (gv_fetchpv (SvPVbyte_nolen (serializer_mretrieve), TRUE, SVt_PVCV))); 239 storable_mretrieve = (CV *)SvREFCNT_inc (GvCV (gv_fetchpv (SvPVbyte_nolen (serializer_mretrieve), TRUE, SVt_PVCV)));
229} 240}
230 241
249 SvREFCNT_dec (storable_mretrieve); storable_mretrieve = 0; 260 SvREFCNT_dec (storable_mretrieve); storable_mretrieve = 0;
250 261
251void 262void
252compress(data) 263compress(data)
253 SV * data 264 SV * data
265 ALIAS:
266 compress_best = 1
254 PROTOTYPE: $ 267 PROTOTYPE: $
255 PPCODE: 268 PPCODE:
256 XPUSHs (sv_2mortal (compress_sv (data, 0, MAGIC_U))); 269 XPUSHs (sv_2mortal (compress_sv (data, 0, MAGIC_U, ix)));
257 270
258void 271void
259decompress(data) 272decompress(data)
260 SV * data 273 SV * data
261 PROTOTYPE: $ 274 PROTOTYPE: $
264 277
265void 278void
266sfreeze(sv) 279sfreeze(sv)
267 SV * sv 280 SV * sv
268 ALIAS: 281 ALIAS:
282 sfreeze = 0
269 sfreeze_cr = 1 283 sfreeze_cr = 1
270 sfreeze_c = 2 284 sfreeze_c = 2
285 sfreeze_best = 4
286 sfreeze_cr_best = 5
287 sfreeze_c_best = 6
271 PROTOTYPE: $ 288 PROTOTYPE: $
272 PPCODE: 289 PPCODE:
290{
291 int best = ix & 4;
292 ix &= 3;
273 293
274 SvGETMAGIC (sv); 294 SvGETMAGIC (sv);
275 295
276 if (!SvOK (sv)) 296 if (!SvOK (sv))
277 XPUSHs (sv_2mortal (newSVpvn ("\02", 1))); /* 02 == MAGIC_undef */ 297 XPUSHs (sv_2mortal (newSVpvn ("\02", 1))); /* 02 == MAGIC_undef */
323 343
324 sv_insert (sv, 0, 0, pfx, 2); 344 sv_insert (sv, 0, 0, pfx, 2);
325 } 345 }
326 346
327 if (ix) /* compress */ 347 if (ix) /* compress */
328 sv = sv_2mortal (compress_sv (sv, deref ? MAGIC_CR_deref : MAGIC_CR, -1)); 348 sv = sv_2mortal (compress_sv (sv, deref ? MAGIC_CR_deref : MAGIC_CR, -1, best));
329 349
330 XPUSHs (sv); 350 XPUSHs (sv);
331 } 351 }
332 else if (SvPOKp (sv) && IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI)) 352 else if (SvPOKp (sv) && IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI))
333 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, MAGIC_U))); /* need to prefix only */ 353 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, MAGIC_U, best))); /* need to prefix only */
334 else if (ix == 2) /* compress always */ 354 else if (ix == 2) /* compress always */
335 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, -1))); 355 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, -1, best)));
336 else if (SvNIOK (sv)) /* don't compress */ 356 else if (SvNIOK (sv)) /* don't compress */
337 { 357 {
338 STRLEN len; 358 STRLEN len;
339 char *s = SvPV (sv, len); 359 char *s = SvPV (sv, len);
340 XPUSHs (sv_2mortal (newSVpvn (s, len))); 360 XPUSHs (sv_2mortal (newSVpvn (s, len)));
341 } 361 }
342 else /* don't compress */ 362 else /* don't compress */
343 XPUSHs (sv_2mortal (newSVsv (sv))); 363 XPUSHs (sv_2mortal (newSVsv (sv)));
364}
344 365
345void 366void
346sthaw(sv) 367sthaw(sv)
347 SV * sv 368 SV * sv
348 PROTOTYPE: $ 369 PROTOTYPE: $
442 SvPVX (sv)[0] = MAGIC_R_deref; 463 SvPVX (sv)[0] = MAGIC_R_deref;
443 } 464 }
444 else 465 else
445 SETs (sv_2mortal (newSVsv (TOPs))); 466 SETs (sv_2mortal (newSVsv (TOPs)));
446 467
447 break; 468 break;
448 469
449 default: 470 default:
450 croak ("Compress::LZF::sthaw(): invalid data, maybe you need a newer version of Compress::LZF?"); 471 croak ("Compress::LZF::sthaw(): invalid data, maybe you need a newer version of Compress::LZF?");
451 } 472 }
452 } 473 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines