1 #define PERL_NO_GET_CONTEXT
8 #include <dec/decode.h>
9 #include <common/dictionary.h>
11 #define BUFFER_SIZE 1048576
12 static uint8_t buffer[BUFFER_SIZE]; /* It's almost 2016, is anyone still using ithreads? */
14 MODULE = IO::Compress::Brotli PACKAGE = IO::Uncompress::Brotli
22 uint8_t *encoded_buffer, *decoded_buffer;
24 encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size);
25 if(!BrotliDecompressedSize(encoded_size, encoded_buffer, &decoded_size)){
26 croak("Error in BrotliDecompressedSize");
28 Newx(decoded_buffer, decoded_size+1, uint8_t);
29 decoded_buffer[decoded_size]=0;
30 if(!BrotliDecoderDecompress(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){
31 croak("Error in BrotliDecoderDecompress");
34 sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL);
38 SV* BrotliDecoderCreateInstance()
40 RETVAL = newSViv((IV)BrotliDecoderCreateInstance(NULL, NULL, NULL));
44 void BrotliDecoderDestroyInstance(state)
47 BrotliDecoderDestroyInstance((BrotliDecoderState*)SvIV(state));
49 SV* BrotliDecoderDecompressStream(state, in)
53 uint8_t *next_in, *next_out;
54 size_t available_in, available_out;
55 BrotliDecoderResult result;
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) {
62 available_out=BUFFER_SIZE;
63 result = BrotliDecoderDecompressStream( (BrotliDecoderState*) SvIV(state),
65 (const uint8_t**) &next_in,
70 croak("Error in BrotliDecoderDecompressStream");
72 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
77 void BrotliDecoderSetCustomDictionary(state, dict)
84 data = SvPV(dict, size);
85 BrotliDecoderSetCustomDictionary((BrotliDecoderState*) SvIV(state), size, data);