ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Compress-LZV1/LZV1.xs
Revision: 1.1
Committed: Thu Sep 27 18:35:26 2001 UTC (22 years, 10 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 /* try to be compatible with older perls */
6 /* SvPV_nolen() macro first defined in 5.005_55 */
7 /* this is slow, not threadsafe, but works */
8 #include "patchlevel.h"
9 #if (PATCHLEVEL == 4) || ((PATCHLEVEL == 5) && (SUBVERSION < 55))
10 static STRLEN nolen_na;
11 # define SvPV_nolen(sv) SvPV ((sv), nolen_na)
12 #endif
13
14 #include "lzv1/lzv1.c"
15
16 MODULE = Compress::LZV1 PACKAGE = Compress::LZV1
17
18 SV *
19 compress(data)
20 SV * data
21 PROTOTYPE: $
22 CODE:
23 {
24 STRLEN usize, csize;
25 void *src = SvPV(data, usize);
26 unsigned char *dst;
27 unsigned short heap[HSIZ]; /* need not be initialized */
28
29 if (usize)
30 {
31 RETVAL = NEWSV (0, usize + 1);
32 SvPOK_only (RETVAL);
33 dst = (unsigned char *)SvPV_nolen (RETVAL);
34
35 /* compress */
36 csize = wLZV1 ((uch *)src, (uch *)(dst + 4), heap, usize, usize - 5);
37 if (csize)
38 {
39 dst[0] = 'L'; /* compressed flag */
40 dst[1] = usize >> 16;
41 dst[2] = usize >> 8;
42 dst[3] = usize >> 0;
43
44 SvCUR_set (RETVAL, csize + 4);
45 }
46 else
47 {
48 dst[0] = 'U';
49 Move ((void *)src, (void *)(dst + 1), usize, unsigned char);
50 SvCUR_set (RETVAL, usize + 1);
51 }
52 }
53 else
54 RETVAL = newSVpv ("", 0);
55 }
56 OUTPUT:
57 RETVAL
58
59 SV *
60 decompress(data)
61 SV * data
62 PROTOTYPE: $
63 CODE:
64 {
65 STRLEN usize, csize;
66 unsigned char *src = (unsigned char *)SvPV(data, csize);
67 void *dst;
68
69 if (csize)
70 {
71 switch (src[0]) {
72 case 'U':
73 usize = csize - 1;
74 RETVAL = NEWSV (0, usize);
75 SvPOK_only (RETVAL);
76 dst = SvPV_nolen (RETVAL);
77
78 Move ((void *)(src + 1), (void *)dst, usize, unsigned char);
79 break;
80 case 'L':
81 usize = (src[1] << 16)
82 | (src[2] << 8)
83 | (src[3] << 0);
84 RETVAL = NEWSV (0, usize);
85 SvPOK_only (RETVAL);
86 dst = SvPV_nolen (RETVAL);
87
88 if (rLZV1 ((uch *)(src + 4), (uch *)dst, csize - 4, usize) != usize)
89 croak ("LZV1: compressed data corrupted (2)");
90 break;
91 default:
92 croak ("LZV1: compressed data corrupted (1)");
93 }
94
95 SvCUR_set (RETVAL, usize);
96 }
97 else
98 RETVAL = newSVpvn ("", 0);
99
100 }
101 OUTPUT:
102 RETVAL
103