1 #define PERL_NO_GET_CONTEXT
8 #include <dec/decode.h>
9 #include <enc/encode.h>
10 #include <common/dictionary.h>
12 #define BUFFER_SIZE 1048576
14 typedef struct brotli_decoder {
15 BrotliDecoderState *decoder;
16 }* IO__Uncompress__Brotli;
18 typedef struct brotli_encoder {
19 BrotliEncoderState *encoder;
20 }* IO__Compress__Brotli;
23 MODULE = IO::Compress::Brotli PACKAGE = IO::Uncompress::Brotli
32 uint8_t *encoded_buffer, *decoded_buffer;
34 encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size);
35 if(!BrotliDecompressedSize(encoded_size, encoded_buffer, &decoded_size)){
36 croak("Error in BrotliDecompressedSize");
38 Newx(decoded_buffer, decoded_size+1, uint8_t);
39 decoded_buffer[decoded_size]=0;
40 if(!BrotliDecoderDecompress(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){
41 croak("Error in BrotliDecoderDecompress");
44 sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL);
48 IO::Uncompress::Brotli
52 Newx(RETVAL, 1, struct brotli_decoder);
53 RETVAL->decoder = BrotliDecoderCreateInstance(NULL, NULL, NULL);
59 IO::Uncompress::Brotli self
61 BrotliDecoderDestroyInstance(self->decoder);
66 IO::Uncompress::Brotli self
69 uint8_t *next_in, *next_out, *buffer;
70 size_t available_in, available_out;
71 BrotliDecoderResult result;
73 next_in = (uint8_t*) SvPV(in, available_in);
74 Newx(buffer, BUFFER_SIZE, uint8_t);
75 RETVAL = newSVpv("", 0);
76 result = BROTLI_RESULT_NEEDS_MORE_OUTPUT;
77 while(result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) {
79 available_out=BUFFER_SIZE;
80 result = BrotliDecoderDecompressStream( self->decoder,
82 (const uint8_t**) &next_in,
88 croak("Error in BrotliDecoderDecompressStream");
90 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
97 set_dictionary(self, dict)
98 IO::Uncompress::Brotli self
104 data = SvPV(dict, size);
105 BrotliDecoderSetCustomDictionary(self->decoder, size, data);
108 MODULE = IO::Compress::Brotli PACKAGE = IO::Compress::Brotli
112 bro(buffer, quality=BROTLI_DEFAULT_QUALITY, lgwin=BROTLI_DEFAULT_WINDOW)
119 uint8_t *encoded_buffer, *decoded_buffer;
122 if( quality < BROTLI_MIN_QUALITY || quality > BROTLI_MAX_QUALITY ) {
123 croak("Invalid quality value");
125 if( lgwin < kBrotliMinWindowBits || lgwin > kBrotliMaxWindowBits ) {
126 croak("Invalid window value");
128 decoded_buffer = (uint8_t*) SvPV(buffer, decoded_size);
129 encoded_size = BrotliEncoderMaxCompressedSize(decoded_size);
131 croak("Compressed size overflow");
133 Newx(encoded_buffer, encoded_size+1, uint8_t);
134 result = BrotliEncoderCompress( quality,
143 croak("Error in BrotliEncoderCompress");
145 encoded_buffer[encoded_size]=0;
147 sv_usepvn_flags(RETVAL, encoded_buffer, encoded_size, SV_SMAGIC | SV_HAS_TRAILING_NUL);
155 Newx(RETVAL, 1, struct brotli_encoder);
156 RETVAL->encoder = BrotliEncoderCreateInstance(NULL, NULL, NULL);
160 bool BrotliEncoderSetParameter(self, value)
161 IO::Compress::Brotli self
168 BrotliEncoderParameter param;
172 croak("BrotliEncoderSetParameter may not be called directly");
175 if( value < kBrotliMinWindowBits || value > kBrotliMaxWindowBits ) {
176 croak("Invalid window value");
178 param = BROTLI_PARAM_LGWIN;
181 if( value < BROTLI_MIN_QUALITY || value > BROTLI_MAX_QUALITY ) {
182 croak("Invalid quality value");
184 param = BROTLI_PARAM_QUALITY;
187 /* Validation done on Perl side */
188 param = BROTLI_PARAM_MODE;
191 croak("Impossible ix in BrotliEncoderSetParameter");
195 self->encoder, param, value
198 _compress(self, in = &PL_sv_undef)
199 IO::Compress::Brotli self
206 uint8_t *next_in, *next_out, *buffer;
207 size_t available_in, available_out;
209 BrotliEncoderOperation op;
213 croak("_compress may not be called directly");
216 op = BROTLI_OPERATION_PROCESS;
219 op = BROTLI_OPERATION_FLUSH;
222 op = BROTLI_OPERATION_FINISH;
225 croak("Impossible ix in _compress");
229 Newx(buffer, BUFFER_SIZE, uint8_t);
230 if(in == &PL_sv_undef)
231 next_in = (uint8_t*) buffer, available_in = 0;
233 next_in = (uint8_t*) SvPV(in, available_in);
234 RETVAL = newSVpv("", 0);
237 available_out = BUFFER_SIZE;
238 result = BrotliEncoderCompressStream( self->encoder,
239 (BrotliEncoderOperation) op,
241 (const uint8_t**) &next_in,
247 croak("Error in BrotliEncoderCompressStream");
250 if( available_out != BUFFER_SIZE ) {
251 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
255 BrotliEncoderIsFinished(self->encoder) ||
256 (!available_in && !BrotliEncoderHasMoreOutput(self->encoder))
265 IO::Compress::Brotli self
267 BrotliEncoderDestroyInstance(self->encoder);
271 set_dictionary(self, dict)
272 IO::Compress::Brotli self
278 data = SvPV(dict, size);
279 BrotliEncoderSetCustomDictionary(self->encoder, size, data);