Use ALIAS: for setting encoder parameters too
[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 #include <enc/encode.h>
10 #include <common/dictionary.h>
11
12 #define BUFFER_SIZE 1048576
13
14 typedef struct brotli_decoder {
15 BrotliDecoderState *decoder;
16 }* IO__Uncompress__Brotli;
17
18 typedef struct brotli_encoder {
19 BrotliEncoderState *encoder;
20 }* IO__Compress__Brotli;
21
22
23 MODULE = IO::Compress::Brotli PACKAGE = IO::Uncompress::Brotli
24 PROTOTYPES: ENABLE
25
26 SV*
27 unbro(buffer)
28 SV* buffer
29 PREINIT:
30 size_t decoded_size;
31 STRLEN encoded_size;
32 uint8_t *encoded_buffer, *decoded_buffer;
33 CODE:
34 encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size);
35 if(!BrotliDecompressedSize(encoded_size, encoded_buffer, &decoded_size)){
36 croak("Error in BrotliDecompressedSize");
37 }
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");
42 }
43 RETVAL = newSV(0);
44 sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL);
45 OUTPUT:
46 RETVAL
47
48 IO::Uncompress::Brotli
49 create(class)
50 SV* class
51 CODE:
52 Newx(RETVAL, 1, struct brotli_decoder);
53 RETVAL->decoder = BrotliDecoderCreateInstance(NULL, NULL, NULL);
54 OUTPUT:
55 RETVAL
56
57 void
58 DESTROY(self)
59 IO::Uncompress::Brotli self
60 CODE:
61 BrotliDecoderDestroyInstance(self->decoder);
62 Safefree(self);
63
64 SV*
65 decompress(self, in)
66 IO::Uncompress::Brotli self
67 SV* in
68 PREINIT:
69 uint8_t *next_in, *next_out, *buffer;
70 size_t available_in, available_out;
71 BrotliDecoderResult result;
72 CODE:
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) {
78 next_out = buffer;
79 available_out=BUFFER_SIZE;
80 result = BrotliDecoderDecompressStream( self->decoder,
81 &available_in,
82 (const uint8_t**) &next_in,
83 &available_out,
84 &next_out,
85 NULL );
86 if(!result){
87 Safefree(buffer);
88 croak("Error in BrotliDecoderDecompressStream");
89 }
90 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
91 }
92 Safefree(buffer);
93 OUTPUT:
94 RETVAL
95
96 void
97 set_dictionary(self, dict)
98 IO::Uncompress::Brotli self
99 SV* dict
100 PREINIT:
101 size_t size;
102 uint8_t *data;
103 CODE:
104 data = SvPV(dict, size);
105 BrotliDecoderSetCustomDictionary(self->decoder, size, data);
106
107
108 MODULE = IO::Compress::Brotli PACKAGE = IO::Compress::Brotli
109 PROTOTYPES: ENABLE
110
111 SV*
112 bro(buffer, quality=BROTLI_DEFAULT_QUALITY, lgwin=BROTLI_DEFAULT_WINDOW)
113 SV* buffer
114 U32 quality
115 U32 lgwin
116 PREINIT:
117 size_t encoded_size;
118 STRLEN decoded_size;
119 uint8_t *encoded_buffer, *decoded_buffer;
120 BROTLI_BOOL result;
121 CODE:
122 if( quality < BROTLI_MIN_QUALITY || quality > BROTLI_MAX_QUALITY ) {
123 croak("Invalid quality value");
124 }
125 if( lgwin < kBrotliMinWindowBits || lgwin > kBrotliMaxWindowBits ) {
126 croak("Invalid window value");
127 }
128 decoded_buffer = (uint8_t*) SvPV(buffer, decoded_size);
129 encoded_size = BrotliEncoderMaxCompressedSize(decoded_size);
130 if(!encoded_size){
131 croak("Compressed size overflow");
132 }
133 Newx(encoded_buffer, encoded_size+1, uint8_t);
134 result = BrotliEncoderCompress( quality,
135 lgwin,
136 BROTLI_DEFAULT_MODE,
137 decoded_size,
138 decoded_buffer,
139 &encoded_size,
140 encoded_buffer );
141 if(!result){
142 Safefree(buffer);
143 croak("Error in BrotliEncoderCompress");
144 }
145 encoded_buffer[encoded_size]=0;
146 RETVAL = newSV(0);
147 sv_usepvn_flags(RETVAL, encoded_buffer, encoded_size, SV_SMAGIC | SV_HAS_TRAILING_NUL);
148 OUTPUT:
149 RETVAL
150
151 IO::Compress::Brotli
152 create(class)
153 SV* class
154 CODE:
155 Newx(RETVAL, 1, struct brotli_encoder);
156 RETVAL->encoder = BrotliEncoderCreateInstance(NULL, NULL, NULL);
157 OUTPUT:
158 RETVAL
159
160 bool BrotliEncoderSetParameter(self, value)
161 IO::Compress::Brotli self
162 U32 value
163 ALIAS:
164 window = 1
165 quality = 2
166 _mode = 3
167 PREINIT:
168 BrotliEncoderParameter param;
169 INIT:
170 switch(ix){
171 case 0:
172 croak("BrotliEncoderSetParameter may not be called directly");
173 break;
174 case 1:
175 if( value < kBrotliMinWindowBits || value > kBrotliMaxWindowBits ) {
176 croak("Invalid window value");
177 }
178 param = BROTLI_PARAM_LGWIN;
179 break;
180 case 2:
181 if( value < BROTLI_MIN_QUALITY || value > BROTLI_MAX_QUALITY ) {
182 croak("Invalid quality value");
183 }
184 param = BROTLI_PARAM_QUALITY;
185 break;
186 case 3:
187 /* Validation done on Perl side */
188 param = BROTLI_PARAM_MODE;
189 break;
190 default:
191 croak("Impossible ix in BrotliEncoderSetParameter");
192 break;
193 }
194 C_ARGS:
195 self->encoder, param, value
196
197 SV*
198 _compress(self, in = &PL_sv_undef)
199 IO::Compress::Brotli self
200 SV* in
201 ALIAS:
202 compress = 1
203 flush = 2
204 finish = 3
205 PREINIT:
206 uint8_t *next_in, *next_out, *buffer;
207 size_t available_in, available_out;
208 BROTLI_BOOL result;
209 BrotliEncoderOperation op;
210 CODE:
211 switch(ix) {
212 case 0:
213 croak("_compress may not be called directly");
214 break;
215 case 1:
216 op = BROTLI_OPERATION_PROCESS;
217 break;
218 case 2:
219 op = BROTLI_OPERATION_FLUSH;
220 break;
221 case 3:
222 op = BROTLI_OPERATION_FINISH;
223 break;
224 default:
225 croak("Impossible ix in _compress");
226 break;
227 }
228
229 Newx(buffer, BUFFER_SIZE, uint8_t);
230 if(in == &PL_sv_undef)
231 next_in = (uint8_t*) buffer, available_in = 0;
232 else
233 next_in = (uint8_t*) SvPV(in, available_in);
234 RETVAL = newSVpv("", 0);
235 while(1) {
236 next_out = buffer;
237 available_out = BUFFER_SIZE;
238 result = BrotliEncoderCompressStream( self->encoder,
239 (BrotliEncoderOperation) op,
240 &available_in,
241 (const uint8_t**) &next_in,
242 &available_out,
243 &next_out,
244 NULL );
245 if(!result) {
246 Safefree(buffer);
247 croak("Error in BrotliEncoderCompressStream");
248 }
249
250 if( available_out != BUFFER_SIZE ) {
251 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
252 }
253
254 if(
255 BrotliEncoderIsFinished(self->encoder) ||
256 (!available_in && !BrotliEncoderHasMoreOutput(self->encoder))
257 ) break;
258 }
259 Safefree(buffer);
260 OUTPUT:
261 RETVAL
262
263 void
264 DESTROY(self)
265 IO::Compress::Brotli self
266 CODE:
267 BrotliEncoderDestroyInstance(self->encoder);
268 Safefree(self);
269
270 void
271 set_dictionary(self, dict)
272 IO::Compress::Brotli self
273 SV* dict
274 PREINIT:
275 size_t size;
276 uint8_t *data;
277 CODE:
278 data = SvPV(dict, size);
279 BrotliEncoderSetCustomDictionary(self->encoder, size, data);
This page took 0.029843 seconds and 5 git commands to generate.