1 #define PERL_NO_GET_CONTEXT
8 #include <dec/decode.h>
9 #include <common/dictionary.h>
11 #define BUFFER_SIZE 1048576
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(!BrotliDecoderDecompress(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){
30 croak("Error in BrotliDecoderDecompress");
33 sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL);
37 SV* BrotliDecoderCreateInstance()
39 RETVAL = newSViv((IV)BrotliDecoderCreateInstance(NULL, NULL, NULL));
43 void BrotliDecoderDestroyInstance(state)
46 BrotliDecoderDestroyInstance((BrotliDecoderState*)SvIV(state));
48 SV* BrotliDecoderDecompressStream(state, in)
52 uint8_t *next_in, *next_out, *buffer;
53 size_t available_in, available_out;
54 BrotliDecoderResult result;
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) {
62 available_out=BUFFER_SIZE;
63 result = BrotliDecoderDecompressStream( (BrotliDecoderState*) SvIV(state),
65 (const uint8_t**) &next_in,
71 croak("Error in BrotliDecoderDecompressStream");
73 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
79 void BrotliDecoderSetCustomDictionary(state, dict)
86 data = SvPV(dict, size);
87 BrotliDecoderSetCustomDictionary((BrotliDecoderState*) SvIV(state), size, data);