Commit | Line | Data |
---|---|---|
f9995f31 MG |
1 | #define PERL_NO_GET_CONTEXT |
2 | #include "EXTERN.h" | |
3 | #include "perl.h" | |
4 | #include "XSUB.h" | |
5 | ||
6 | #include "ppport.h" | |
7 | ||
8 | #include <dec/decode.h> | |
fd0f6b13 | 9 | #include <common/dictionary.h> |
f9995f31 MG |
10 | |
11 | #define BUFFER_SIZE 1048576 | |
12 | static uint8_t buffer[BUFFER_SIZE]; /* It's almost 2016, is anyone still using ithreads? */ | |
13 | ||
14 | MODULE = IO::Compress::Brotli PACKAGE = IO::Uncompress::Brotli | |
15 | PROTOTYPES: ENABLE | |
16 | ||
17 | SV* unbro(buffer) | |
18 | SV* buffer | |
19 | PREINIT: | |
20 | size_t decoded_size; | |
21 | STRLEN encoded_size; | |
22 | uint8_t *encoded_buffer, *decoded_buffer; | |
23 | CODE: | |
24 | encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size); | |
25 | if(!BrotliDecompressedSize(encoded_size, encoded_buffer, &decoded_size)){ | |
26 | croak("Error in BrotliDecompressedSize"); | |
27 | } | |
28 | Newx(decoded_buffer, decoded_size+1, uint8_t); | |
29 | decoded_buffer[decoded_size]=0; | |
ea94b872 QR |
30 | if(!BrotliDecoderDecompress(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){ |
31 | croak("Error in BrotliDecoderDecompress"); | |
f9995f31 MG |
32 | } |
33 | RETVAL = newSV(0); | |
34 | sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL); | |
35 | OUTPUT: | |
36 | RETVAL | |
37 | ||
ea94b872 | 38 | SV* BrotliDecoderCreateInstance() |
f9995f31 | 39 | CODE: |
ea94b872 | 40 | RETVAL = newSViv((IV)BrotliDecoderCreateInstance(NULL, NULL, NULL)); |
f9995f31 MG |
41 | OUTPUT: |
42 | RETVAL | |
43 | ||
ea94b872 | 44 | void BrotliDecoderDestroyInstance(state) |
f9995f31 MG |
45 | SV* state |
46 | CODE: | |
ea94b872 | 47 | BrotliDecoderDestroyInstance((BrotliDecoderState*)SvIV(state)); |
f9995f31 | 48 | |
ea94b872 | 49 | SV* BrotliDecoderDecompressStream(state, in) |
f9995f31 MG |
50 | SV* state |
51 | SV* in | |
52 | PREINIT: | |
53 | uint8_t *next_in, *next_out; | |
54 | size_t available_in, available_out, total_out; | |
ea94b872 | 55 | BrotliDecoderResult result; |
f9995f31 MG |
56 | CODE: |
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) { | |
61 | next_out = buffer; | |
62 | available_out=BUFFER_SIZE; | |
ea94b872 | 63 | result = BrotliDecoderDecompressStream((BrotliDecoderState*) SvIV(state), &available_in, (const uint8_t**) &next_in, &available_out, &next_out, &total_out); |
f9995f31 | 64 | if(!result){ |
ea94b872 | 65 | croak("Error in BrotliDecoderDecompressStream"); |
f9995f31 MG |
66 | } |
67 | sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out); | |
68 | } | |
69 | OUTPUT: | |
70 | RETVAL | |
71 | ||
5f975f9c | 72 | void BrotliDecoderSetCustomDictionary(state, dict) |
f9995f31 MG |
73 | SV* state |
74 | SV* dict | |
75 | PREINIT: | |
76 | size_t size; | |
77 | uint8_t *data; | |
78 | CODE: | |
79 | data = SvPV(dict, size); | |
ea94b872 | 80 | BrotliDecoderSetCustomDictionary((BrotliDecoderState*) SvIV(state), size, data); |