Don't leak memory on encoder DESTROY
[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 SV*
161 window(self, window)
162 IO::Compress::Brotli self
163 U32 window
164 CODE:
165 if( window < kBrotliMinWindowBits || window > kBrotliMaxWindowBits ) {
166 croak("Invalid window value");
167 }
168 if( BrotliEncoderSetParameter(self->encoder, BROTLI_PARAM_LGWIN, window) )
169 RETVAL = newSVuv(1);
170 else
171 RETVAL = newSVuv(0);
172 OUTPUT:
173 RETVAL
174
175 SV*
176 quality(self, quality)
177 IO::Compress::Brotli self
178 U32 quality
179 CODE:
180 if( quality < BROTLI_MIN_QUALITY || quality > BROTLI_MAX_QUALITY ) {
181 croak("Invalid quality value");
182 }
183 if( BrotliEncoderSetParameter(self->encoder, BROTLI_PARAM_QUALITY, quality) )
184 RETVAL = newSVuv(1);
185 else
186 RETVAL = newSVuv(0);
187 OUTPUT:
188 RETVAL
189
190 SV*
191 _mode(self, mode)
192 IO::Compress::Brotli self
193 U32 mode
194 CODE:
195 if( BrotliEncoderSetParameter(self->encoder, BROTLI_PARAM_MODE, mode) )
196 RETVAL = newSVuv(1);
197 else
198 RETVAL = newSVuv(0);
199 OUTPUT:
200 RETVAL
201
202 SV*
203 _compress(self, in = &PL_sv_undef)
204 IO::Compress::Brotli self
205 SV* in
206 ALIAS:
207 compress = 1
208 flush = 2
209 finish = 3
210 PREINIT:
211 uint8_t *next_in, *next_out, *buffer;
212 size_t available_in, available_out;
213 BROTLI_BOOL result;
214 BrotliEncoderOperation op;
215 CODE:
216 switch(ix) {
217 case 0:
218 croak("_compress may not be called directly");
219 break;
220 case 1:
221 op = BROTLI_OPERATION_PROCESS;
222 break;
223 case 2:
224 op = BROTLI_OPERATION_FLUSH;
225 break;
226 case 3:
227 op = BROTLI_OPERATION_FINISH;
228 break;
229 default:
230 croak("Impossible ix in _compress");
231 break;
232 }
233
234 Newx(buffer, BUFFER_SIZE, uint8_t);
235 if(in == &PL_sv_undef)
236 next_in = (uint8_t*) buffer, available_in = 0;
237 else
238 next_in = (uint8_t*) SvPV(in, available_in);
239 RETVAL = newSVpv("", 0);
240 while(1) {
241 next_out = buffer;
242 available_out = BUFFER_SIZE;
243 result = BrotliEncoderCompressStream( self->encoder,
244 (BrotliEncoderOperation) op,
245 &available_in,
246 (const uint8_t**) &next_in,
247 &available_out,
248 &next_out,
249 NULL );
250 if(!result) {
251 Safefree(buffer);
252 croak("Error in BrotliEncoderCompressStream");
253 }
254
255 if( available_out != BUFFER_SIZE ) {
256 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
257 }
258
259 if(
260 BrotliEncoderIsFinished(self->encoder) ||
261 (!available_in && !BrotliEncoderHasMoreOutput(self->encoder))
262 ) break;
263 }
264 Safefree(buffer);
265 OUTPUT:
266 RETVAL
267
268 void
269 DESTROY(self)
270 IO::Compress::Brotli self
271 CODE:
272 BrotliEncoderDestroyInstance(self->encoder);
273 Safefree(self);
274
275 void
276 set_dictionary(self, dict)
277 IO::Compress::Brotli self
278 SV* dict
279 PREINIT:
280 size_t size;
281 uint8_t *data;
282 CODE:
283 data = SvPV(dict, size);
284 BrotliEncoderSetCustomDictionary(self->encoder, size, data);
This page took 0.030991 seconds and 5 git commands to generate.