File: Snappy.xs

package info (click to toggle)
libcompress-snappy-perl 0.24+ds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 240 kB
  • sloc: ansic: 3,120; perl: 106; makefile: 3
file content (82 lines) | stat: -rw-r--r-- 1,748 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#define PERL_NO_GET_CONTEXT

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#define NEED_sv_2pvbyte
#include "ppport.h"

#include <snappy-c.h>

MODULE = Compress::Snappy    PACKAGE = Compress::Snappy

PROTOTYPES: ENABLE

SV *
compress (sv)
    SV *sv
PREINIT:
    char *src, *dest;
    STRLEN src_len;
    size_t dest_len;
CODE:
    SvGETMAGIC(sv);
    if (SvROK(sv) && ! SvAMAGIC(sv)) {
        sv = SvRV(sv);
        SvGETMAGIC(sv);
    }
    if (! SvOK(sv))
        XSRETURN_NO;
    src = SvPVbyte(sv, src_len);
    if (! src_len)
        XSRETURN_NO;
    dest_len = snappy_max_compressed_length(src_len);
    if (! dest_len)
        XSRETURN_UNDEF;
    RETVAL = newSV(dest_len);
    dest = SvPVX(RETVAL);
    if (! dest)
        XSRETURN_UNDEF;
    snappy_compress(src, src_len, dest, &dest_len);
    SvCUR_set(RETVAL, dest_len);
    SvPOK_on(RETVAL);
OUTPUT:
    RETVAL

SV *
decompress (sv)
    SV *sv
ALIAS:
    uncompress = 1
PREINIT:
    char *src, *dest;
    STRLEN src_len;
    size_t dest_len;
    int header_len;
CODE:
    PERL_UNUSED_VAR(ix); /* -W */
    SvGETMAGIC(sv);
    if (SvROK(sv) && ! SvAMAGIC(sv)) {
        sv = SvRV(sv);
        SvGETMAGIC(sv);
    }
    if (! SvOK(sv))
        XSRETURN_NO;
    src = SvPVbyte(sv, src_len);
    if (! src_len)
        XSRETURN_NO;
    header_len = snappy_uncompressed_length(src, src_len, &dest_len);
    if (0 > header_len || ! dest_len)
        XSRETURN_UNDEF;
    RETVAL = newSV(dest_len);
    dest = SvPVX(RETVAL);
    if (! dest)
        XSRETURN_UNDEF;
    if (snappy_uncompress(src + header_len, src_len - header_len,
                                    dest, &dest_len))
        XSRETURN_UNDEF;
    SvCUR_set(RETVAL, dest_len);
    SvPOK_on(RETVAL);
OUTPUT:
    RETVAL