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.29 by root, Wed Dec 17 00:36:09 2008 UTC vs.
Revision 1.34 by root, Mon Jun 29 23:51:28 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 void *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
41 if (usize) 44 if (usize)
42 { 45 {
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);
90 dst[skip++] = (( usize & 0x3f) | 0x80); 93 dst[skip++] = (( usize & 0x3f) | 0x80);
91 } 94 }
92 else 95 else
93 croak ("compress can only compress up to %ld bytes", 0x7fffffffL); 96 croak ("compress can only compress up to %ld bytes", 0x7fffffffL);
94 97
95 New (0, state, 1, LZF_STATE); 98 if (usize > 2000) perlinterp_release ();
99
100 state = malloc (best ? sizeof (LZF_STATE_BEST) : sizeof (LZF_STATE));
96 if (!state) 101 if (!state)
102 {
103 if (usize > 2000) perlinterp_acquire ();
97 croak ("Compress::LZF unable to allocate memory for compression state"); 104 croak ("Compress::LZF unable to allocate memory for compression state");
105 }
98 106
99 /* 11 bytes is the smallest compressible string */ 107 /* 11 bytes is the smallest compressible string */
100 csize = usize < 11 ? 0 : 108 csize = usize < 11 ? 0 :
109 (best ? lzf_compress_best (src, usize, dst + skip, usize - skip, *(LZF_STATE_BEST *)state)
101 lzf_compress (src, usize, dst + skip, usize - skip, *state); 110 : lzf_compress (src, usize, dst + skip, usize - skip, *(LZF_STATE *)state));
102 111
103 Safefree (state); 112 free (state);
113
114 if (usize > 2000) perlinterp_acquire ();
104 115
105 if (csize) 116 if (csize)
106 { 117 {
107 SvCUR_set (ret, csize + skip); 118 SvCUR_set (ret, csize + skip);
108 } 119 }
134 145
135 if (csize) 146 if (csize)
136 { 147 {
137 void *dst; 148 void *dst;
138 SV *ret; 149 SV *ret;
150 int res;
139 151
140 csize -= skip; 152 csize -= skip;
141 153
142 if (src[0]) 154 if (src[0])
143 { 155 {
189 else 201 else
190 croak ("compressed data corrupted (invalid length)"); 202 croak ("compressed data corrupted (invalid length)");
191 203
192 if (!usize) 204 if (!usize)
193 croak ("compressed data corrupted (invalid length)"); 205 croak ("compressed data corrupted (invalid length)");
194 206
195 ret = NEWSV (0, usize); 207 ret = NEWSV (0, usize);
196 SvPOK_only (ret); 208 SvPOK_only (ret);
197 dst = SvPVX (ret); 209 dst = SvPVX (ret);
198 210
211 if (usize > 4000) perlinterp_release ();
199 if (lzf_decompress (src, csize, dst, usize) != usize) 212 res = lzf_decompress (src, csize, dst, usize) != usize;
213 if (usize > 4000) perlinterp_acquire ();
214
215 if (res)
200 { 216 {
201 SvREFCNT_dec (ret); 217 SvREFCNT_dec (ret);
202 croak ("compressed data corrupted (size mismatch)", csize, skip, usize); 218 croak ("compressed data corrupted (size mismatch)", csize, skip, usize);
203 } 219 }
204 } 220 }
220} 236}
221 237
222static void 238static void
223need_storable (void) 239need_storable (void)
224{ 240{
225 eval_sv (sv_2mortal (Perl_newSVpvf ("require %s", SvPVbyte_nolen (serializer_package))), G_VOID | G_DISCARD); 241 eval_sv (sv_2mortal (newSVpvf ("require %s", SvPVbyte_nolen (serializer_package))), G_VOID | G_DISCARD);
226 242
227 storable_mstore = (CV *)SvREFCNT_inc (GvCV (gv_fetchpv (SvPVbyte_nolen (serializer_mstore ), TRUE, SVt_PVCV))); 243 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))); 244 storable_mretrieve = (CV *)SvREFCNT_inc (GvCV (gv_fetchpv (SvPVbyte_nolen (serializer_mretrieve), TRUE, SVt_PVCV)));
229} 245}
230 246
249 SvREFCNT_dec (storable_mretrieve); storable_mretrieve = 0; 265 SvREFCNT_dec (storable_mretrieve); storable_mretrieve = 0;
250 266
251void 267void
252compress(data) 268compress(data)
253 SV * data 269 SV * data
270 ALIAS:
271 compress_best = 1
254 PROTOTYPE: $ 272 PROTOTYPE: $
255 PPCODE: 273 PPCODE:
256 XPUSHs (sv_2mortal (compress_sv (data, 0, MAGIC_U))); 274 XPUSHs (sv_2mortal (compress_sv (data, 0, MAGIC_U, ix)));
257 275
258void 276void
259decompress(data) 277decompress(data)
260 SV * data 278 SV * data
261 PROTOTYPE: $ 279 PROTOTYPE: $
264 282
265void 283void
266sfreeze(sv) 284sfreeze(sv)
267 SV * sv 285 SV * sv
268 ALIAS: 286 ALIAS:
287 sfreeze = 0
269 sfreeze_cr = 1 288 sfreeze_cr = 1
270 sfreeze_c = 2 289 sfreeze_c = 2
290 sfreeze_best = 4
291 sfreeze_cr_best = 5
292 sfreeze_c_best = 6
271 PROTOTYPE: $ 293 PROTOTYPE: $
272 PPCODE: 294 PPCODE:
295{
296 int best = ix & 4;
297 ix &= 3;
273 298
274 SvGETMAGIC (sv); 299 SvGETMAGIC (sv);
275 300
276 if (!SvOK (sv)) 301 if (!SvOK (sv))
277 XPUSHs (sv_2mortal (newSVpvn ("\02", 1))); /* 02 == MAGIC_undef */ 302 XPUSHs (sv_2mortal (newSVpvn ("\02", 1))); /* 02 == MAGIC_undef */
323 348
324 sv_insert (sv, 0, 0, pfx, 2); 349 sv_insert (sv, 0, 0, pfx, 2);
325 } 350 }
326 351
327 if (ix) /* compress */ 352 if (ix) /* compress */
328 sv = sv_2mortal (compress_sv (sv, deref ? MAGIC_CR_deref : MAGIC_CR, -1)); 353 sv = sv_2mortal (compress_sv (sv, deref ? MAGIC_CR_deref : MAGIC_CR, -1, best));
329 354
330 XPUSHs (sv); 355 XPUSHs (sv);
331 } 356 }
332 else if (SvPOKp (sv) && IN_RANGE (SvPVX (sv)[0], MAGIC_LO, MAGIC_HI)) 357 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 */ 358 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, MAGIC_U, best))); /* need to prefix only */
334 else if (ix == 2) /* compress always */ 359 else if (ix == 2) /* compress always */
335 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, -1))); 360 XPUSHs (sv_2mortal (compress_sv (sv, MAGIC_C, -1, best)));
336 else if (SvNIOK (sv)) /* don't compress */ 361 else if (SvNIOK (sv)) /* don't compress */
337 { 362 {
338 STRLEN len; 363 STRLEN len;
339 char *s = SvPV (sv, len); 364 char *s = SvPV (sv, len);
340 XPUSHs (sv_2mortal (newSVpvn (s, len))); 365 XPUSHs (sv_2mortal (newSVpvn (s, len)));
341 } 366 }
342 else /* don't compress */ 367 else /* don't compress */
343 XPUSHs (sv_2mortal (newSVsv (sv))); 368 XPUSHs (sv_2mortal (newSVsv (sv)));
369}
344 370
345void 371void
346sthaw(sv) 372sthaw(sv)
347 SV * sv 373 SV * sv
348 PROTOTYPE: $ 374 PROTOTYPE: $
442 SvPVX (sv)[0] = MAGIC_R_deref; 468 SvPVX (sv)[0] = MAGIC_R_deref;
443 } 469 }
444 else 470 else
445 SETs (sv_2mortal (newSVsv (TOPs))); 471 SETs (sv_2mortal (newSVsv (TOPs)));
446 472
447 break; 473 break;
448 474
449 default: 475 default:
450 croak ("Compress::LZF::sthaw(): invalid data, maybe you need a newer version of Compress::LZF?"); 476 croak ("Compress::LZF::sthaw(): invalid data, maybe you need a newer version of Compress::LZF?");
451 } 477 }
452 } 478 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines