Break down the long call to BrotliDecoderDecompressStream
[io-compress-brotli.git] / Brotli.xs
CommitLineData
f9995f31
MG
1#define PERL_NO_GET_CONTEXT
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5
6#include "ppport.h"
7
8#include <dec/decode.h>
fd0f6b13 9#include <common/dictionary.h>
f9995f31
MG
10
11#define BUFFER_SIZE 1048576
12static uint8_t buffer[BUFFER_SIZE]; /* It's almost 2016, is anyone still using ithreads? */
13
14MODULE = IO::Compress::Brotli PACKAGE = IO::Uncompress::Brotli
15PROTOTYPES: ENABLE
16
17SV* unbro(buffer)
18 SV* buffer
19 PREINIT:
20 size_t decoded_size;
21 STRLEN encoded_size;
22 uint8_t *encoded_buffer, *decoded_buffer;
23 CODE:
24 encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size);
25 if(!BrotliDecompressedSize(encoded_size, encoded_buffer, &decoded_size)){
26 croak("Error in BrotliDecompressedSize");
27 }
28 Newx(decoded_buffer, decoded_size+1, uint8_t);
29 decoded_buffer[decoded_size]=0;
ea94b872
QR
30 if(!BrotliDecoderDecompress(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){
31 croak("Error in BrotliDecoderDecompress");
f9995f31
MG
32 }
33 RETVAL = newSV(0);
34 sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL);
35 OUTPUT:
36 RETVAL
37
ea94b872 38SV* BrotliDecoderCreateInstance()
f9995f31 39 CODE:
ea94b872 40 RETVAL = newSViv((IV)BrotliDecoderCreateInstance(NULL, NULL, NULL));
f9995f31
MG
41 OUTPUT:
42 RETVAL
43
ea94b872 44void BrotliDecoderDestroyInstance(state)
f9995f31
MG
45 SV* state
46 CODE:
ea94b872 47 BrotliDecoderDestroyInstance((BrotliDecoderState*)SvIV(state));
f9995f31 48
ea94b872 49SV* BrotliDecoderDecompressStream(state, in)
f9995f31
MG
50 SV* state
51 SV* in
52 PREINIT:
53 uint8_t *next_in, *next_out;
54 size_t available_in, available_out, total_out;
ea94b872 55 BrotliDecoderResult result;
f9995f31
MG
56 CODE:
57 next_in = (uint8_t*) SvPV(in, available_in);
58 RETVAL = newSVpv("", 0);
59 result = BROTLI_RESULT_NEEDS_MORE_OUTPUT;
60 while(result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) {
61 next_out = buffer;
62 available_out=BUFFER_SIZE;
782ccd86
QR
63 result = BrotliDecoderDecompressStream( (BrotliDecoderState*) SvIV(state),
64 &available_in,
65 (const uint8_t**) &next_in,
66 &available_out,
67 &next_out,
68 &total_out );
f9995f31 69 if(!result){
ea94b872 70 croak("Error in BrotliDecoderDecompressStream");
f9995f31
MG
71 }
72 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
73 }
74 OUTPUT:
75 RETVAL
76
5f975f9c 77void BrotliDecoderSetCustomDictionary(state, dict)
f9995f31
MG
78 SV* state
79 SV* dict
80 PREINIT:
81 size_t size;
82 uint8_t *data;
83 CODE:
84 data = SvPV(dict, size);
ea94b872 85 BrotliDecoderSetCustomDictionary((BrotliDecoderState*) SvIV(state), size, data);
This page took 0.013847 seconds and 4 git commands to generate.