]>
Commit | Line | Data |
---|---|---|
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> | |
9 | #include <common/dictionary.h> | |
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; | |
30 | if(!BrotliDecompressBuffer(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){ | |
31 | croak("Error in BrotliDecompressBuffer"); | |
32 | } | |
33 | RETVAL = newSV(0); | |
34 | sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL); | |
35 | OUTPUT: | |
36 | RETVAL | |
37 | ||
38 | SV* BrotliCreateState() | |
39 | CODE: | |
40 | RETVAL = newSViv((IV)BrotliCreateState(NULL, NULL, NULL)); | |
41 | OUTPUT: | |
42 | RETVAL | |
43 | ||
44 | void BrotliDestroyState(state) | |
45 | SV* state | |
46 | CODE: | |
47 | BrotliDestroyState((BrotliState*)SvIV(state)); | |
48 | ||
49 | SV* BrotliDecompressStream(state, in) | |
50 | SV* state | |
51 | SV* in | |
52 | PREINIT: | |
53 | uint8_t *next_in, *next_out; | |
54 | size_t available_in, available_out, total_out; | |
55 | BrotliResult result; | |
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; | |
63 | result = BrotliDecompressStream(&available_in, (const uint8_t**) &next_in, &available_out, &next_out, &total_out, (BrotliState*) SvIV(state)); | |
64 | if(!result){ | |
65 | croak("Error in BrotliDecompressStream"); | |
66 | } | |
67 | sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out); | |
68 | } | |
69 | OUTPUT: | |
70 | RETVAL | |
71 | ||
72 | void BrotliSetCustomDictionary(state, dict) | |
73 | SV* state | |
74 | SV* dict | |
75 | PREINIT: | |
76 | size_t size; | |
77 | uint8_t *data; | |
78 | CODE: | |
79 | data = SvPV(dict, size); | |
80 | BrotliSetCustomDictionary(size, data, (BrotliState*) SvIV(state)); |