#include "ppport.h"
-#include <dec/decode.h>
-#include <enc/encode.h>
-#include <common/dictionary.h>
+#include <brotli/decode.h>
+#include <brotli/encode.h>
#define BUFFER_SIZE 1048576
PROTOTYPES: ENABLE
SV*
-unbro(buffer)
+unbro(buffer, decoded_size)
SV* buffer
+ size_t decoded_size
PREINIT:
- size_t decoded_size;
STRLEN encoded_size;
uint8_t *encoded_buffer, *decoded_buffer;
CODE:
encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size);
- if(!BrotliDecompressedSize(encoded_size, encoded_buffer, &decoded_size)){
- croak("Error in BrotliDecompressedSize");
- }
- Newx(decoded_buffer, decoded_size+1, uint8_t);
- decoded_buffer[decoded_size]=0;
+ Newx(decoded_buffer, decoded_size, uint8_t);
if(!BrotliDecoderDecompress(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){
croak("Error in BrotliDecoderDecompress");
}
RETVAL = newSV(0);
- sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL);
+ sv_usepvn(RETVAL, decoded_buffer, decoded_size);
OUTPUT:
RETVAL
next_in = (uint8_t*) SvPV(in, available_in);
Newx(buffer, BUFFER_SIZE, uint8_t);
RETVAL = newSVpv("", 0);
- result = BROTLI_RESULT_NEEDS_MORE_OUTPUT;
- while(result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) {
+ result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
+ while(result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
next_out = buffer;
available_out=BUFFER_SIZE;
result = BrotliDecoderDecompressStream( self->decoder,
if( quality < BROTLI_MIN_QUALITY || quality > BROTLI_MAX_QUALITY ) {
croak("Invalid quality value");
}
- if( lgwin < kBrotliMinWindowBits || lgwin > kBrotliMaxWindowBits ) {
+ if( lgwin < BROTLI_MIN_WINDOW_BITS || lgwin > BROTLI_MAX_WINDOW_BITS ) {
croak("Invalid window value");
}
decoded_buffer = (uint8_t*) SvPV(buffer, decoded_size);
croak("BrotliEncoderSetParameter may not be called directly");
break;
case 1:
- if( value < kBrotliMinWindowBits || value > kBrotliMaxWindowBits ) {
+ if( value < BROTLI_MIN_WINDOW_BITS || value > BROTLI_MAX_WINDOW_BITS ) {
croak("Invalid window value");
}
param = BROTLI_PARAM_LGWIN;
use IO::Uncompress::Brotli;
- # uncompress a buffer
- my $decoded = unbro $encoded;
+ # uncompress a buffer (yielding at most 10MB)
+ my $decoded = unbro $encoded, 10_000_000;
# uncompress a stream
my $bro = IO::Uncompress::Brotli->create;
=over
-=item B<unbro>(I<$input>)
+=item B<unbro>(I<$input>, I<$maximum_decoded_size>)
Takes a whole compressed buffer as input and returns the decompressed
-data. This function relies on the BrotliDecompressedSize function. In
-other words, it only works if the buffer has a single meta block or
-two meta-blocks where the first is uncompressed and the second is
-empty.
+data. It allocates a buffer of size I<$maximum_decoded_size> to store
+the decompressed data, if this is not sufficient (or there is another
+error) this function will croak.
Exported by default.
use v5.14;
use warnings;
-use Test::More tests => 80;
+use Test::More tests => 84;
use File::Slurp;
use IO::Uncompress::Brotli;
-my $todo_re = qr/empty\.compressed\.(?:1[7-9]|2)|x\.compressed\.0[12]/;
-
for my $test (<brotli/tests/testdata/*.compressed*>) {
my ($expected) = $test =~ s/\.compressed.*//r;
$expected = read_file $expected;
- if($test !~ $todo_re) {
- my $decoded = unbro (scalar read_file $test);
- is $decoded, $expected, "$test";
- }
+ my $decoded = unbro ((scalar read_file $test), 1_000_000);
+ is $decoded, $expected, "$test";
open FH, '<', $test;
my $unbro = IO::Uncompress::Brotli->create;