Use a local temporary buffer for decompression instead of a global.
[io-compress-brotli.git] / Brotli.xs
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>
9 #include <common/dictionary.h>
10
11 #define BUFFER_SIZE 1048576
12
13 MODULE = IO::Compress::Brotli PACKAGE = IO::Uncompress::Brotli
14 PROTOTYPES: ENABLE
15
16 SV* unbro(buffer)
17 SV* buffer
18 PREINIT:
19 size_t decoded_size;
20 STRLEN encoded_size;
21 uint8_t *encoded_buffer, *decoded_buffer;
22 CODE:
23 encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size);
24 if(!BrotliDecompressedSize(encoded_size, encoded_buffer, &decoded_size)){
25 croak("Error in BrotliDecompressedSize");
26 }
27 Newx(decoded_buffer, decoded_size+1, uint8_t);
28 decoded_buffer[decoded_size]=0;
29 if(!BrotliDecoderDecompress(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){
30 croak("Error in BrotliDecoderDecompress");
31 }
32 RETVAL = newSV(0);
33 sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL);
34 OUTPUT:
35 RETVAL
36
37 SV* BrotliDecoderCreateInstance()
38 CODE:
39 RETVAL = newSViv((IV)BrotliDecoderCreateInstance(NULL, NULL, NULL));
40 OUTPUT:
41 RETVAL
42
43 void BrotliDecoderDestroyInstance(state)
44 SV* state
45 CODE:
46 BrotliDecoderDestroyInstance((BrotliDecoderState*)SvIV(state));
47
48 SV* BrotliDecoderDecompressStream(state, in)
49 SV* state
50 SV* in
51 PREINIT:
52 uint8_t *next_in, *next_out, *buffer;
53 size_t available_in, available_out;
54 BrotliDecoderResult result;
55 CODE:
56 next_in = (uint8_t*) SvPV(in, available_in);
57 Newx(buffer, BUFFER_SIZE, uint8_t);
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;
63 result = BrotliDecoderDecompressStream( (BrotliDecoderState*) SvIV(state),
64 &available_in,
65 (const uint8_t**) &next_in,
66 &available_out,
67 &next_out,
68 NULL );
69 if(!result){
70 Safefree(buffer);
71 croak("Error in BrotliDecoderDecompressStream");
72 }
73 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
74 }
75 Safefree(buffer);
76 OUTPUT:
77 RETVAL
78
79 void BrotliDecoderSetCustomDictionary(state, dict)
80 SV* state
81 SV* dict
82 PREINIT:
83 size_t size;
84 uint8_t *data;
85 CODE:
86 data = SvPV(dict, size);
87 BrotliDecoderSetCustomDictionary((BrotliDecoderState*) SvIV(state), size, data);
This page took 0.026113 seconds and 5 git commands to generate.