Initial commit
[io-compress-brotli.git] / Brotli.xs
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
10 #define BUFFER_SIZE 1048576
11 static uint8_t buffer[BUFFER_SIZE]; /* It's almost 2016, is anyone still using ithreads? */
12
13 MODULE = IO::Compress::Brotli PACKAGE = IO::Uncompress::Brotli
14 PROTOTYPES: ENABLE
15
16 SV* unbro(buffer)
17 SV* buffer
18 PREINIT:
19 size_t decoded_size;
20 STRLEN encoded_size;
21 uint8_t *encoded_buffer, *decoded_buffer;
22 CODE:
23 encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size);
24 if(!BrotliDecompressedSize(encoded_size, encoded_buffer, &decoded_size)){
25 croak("Error in BrotliDecompressedSize");
26 }
27 Newx(decoded_buffer, decoded_size+1, uint8_t);
28 decoded_buffer[decoded_size]=0;
29 if(!BrotliDecompressBuffer(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){
30 croak("Error in BrotliDecompressBuffer");
31 }
32 RETVAL = newSV(0);
33 sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL);
34 OUTPUT:
35 RETVAL
36
37 SV* BrotliCreateState()
38 CODE:
39 RETVAL = newSViv((IV)BrotliCreateState(NULL, NULL, NULL));
40 OUTPUT:
41 RETVAL
42
43 void BrotliDestroyState(state)
44 SV* state
45 CODE:
46 BrotliDestroyState((BrotliState*)SvIV(state));
47
48 SV* BrotliDecompressStream(state, in)
49 SV* state
50 SV* in
51 PREINIT:
52 uint8_t *next_in, *next_out;
53 size_t available_in, available_out, total_out;
54 BrotliResult result;
55 CODE:
56 next_in = (uint8_t*) SvPV(in, available_in);
57 RETVAL = newSVpv("", 0);
58 result = BROTLI_RESULT_NEEDS_MORE_OUTPUT;
59 while(result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) {
60 next_out = buffer;
61 available_out=BUFFER_SIZE;
62 result = BrotliDecompressStream(&available_in, (const uint8_t**) &next_in, &available_out, &next_out, &total_out, (BrotliState*) SvIV(state));
63 if(!result){
64 croak("Error in BrotliDecompressStream");
65 }
66 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
67 }
68 OUTPUT:
69 RETVAL
70
71 void BrotliSetCustomDictionary(state, dict)
72 SV* state
73 SV* dict
74 PREINIT:
75 size_t size;
76 uint8_t *data;
77 CODE:
78 data = SvPV(dict, size);
79 BrotliSetCustomDictionary(size, data, (BrotliState*) SvIV(state));
This page took 0.022919 seconds and 4 git commands to generate.