1 #define PERL_NO_GET_CONTEXT
8 #include <dec/decode.h>
10 #define BUFFER_SIZE 1048576
11 static uint8_t buffer[BUFFER_SIZE]; /* It's almost 2016, is anyone still using ithreads? */
13 MODULE = IO::Compress::Brotli PACKAGE = IO::Uncompress::Brotli
21 uint8_t *encoded_buffer, *decoded_buffer;
23 encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size);
24 if(!BrotliDecompressedSize(encoded_size, encoded_buffer, &decoded_size)){
25 croak("Error in BrotliDecompressedSize");
27 Newx(decoded_buffer, decoded_size+1, uint8_t);
28 decoded_buffer[decoded_size]=0;
29 if(!BrotliDecompressBuffer(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){
30 croak("Error in BrotliDecompressBuffer");
33 sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL);
37 SV* BrotliCreateState()
39 RETVAL = newSViv((IV)BrotliCreateState(NULL, NULL, NULL));
43 void BrotliDestroyState(state)
46 BrotliDestroyState((BrotliState*)SvIV(state));
48 SV* BrotliDecompressStream(state, in)
52 uint8_t *next_in, *next_out;
53 size_t available_in, available_out, total_out;
56 next_in = (uint8_t*) SvPV(in, available_in);
57 RETVAL = newSVpv("", 0);
58 result = BROTLI_RESULT_NEEDS_MORE_OUTPUT;
59 while(result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) {
61 available_out=BUFFER_SIZE;
62 result = BrotliDecompressStream(&available_in, (const uint8_t**) &next_in, &available_out, &next_out, &total_out, (BrotliState*) SvIV(state));
64 croak("Error in BrotliDecompressStream");
66 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
71 void BrotliSetCustomDictionary(state, dict)
78 data = SvPV(dict, size);
79 BrotliSetCustomDictionary(size, data, (BrotliState*) SvIV(state));