Brotli.xs

filter: Statementstmt 66.6% 62 / 93 Branchbran 55.8% 19 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 63.7% 81 / 127 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/IO/Uncompress/Brotli.pm ↑ Summary
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 <brotli/decode.h>
9 #include <brotli/encode.h>
10  
11 #define BUFFER_SIZE 1048576
12  
13 typedef struct brotli_decoder {
14 BrotliDecoderState *decoder;
15 }* IO__Uncompress__Brotli;
16  
17 typedef struct brotli_encoder {
18 BrotliEncoderState *encoder;
19 }* IO__Compress__Brotli;
20  
21  
22 MODULE = IO::Compress::Brotli PACKAGE = IO::Uncompress::Brotli
23 PROTOTYPES: ENABLE
24  
25 SV*
26 unbro_given_size(buffer, decoded_size)
27 SV* buffer
28 size_t decoded_size
29 PREINIT:
30 STRLEN encoded_size;
31 uint8_t *encoded_buffer, *decoded_buffer;
32 CODE:
33 89 encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size);
34 89 Newx(decoded_buffer, decoded_size, uint8_t);
35 89 ▶ if(!BrotliDecoderDecompress(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){
Branch: 1 branch
BranchTrueFalseTotal
if(!BrotliDecoderDecompress(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){ 0 89 89
36 0 Safefree(decoded_buffer);
37 0 croak("Error in BrotliDecoderDecompress");
38 }
39 89 RETVAL = newSV(0);
40 89 sv_usepvn(RETVAL, (char *) decoded_buffer, decoded_size);
41 OUTPUT:
42 RETVAL
43  
44 IO::Uncompress::Brotli
45 create(class)
46 SV* class
47 CODE:
48 178 Newx(RETVAL, 1, struct brotli_decoder);
49 178 RETVAL->decoder = BrotliDecoderCreateInstance(NULL, NULL, NULL);
50 OUTPUT:
51 RETVAL
52  
53 void
54 DESTROY(self)
55 IO::Uncompress::Brotli self
56 CODE:
57 178 BrotliDecoderDestroyInstance(self->decoder);
58 178 Safefree(self);
59  
60 SV*
61 decompress(self, in)
62 IO::Uncompress::Brotli self
63 SV* in
64 PREINIT:
65 uint8_t *next_in, *next_out, *buffer;
66 size_t available_in, available_out;
67 BrotliDecoderResult result;
68 CODE:
69 9482 next_in = (uint8_t*) SvPV(in, available_in);
70 9482 Newx(buffer, BUFFER_SIZE, uint8_t);
71 9482 RETVAL = newSVpv("", 0);
72 9482 result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
73 18964 ▶ while(result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
Branch: 1 branch
BranchTrueFalseTotal
while(result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { 9482 9482 18964
74 9482 next_out = buffer;
75 9482 available_out=BUFFER_SIZE;
76 9482 result = BrotliDecoderDecompressStream( self->decoder,
77 &available_in,
78 (const uint8_t**) &next_in,
79 &available_out,
80 &next_out,
81 NULL );
82 9482 ▶ if(!result){
Branch: 1 branch
BranchTrueFalseTotal
if(!result){ 0 9482 9482
83 0 Safefree(buffer);
84 0 croak("Error in BrotliDecoderDecompressStream");
85 }
86 9482 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
87 }
88 9482 Safefree(buffer);
89 OUTPUT:
90 RETVAL
91  
92  
93 MODULE = IO::Compress::Brotli PACKAGE = IO::Compress::Brotli
94 PROTOTYPES: ENABLE
95  
96 SV*
97 bro(buffer, quality=BROTLI_DEFAULT_QUALITY, lgwin=BROTLI_DEFAULT_WINDOW)
98 SV* buffer
99 U32 quality
100 U32 lgwin
101 PREINIT:
102 size_t encoded_size;
103 STRLEN decoded_size;
104 uint8_t *encoded_buffer, *decoded_buffer;
105 BROTLI_BOOL result;
106 CODE:
107 44 ▶ if( quality < BROTLI_MIN_QUALITY || quality > BROTLI_MAX_QUALITY ) {
Branch: 1 branch
BranchTrueFalseTotal
if( quality < BROTLI_MIN_QUALITY || quality > BROTLI_MAX_QUALITY ) { 0 44 44
108 0 croak("Invalid quality value");
109 }
110 44 ▶ if( lgwin < BROTLI_MIN_WINDOW_BITS || lgwin > BROTLI_MAX_WINDOW_BITS ) {
Branch: 2 branches
BranchTrueFalseTotal
if( lgwin < BROTLI_MIN_WINDOW_BITS || lgwin > BROTLI_MAX_WINDOW_BITS ) { 44 0 44
if( lgwin < BROTLI_MIN_WINDOW_BITS || lgwin > BROTLI_MAX_WINDOW_BITS ) { 0 44 44
111 0 croak("Invalid window value");
112 }
113 44 decoded_buffer = (uint8_t*) SvPV(buffer, decoded_size);
114 44 encoded_size = BrotliEncoderMaxCompressedSize(decoded_size);
115 44 ▶ if(!encoded_size){
Branch: 1 branch
BranchTrueFalseTotal
if(!encoded_size){ 0 44 44
116 0 croak("Compressed size overflow");
117 }
118 44 Newx(encoded_buffer, encoded_size+1, uint8_t);
119 44 result = BrotliEncoderCompress( quality,
120 lgwin,
121 BROTLI_DEFAULT_MODE,
122 decoded_size,
123 decoded_buffer,
124 &encoded_size,
125 encoded_buffer );
126 44 ▶ if(!result){
Branch: 1 branch
BranchTrueFalseTotal
if(!result){ 0 44 44
127 0 Safefree(encoded_buffer);
128 0 croak("Error in BrotliEncoderCompress");
129 }
130 44 encoded_buffer[encoded_size]=0;
131 44 RETVAL = newSV(0);
132 44 sv_usepvn_flags(RETVAL, (char *) encoded_buffer, encoded_size, SV_SMAGIC | SV_HAS_TRAILING_NUL);
133 OUTPUT:
134 RETVAL
135  
136 IO::Compress::Brotli
137 create(class)
138 SV* class
139 CODE:
140 88 Newx(RETVAL, 1, struct brotli_encoder);
141 88 RETVAL->encoder = BrotliEncoderCreateInstance(NULL, NULL, NULL);
142 OUTPUT:
143 RETVAL
144  
145 bool BrotliEncoderSetParameter(self, value)
146 IO::Compress::Brotli self
147 U32 value
148 ALIAS:
149 window = 1
150 quality = 2
151 _mode = 3
152 PREINIT:
153 BrotliEncoderParameter param;
154 INIT:
155 88 switch(ix){
156 0 case 0:
157 0 croak("BrotliEncoderSetParameter may not be called directly");
158 break;
159 0 case 1:
160 0 ▶ if( value < BROTLI_MIN_WINDOW_BITS || value > BROTLI_MAX_WINDOW_BITS ) {
Branch: 2 branches
BranchTrueFalseTotal
if( value < BROTLI_MIN_WINDOW_BITS || value > BROTLI_MAX_WINDOW_BITS ) { 0 0 0
if( value < BROTLI_MIN_WINDOW_BITS || value > BROTLI_MAX_WINDOW_BITS ) { 0 0 0
161 0 croak("Invalid window value");
162 }
163 0 param = BROTLI_PARAM_LGWIN;
164 0 break;
165 88 case 2:
166 88 ▶ if( value < BROTLI_MIN_QUALITY || value > BROTLI_MAX_QUALITY ) {
Branch: 1 branch
BranchTrueFalseTotal
if( value < BROTLI_MIN_QUALITY || value > BROTLI_MAX_QUALITY ) { 0 88 88
167 0 croak("Invalid quality value");
168 }
169 88 param = BROTLI_PARAM_QUALITY;
170 88 break;
171 0 case 3:
172 /* Validation done on Perl side */
173 0 param = BROTLI_PARAM_MODE;
174 0 break;
175 0 default:
176 0 croak("Impossible ix in BrotliEncoderSetParameter");
177 break;
178 }
179 C_ARGS:
180 self->encoder, param, value
181  
182 SV*
183 _compress(self, in = &PL_sv_undef)
184 IO::Compress::Brotli self
185 SV* in
186 ALIAS:
187 compress = 1
188 flush = 2
189 finish = 3
190 PREINIT:
191 uint8_t *next_in, *next_out, *buffer;
192 size_t available_in, available_out;
193 BROTLI_BOOL result;
194 BrotliEncoderOperation op;
195 CODE:
196 176 switch(ix) {
197 0 case 0:
198 0 croak("_compress may not be called directly");
199 break;
200 88 case 1:
201 88 op = BROTLI_OPERATION_PROCESS;
202 88 break;
203 0 case 2:
204 0 op = BROTLI_OPERATION_FLUSH;
205 0 break;
206 88 case 3:
207 88 op = BROTLI_OPERATION_FINISH;
208 88 break;
209 0 default:
210 0 croak("Impossible ix in _compress");
211 break;
212 }
213  
214 176 Newx(buffer, BUFFER_SIZE, uint8_t);
215 176 ▶ if(in == &PL_sv_undef)
Branch: 1 branch
BranchTrueFalseTotal
if(in == &PL_sv_undef) 88 88 176
216 88 next_in = (uint8_t*) buffer, available_in = 0;
217 else
218 88 next_in = (uint8_t*) SvPV(in, available_in);
219 176 RETVAL = newSVpv("", 0);
220 while(1) {
221 176 next_out = buffer;
222 176 available_out = BUFFER_SIZE;
223 176 result = BrotliEncoderCompressStream( self->encoder,
224 (BrotliEncoderOperation) op,
225 &available_in,
226 (const uint8_t**) &next_in,
227 &available_out,
228 &next_out,
229 NULL );
230 176 ▶ if(!result) {
Branch: 1 branch
BranchTrueFalseTotal
if(!result) { 0 176 176
231 0 Safefree(buffer);
232 0 croak("Error in BrotliEncoderCompressStream");
233 }
234  
235 176 ▶ if( available_out != BUFFER_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if( available_out != BUFFER_SIZE ) { 109 67 176
236 109 sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out);
237 }
238  
239 176 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if( 88 88 176
240 176 BrotliEncoderIsFinished(self->encoder) ||
241 88 ▶ (!available_in && !BrotliEncoderHasMoreOutput(self->encoder))
Branch: 2 branches
BranchTrueFalseTotal
(!available_in && !BrotliEncoderHasMoreOutput(self->encoder)) 88 0 88
(!available_in && !BrotliEncoderHasMoreOutput(self->encoder)) 0 88 88
242 ) break;
243 }
244 176 Safefree(buffer);
245 OUTPUT:
246 RETVAL
247  
248 void
249 DESTROY(self)
250 IO::Compress::Brotli self
251 CODE:
252 88 BrotliEncoderDestroyInstance(self->encoder);
253 88 Safefree(self);
« blib/lib/IO/Uncompress/Brotli.pm ↑ Summary