File: Exec.xs

package info (click to toggle)
libhipi-perl 0.93-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,048 kB
  • sloc: perl: 471,917; ansic: 22; makefile: 10
file content (75 lines) | stat: -rw-r--r-- 2,394 bytes parent folder | download | duplicates (2)
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
///////////////////////////////////////////////////////////////////////////////////////
// File          Exec.xs
// Description:  XS module for HiPi::Utils::Exec
// Copyright:    Copyright (c) 2013-2017 Mark Dootson
// License:      This is free software; you can redistribute it and/or modify it under
//               the same terms as the Perl 5 programming language system itself.
///////////////////////////////////////////////////////////////////////////////////////

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

#define NEED_sv_2pv_nolen
#include "mylib/include/ppport.h"

#include "zlib.h"

MODULE=HiPi::Utils::Exec PACKAGE=HiPi::Utils::Exec

PROTOTYPES:  DISABLE

void
_compress_buffer(SV* inputsv)
  PPCODE:
    unsigned char* outputbuffer;
    SV* output = sv_newmortal();
    unsigned inputsize = SvCUR(inputsv);
    unsigned char* inputbuffer = (unsigned char *)SvPVX(inputsv);
    unsigned outputsize = inputsize + (inputsize * 0.1) + 12;
    
   /* allocate the output buffers */
    SvUPGRADE(output, SVt_PV);
    outputbuffer = (unsigned char *)SvGROW(output, outputsize + sizeof(unsigned char) );
    
   /* do the compression */
    uLongf compressedsize = (uLongf)(outputsize + sizeof(unsigned char));
    compress((Bytef*)outputbuffer, (uLongf*)&compressedsize,(const Bytef*)inputbuffer, (uLongf)inputsize);
    
   /* fixup output */
    SvCUR_set(output, compressedsize);
    *SvEND(output) = '\0';
    (void) SvPOK_only(output);
    
  /* return SVs */
    EXTEND(SP, 2);
    PUSHs(output);
    PUSHs(sv_2mortal(newSViv(inputsize)));
    PUSHs(sv_2mortal(newSViv(compressedsize)));
  

void
_decompress_buffer(SV* inputsv, unsigned outputsize)
  PPCODE:
    unsigned char* outputbuffer;
    SV* output = sv_newmortal();
    unsigned inputsize = SvCUR(inputsv);
    unsigned char* inputbuffer = (unsigned char *)SvPVX(inputsv);
    
  /* allocate the output buffers */
    SvUPGRADE(output, SVt_PV);
    outputbuffer = (unsigned char *)SvGROW(output, outputsize + sizeof(unsigned char) );
    
  /* do the decompression */
    uLongf uncompressedsize = (uLongf)outputsize;
    uncompress((Bytef*)outputbuffer, &uncompressedsize, (const Bytef*)inputbuffer, (uLongf)inputsize);
    
   /* fixup output */
    SvCUR_set(output, outputsize);
    *SvEND(output) = '\0';
    (void) SvPOK_only(output);
    
  /* return SVs */
    EXTEND(SP, 1);
    PUSHs(output);