blib/lib/Compression/Util.pm

filter: Statementstmt 95.6% 2489 / 2602 Branchbran 78.6% 983 / 1250 Conditioncond 67.1% 255 / 380 MC/DCmcdc 41.7% 89 / 213 Subroutinesub 100% 151 / 151 Podpod 100% 124 / 124 totaltotal 86.6% 4091 / 4720 CC 936 SCAR 28.0
CC
936
Cov
88%
bzip2_decompress
66.5
gzip_decompress
63.4
lz4_decompress
52.1
CRAP 16.5
↑ Summary
1 package Compression::Util;
2  
3 48 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 48 ▶use 5.036;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 48 ▶use List::Util qw(min uniq max sum all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 48 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 48 require Exporter;
9  
10 48 our @ISA = qw(Exporter);
11  
12 48 our $VERSION = '0.18';
13 48 our $VERBOSE = 0; # verbose mode
14  
15 48 our $LZ_MIN_LEN = 4; # minimum match length in LZ parsing
16 48 our $LZ_MAX_LEN = 1 << 15; # maximum match length in LZ parsing
17 48 our $LZ_MAX_DIST = ~0; # maximum allowed back-reference distance in LZ parsing
18 48 our $LZ_MAX_CHAIN_LEN = 32; # how many recent positions to remember in LZ parsing
19 48 our $LZ_MAX_CHAIN_WIDTH = 1024; # maximum number of positions to remember for each match
20  
21 # Arithmetic Coding settings
22 48 ▶use constant BITS => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 48 ▶use constant MAX => oct('0b' . ('1' x BITS));
Subroutine BEGIN: called CC=1 SCAR=0.0
24 48 ▶use constant INITIAL_FREQ => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 48 our %EXPORT_TAGS = (
27 'all' => [
28 qw(
29  
30 crc32
31 adler32
32  
33 read_bit
34 read_bit_lsb
35  
36 read_bits
37 read_bits_lsb
38  
39 int2bits
40 int2bits_lsb
41  
42 int2bytes
43 int2bytes_lsb
44  
45 bits2int
46 bits2int_lsb
47  
48 bytes2int
49 bytes2int_lsb
50  
51 string2symbols
52 symbols2string
53  
54 read_null_terminated
55  
56 bwt_encode
57 bwt_decode
58  
59 bwt_encode_symbolic
60 bwt_decode_symbolic
61  
62 bwt_sort
63 bwt_sort_symbolic
64  
65 bwt_compress
66 bwt_decompress
67  
68 bwt_compress_symbolic
69 bwt_decompress_symbolic
70  
71 bzip2_compress
72 bzip2_decompress
73  
74 gzip_compress
75 gzip_decompress
76  
77 mrl_compress
78 mrl_decompress
79  
80 mrl_compress_symbolic
81 mrl_decompress_symbolic
82  
83 create_huffman_entry
84 decode_huffman_entry
85  
86 delta_encode
87 delta_decode
88  
89 huffman_encode
90 huffman_decode
91  
92 huffman_from_freq
93 huffman_from_symbols
94 huffman_from_code_lengths
95  
96 mtf_encode
97 mtf_decode
98  
99 encode_alphabet
100 decode_alphabet
101  
102 encode_alphabet_256
103 decode_alphabet_256
104  
105 deltas
106 accumulate
107 frequencies
108  
109 run_length
110  
111 binary_vrl_encode
112 binary_vrl_decode
113  
114 rle4_encode
115 rle4_decode
116  
117 zrle_encode
118 zrle_decode
119  
120 lzss_compress
121 lzss_decompress
122  
123 make_deflate_tables
124 find_deflate_index
125  
126 deflate_encode
127 deflate_decode
128  
129 lzss_encode
130 lzss_encode_fast
131 lzss_encode_fast_symbolic
132 lzss_decode
133  
134 lzss_encode_symbolic
135 lzss_decode_symbolic
136  
137 lzss_compress_symbolic
138 lzss_decompress_symbolic
139  
140 lz77_encode
141 lz77_decode
142  
143 lz77_encode_symbolic
144 lz77_decode_symbolic
145  
146 lz77_compress
147 lz77_decompress
148  
149 lz77_compress_symbolic
150 lz77_decompress_symbolic
151  
152 lzb_compress
153 lzb_decompress
154  
155 lz4_compress
156 lz4_decompress
157  
158 ac_encode
159 ac_decode
160  
161 create_ac_entry
162 decode_ac_entry
163  
164 adaptive_ac_encode
165 adaptive_ac_decode
166  
167 create_adaptive_ac_entry
168 decode_adaptive_ac_entry
169  
170 abc_encode
171 abc_decode
172  
173 fibonacci_encode
174 fibonacci_decode
175  
176 elias_gamma_encode
177 elias_gamma_decode
178  
179 elias_delta_encode
180 elias_delta_decode
181  
182 elias_omega_encode
183 elias_omega_decode
184  
185 obh_encode
186 obh_decode
187  
188 lzw_encode
189 lzw_decode
190  
191 lzw_compress
192 lzw_decompress
193  
194 zlib_compress
195 zlib_decompress
196  
197 deflate_create_block_type_0_header
198 deflate_create_block_type_1
199 deflate_create_block_type_2
200  
201 deflate_extract_next_block
202 deflate_extract_block_type_0
203 deflate_extract_block_type_1
204 deflate_extract_block_type_2
205  
206 golomb_rice_encode
207 golomb_rice_decode
208  
209 leb128_encode
210 leb128_decode
211  
212 shannon_entropy
213  
214 detect_format
215 decompress_auto
216  
217 best_compress
218 best_decompress
219 )
220 ]
221 );
222  
223 48 our @EXPORT_OK = (@{$EXPORT_TAGS{'all'}}, '$VERBOSE', '$LZ_MAX_CHAIN_LEN', '$LZ_MAX_CHAIN_WIDTH', '$LZ_MIN_LEN', '$LZ_MAX_LEN', '$LZ_MAX_DIST');
224 48 our @EXPORT;
225  
226 ##########################
227 # Misc low-level functions
228 ##########################
229  
230 191888 ▶sub read_bit ($fh, $bitstring) {
Subroutine read_bit: called CC=4 SCAR=14.3
Pod documented
231  
232 191888 ▶ if (($$bitstring // '') eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (($$bitstring // "") eq "") 26199 165689 191888
Condition50%1/2
$$bitstring // ""
l!l
1918880
MC/DC0%0/1
$$bitstring // ""
$$bitstring
Truth table50%1/2
A
A= $$bitstring
Aresult
11
00
233 26199 ▶ $$bitstring = unpack('b*', getc($fh) // confess "can't read bit");
Condition50%1/2
getc $fh // confess("can't read bit")
l!l
261990
MC/DC0%0/1
getc $fh // confess("can't read bit")
getc $fh
Truth table50%1/2
A
A= getc $fh
Aresult
11
00
234 }
235  
236 191888 chop($$bitstring);
237 }
238  
239 8211 ▶sub read_bit_lsb ($fh, $bitstring) {
Subroutine read_bit_lsb: called CC=4 SCAR=14.3
Pod documented
240  
241 8211 ▶ if (($$bitstring // '') eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (($$bitstring // "") eq "") 4331 3880 8211
Condition50%1/2
$$bitstring // ""
l!l
82110
MC/DC0%0/1
$$bitstring // ""
$$bitstring
Truth table50%1/2
A
A= $$bitstring
Aresult
11
00
242 4331 ▶ $$bitstring = unpack('B*', getc($fh) // confess "can't read bit");
Condition50%1/2
getc $fh // confess("can't read bit")
l!l
43310
MC/DC0%0/1
getc $fh // confess("can't read bit")
getc $fh
Truth table50%1/2
A
A= getc $fh
Aresult
11
00
243 }
244  
245 8211 chop($$bitstring);
246 }
247  
248 1014 ▶sub read_bits ($fh, $bits_len) {
Subroutine read_bits: called CC=5 SCAR=16.2
Pod documented
249  
250 1014 ▶ read($fh, (my $data), $bits_len >> 3) // confess "Read error: $!";
Condition50%1/2
read($fh, my $data, $bits_len >> 3) // confess("Read error: $!")
l!l
10140
MC/DC0%0/1
read($fh, my $data, $bits_len >> 3) // confess("Read error: $!")
read($fh, my $data, $bits_len >> 3)
Truth table50%1/2
A
A= read($fh, my $data, $bits_len >> 3)
Aresult
11
00
251 1014 $data = unpack('B*', $data);
252  
253 1014 ▶ while (length($data) < $bits_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < $bits_len) 527 1014 1541
254 527 ▶ $data .= unpack('B*', getc($fh) // confess "can't read bits");
Condition50%1/2
getc $fh // confess("can't read bits")
l!l
5270
MC/DC0%0/1
getc $fh // confess("can't read bits")
getc $fh
Truth table50%1/2
A
A= getc $fh
Aresult
11
00
255 }
256  
257 1014 ▶ if (length($data) > $bits_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > $bits_len) 527 487 1014
258 527 $data = substr($data, 0, $bits_len);
259 }
260  
261 1014 return $data;
262 }
263  
264 1 ▶sub read_bits_lsb ($fh, $bits_len) {
Subroutine read_bits_lsb: called CC=5 SCAR=20.2
Pod documented
265  
266 1 ▶ read($fh, (my $data), $bits_len >> 3) // confess "Read error: $!";
Condition50%1/2
read($fh, my $data, $bits_len >> 3) // confess("Read error: $!")
l!l
10
MC/DC0%0/1
read($fh, my $data, $bits_len >> 3) // confess("Read error: $!")
read($fh, my $data, $bits_len >> 3)
Truth table50%1/2
A
A= read($fh, my $data, $bits_len >> 3)
Aresult
11
00
267 1 $data = unpack('b*', $data);
268  
269 1 ▶ while (length($data) < $bits_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < $bits_len) 0 1 1
270 0 ▶ $data .= unpack('b*', getc($fh) // confess "can't read bits");
Condition0%0/2
getc $fh // confess("can't read bits")
l!l
00
MC/DC0%0/1
getc $fh // confess("can't read bits")
getc $fh
Truth table0%0/2
A
A= getc $fh
Aresult
11
00
271 }
272  
273 1 ▶ if (length($data) > $bits_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > $bits_len) 0 1 1
274 0 $data = substr($data, 0, $bits_len);
275 }
276  
277 1 return $data;
278 }
279  
280 139 ▶sub int2bits ($value, $size) {
Subroutine int2bits: called CC=1 SCAR=0.0
Pod documented
281 139 sprintf("%0*b", $size, $value);
282 }
283  
284 13230 ▶sub int2bits_lsb ($value, $size) {
Subroutine int2bits_lsb: called CC=1 SCAR=0.0
Pod documented
285 13230 scalar reverse sprintf("%0*b", $size, $value);
286 }
287  
288 259 ▶sub int2bytes ($value, $size) {
Subroutine int2bytes: called CC=1 SCAR=0.0
Pod documented
289 259 pack('B*', sprintf("%0*b", 8 * $size, $value));
290 }
291  
292 213 ▶sub int2bytes_lsb ($value, $size) {
Subroutine int2bytes_lsb: called CC=1 SCAR=0.0
Pod documented
293 213 pack('b*', scalar reverse sprintf("%0*b", 8 * $size, $value));
294 }
295  
296 803 ▶sub bytes2int ($fh, $n) {
Subroutine bytes2int: called CC=4 SCAR=14.1
Pod documented
297 803 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 8 795 803
298 8 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 8 0 8
299 8 return __SUB__->($fh2, $n);
300 }
301 795 ▶ read($fh, my $bytes, $n) == $n or confess "truncated read: expected $n bytes";
Branch: 1 branch
BranchTrueFalseTotal
read($fh, my $bytes, $n) == $n or confess("truncated read: expected $n bytes") 795 0 795
302 795 oct('0b' . unpack('B*', $bytes));
303 }
304  
305 391 ▶sub bytes2int_lsb ($fh, $n) {
Subroutine bytes2int_lsb: called CC=4 SCAR=14.1
Pod documented
306 391 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 8 383 391
307 8 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 8 0 8
308 8 return __SUB__->($fh2, $n);
309 }
310 383 ▶ read($fh, my $bytes, $n) == $n or confess "truncated read: expected $n bytes";
Branch: 1 branch
BranchTrueFalseTotal
read($fh, my $bytes, $n) == $n or confess("truncated read: expected $n bytes") 383 0 383
311 383 oct('0b' . reverse unpack('b*', $bytes));
312 }
313  
314 1425 ▶sub bits2int ($fh, $size, $buffer) {
Subroutine bits2int: called CC=7 SCAR=19.5
Pod documented
315  
316 1425 ▶ if ($size % 8 == 0 and ($$buffer // '') eq '') { # optimization
Branch: 1 branch
BranchTrueFalseTotal
if ($size % 8 == 0 and ($$buffer // "") eq "") 39 1386 1425
Condition100%5/5
$size % 8 == 0 and ($$buffer // "") eq ""
!ll&&!rl&&r
118420239
$$buffer // ""
l!l
2401
MC/DC100%2/2
$size % 8 == 0 and ($$buffer // "") eq ""
$size % 8 == 0 $$buffer
Truth table100%3/3
A and B
A= $size % 8 == 0B= $$buffer
ABresult
0X0
100
111
317 39 return bytes2int($fh, $size >> 3);
318 }
319  
320 1386 my $bitstring = '0b';
321 1386 for (1 .. $size) {
322 12657 ▶ $bitstring .= ($$buffer // '') eq '' ? read_bit($fh, $buffer) : chop($$buffer);
Branch: 1 branch
BranchTrueFalseTotal
($$buffer // '') eq '' ? : 701 11956 12657
Condition50%1/2
$$buffer // ""
l!l
126570
MC/DC0%0/1
$$buffer // ""
$$buffer
Truth table50%1/2
A
A= $$buffer
Aresult
11
00
323 }
324 1386 oct($bitstring);
325 }
326  
327 5838 ▶sub bits2int_lsb ($fh, $size, $buffer) {
Subroutine bits2int_lsb: called CC=7 SCAR=19.5
Pod documented
328  
329 5838 ▶ if ($size % 8 == 0 and ($$buffer // '') eq '') { # optimization
Branch: 1 branch
BranchTrueFalseTotal
if ($size % 8 == 0 and ($$buffer // "") eq "") 173 5665 5838
Condition100%5/5
$size % 8 == 0 and ($$buffer // "") eq ""
!ll&&!rl&&r
5298367173
$$buffer // ""
l!l
5391
MC/DC100%2/2
$size % 8 == 0 and ($$buffer // "") eq ""
$size % 8 == 0 $$buffer
Truth table100%3/3
A and B
A= $size % 8 == 0B= $$buffer
ABresult
0X0
100
111
330 173 return bytes2int_lsb($fh, $size >> 3);
331 }
332  
333 5665 my $bitstring = '';
334 5665 for (1 .. $size) {
335 29989 ▶ $bitstring .= ($$buffer // '') eq '' ? read_bit_lsb($fh, $buffer) : chop($$buffer);
Branch: 1 branch
BranchTrueFalseTotal
($$buffer // '') eq '' ? : 3682 26307 29989
Condition50%1/2
$$buffer // ""
l!l
299890
MC/DC0%0/1
$$buffer // ""
$$buffer
Truth table50%1/2
A
A= $$buffer
Aresult
11
00
336 }
337 5665 oct('0b' . reverse($bitstring));
338 }
339  
340 186 ▶sub string2symbols ($string) {
Subroutine string2symbols: called CC=1 SCAR=0.0
Pod documented
341 186 [unpack('C*', $string)];
342 }
343  
344 113 ▶sub symbols2string ($symbols) {
Subroutine symbols2string: called CC=1 SCAR=0.0
Pod documented
345 113 pack('C*', @$symbols);
346 }
347  
348 8 ▶sub read_null_terminated ($fh) {
Subroutine read_null_terminated: called CC=2 SCAR=7.0
Pod documented
349 8 local $/ = "\0";
350 8 ▶ my $string = <$fh> // confess "can't read string";
Condition50%1/2
readline $fh // confess("can't read string")
l!l
80
MC/DC0%0/1
readline $fh // confess("can't read string")
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
351 8 chop($string);
352 8 return $string;
353 }
354  
355 844 ▶sub frequencies ($symbols) {
Subroutine frequencies: called CC=2 SCAR=6.9
Pod documented
356 844 my %freq;
357 844 ++$freq{$_} for @$symbols;
358 844 return \%freq;
359 }
360  
361 2 ▶sub shannon_entropy ($symbols) {
Subroutine shannon_entropy: called CC=3 SCAR=11.0
Pod documented
362  
363 2 ▶ my $len = scalar(@$symbols) || return 0;
Condition50%1/2
scalar @$symbols || (return 0)
l!l
20
MC/DC0%0/1
scalar @$symbols || (return 0)
scalar @$symbols
Truth table50%1/2
A
A= scalar @$symbols
Aresult
11
00
364 2 my $freq = frequencies($symbols);
365  
366 2 my $entropy = 0;
367 2 foreach my $count (values %$freq) {
368 3 my $p = $count / $len;
369 3 $entropy -= $p * log($p);
370 }
371  
372 2 return $entropy / log(2);
373 }
374  
375 1056 ▶sub deltas ($integers) {
Subroutine deltas: called CC=2 SCAR=6.9
Pod documented
376  
377 1056 my @deltas;
378 1056 my $prev = 0;
379  
380 1056 foreach my $n (@$integers) {
381 224816 push @deltas, $n - $prev;
382 224816 $prev = $n;
383 }
384  
385 1056 return \@deltas;
386 }
387  
388 471 ▶sub accumulate ($deltas) {
Subroutine accumulate: called CC=2 SCAR=6.9
Pod documented
389  
390 471 my @acc;
391 471 my $prev = 0;
392  
393 471 foreach my $d (@$deltas) {
394 8022 $prev += $d;
395 8022 push @acc, $prev;
396 }
397  
398 471 return \@acc;
399 }
400  
401 ########################
402 # Fibonacci Coding
403 ########################
404  
405 439 ▶sub fibonacci_encode ($symbols) {
Subroutine fibonacci_encode: called CC=5 SCAR=16.1
Pod documented
406  
407 439 my $bitstring = '';
408  
409 439 foreach my $n (scalar(@$symbols), @$symbols) {
410 2475 my ($f1, $f2, $f3) = (0, 1, 1);
411 2475 my ($rn, $s, $k) = ($n + 1, '', 2);
412 2475 ▶ for (; $f3 <= $rn ; ++$k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f3 <= $rn) 18775 2475 21250
413 18775 ($f1, $f2, $f3) = ($f2, $f3, $f2 + $f3);
414 }
415 2475 foreach my $i (1 .. $k - 2) {
416 18775 ($f3, $f2, $f1) = ($f2, $f1, $f2 - $f1);
417 18775 ▶ if ($f3 <= $rn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f3 <= $rn) { } 6124 12651 18775
418 6124 $rn -= $f3;
419 6124 $s .= '1';
420 }
421 else {
422 12651 $s .= '0';
423 }
424 }
425 2475 $bitstring .= reverse($s) . '1';
426 }
427  
428 439 pack('B*', $bitstring);
429 }
430  
431 452 ▶sub fibonacci_decode ($fh) {
Subroutine fibonacci_decode: called CC=9 SCAR=22.0
Pod documented
432  
433 452 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 13 439 452
434 13 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 13 0 13
435 13 return __SUB__->($fh2);
436 }
437  
438 439 my @symbols;
439  
440 439 my $enc = '';
441 439 my $prev_bit = '0';
442  
443 439 my $len = 0;
444 439 my $buffer = '';
445  
446 439 ▶ for (my $k = 0 ; $k <= $len ;) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $len) 21250 439 21689
447 21250 my $bit = read_bit($fh, \$buffer);
448  
449 21250 ▶ if ($bit eq '1' and $prev_bit eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit eq '1' and $prev_bit eq '1') { } 2475 18775 21250
Condition100%3/3
$bit eq 1 and $prev_bit eq 1
!ll&&!rl&&r
1265161242475
MC/DC100%2/2
$bit eq 1 and $prev_bit eq 1
$bit eq 1 $prev_bit eq 1
Truth table100%3/3
A and B
A= $bit eq 1B= $prev_bit eq 1
ABresult
0X0
100
111
450 2475 my ($value, $f1, $f2) = (0, 1, 1);
451 2475 foreach my $bit (split //, $enc) {
452 18775 ▶ $value += $f2 if $bit;
Branch: 1 branch
BranchTrueFalseTotal
if $bit 6124 12651 18775
453 18775 ($f1, $f2) = ($f2, $f1 + $f2);
454 }
455 2475 push @symbols, $value - 1;
456 2475 ▶ $len = pop @symbols if (++$k == 1);
Branch: 1 branch
BranchTrueFalseTotal
if ++$k == 1 439 2036 2475
457 2475 $enc = '';
458 2475 $prev_bit = '0';
459 }
460 else {
461 18775 $enc .= $bit;
462 18775 $prev_bit = $bit;
463 }
464 }
465  
466 439 return \@symbols;
467 }
468  
469 #######################################
470 # Adaptive Binary Concatenation method
471 #######################################
472  
473 31 ▶sub abc_encode ($integers) {
Subroutine abc_encode: called CC=9 SCAR=22.1
Pod documented
474  
475 31 my @counts;
476 31 my $count = 0;
477 31 my $bits_width = 1;
478 31 my $bits_max_symbol = 1 << $bits_width;
479 31 my $processed_len = 0;
480  
481 31 foreach my $k (@$integers) {
482 9363 ▶ while ($k >= $bits_max_symbol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k >= $bits_max_symbol) 202 9363 9565
483  
484 202 ▶ if ($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 23 179 202
485 23 push @counts, [$bits_width, $count];
486 23 $processed_len += $count;
487 }
488  
489 202 $count = 0;
490 202 $bits_max_symbol *= 2;
491 202 $bits_width += 1;
492 }
493 9363 ++$count;
494 }
495  
496 31 push @counts, grep { $_->[1] > 0 } [$bits_width, scalar(@$integers) - $processed_len];
497  
498 0 ▶ $VERBOSE && say STDERR "Bit sizes: ", join(' ', map { $_->[0] } @counts);
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Bit sizes: ", join(" ", map({$_->[0];} @counts)) 0 31 31
499 0 ▶ $VERBOSE && say STDERR "Lengths : ", join(' ', map { $_->[1] } @counts);
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Lengths : ", join(" ", map({$_->[1];} @counts)) 0 31 31
500 31 ▶ $VERBOSE && say STDERR '';
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "" 0 31 31
501  
502 31 my $compressed = fibonacci_encode([(map { $_->[0] } @counts), (map { $_->[1] } @counts)]);
503  
504 31 my $bits = '';
505 31 my @ints = @$integers;
506  
507 31 foreach my $pair (@counts) {
508 52 my ($blen, $len) = @$pair;
509 52 foreach my $symbol (splice(@ints, 0, $len)) {
510 9363 $bits .= sprintf("%0*b", $blen, $symbol);
511 }
512 }
513  
514 31 $compressed .= pack('B*', $bits);
515 31 return $compressed;
516 }
517  
518 44 ▶sub abc_decode ($fh) {
Subroutine abc_decode: called CC=7 SCAR=19.5
Pod documented
519  
520 44 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 13 31 44
521 13 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 13 0 13
522 13 return __SUB__->($fh2);
523 }
524  
525 31 my $ints = fibonacci_decode($fh);
526 31 my $half = scalar(@$ints) >> 1;
527  
528 31 my @counts;
529 31 foreach my $i (0 .. ($half - 1)) {
530 52 push @counts, [$ints->[$i], $ints->[$half + $i]];
531 }
532  
533 31 my $bits_len = 0;
534  
535 31 foreach my $pair (@counts) {
536 52 my ($blen, $len) = @$pair;
537 52 $bits_len += $blen * $len;
538 }
539  
540 31 my $bits = read_bits($fh, $bits_len);
541  
542 31 my @integers;
543 31 foreach my $pair (@counts) {
544 52 my ($blen, $len) = @$pair;
545 52 foreach my $chunk (unpack(sprintf('(a%d)*', $blen), substr($bits, 0, $blen * $len, ''))) {
546 9363 push @integers, oct('0b' . $chunk);
547 }
548 }
549  
550 31 return \@integers;
551 }
552  
553 ################################################
554 # LEB128 (byte-aligned variable-length quantity)
555 ################################################
556  
557 13 ▶sub leb128_encode ($integers) {
Subroutine leb128_encode: called CC=4 SCAR=13.9
Pod documented
558  
559 13 my $bytes = '';
560  
561 13 foreach my $n (scalar(@$integers), @$integers) {
562 1065 ▶ $n >= 0 or confess "error: leb128_encode() requires non-negative integers";
Branch: 1 branch
BranchTrueFalseTotal
$n >= 0 or confess("error: leb128_encode() requires non-negative integers") 1065 0 1065
563  
564 1065 my $v = $n;
565 1065 while (1) {
566 1092 my $byte = $v & 0x7f;
567 1092 $v >>= 7;
568 1092 ▶ if ($v != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v != 0) { } 27 1065 1092
569 27 $bytes .= chr($byte | 0x80);
570 }
571 else {
572 1065 $bytes .= chr($byte);
573 1065 last;
574 }
575 }
576 }
577  
578 13 return $bytes;
579 }
580  
581 26 ▶sub leb128_decode ($fh) {
Subroutine leb128_decode: called CC=4 SCAR=13.9
Pod documented
582  
583 26 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 13 13 26
584 13 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 13 0 13
585 13 return __SUB__->($fh2);
586 }
587  
588 1065 ▶ my $read_one = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
589 1065 my ($n, $shift) = (0, 0);
590 1065 while (1) {
591 1092 ▶ my $byte = ord(getc($fh) // confess "can't read byte");
Condition50%1/2
getc $fh // confess("can't read byte")
l!l
10920
MC/DC0%0/1
getc $fh // confess("can't read byte")
getc $fh
Truth table50%1/2
A
A= getc $fh
Aresult
11
00
592 1092 $n |= ($byte & 0x7f) << $shift;
593 1092 ▶ last if !($byte & 0x80);
Branch: 1 branch
BranchTrueFalseTotal
unless $byte & 128 1065 27 1092
594 27 $shift += 7;
595 }
596 1065 return $n;
597 13 };
598  
599 13 my $len = $read_one->();
600  
601 13 my @integers;
602 13 push @integers, $read_one->() for (1 .. $len);
603  
604 13 return \@integers;
605 }
606  
607 ###################################
608 # Arithmetic Coding (in fixed bits)
609 ###################################
610  
611 158 ▶sub _create_cfreq ($freq) {
Subroutine _create_cfreq: called CC=3 SCAR=11.0
612  
613 158 my @cf;
614 158 my $T = 0;
615  
616 158 foreach my $i (sort { $a <=> $b } keys %$freq) {
617 1652 ▶ $freq->{$i} // next;
Condition50%1/2
$freq->{$i} // (next)
l!l
16520
MC/DC0%0/1
$freq->{$i} // (next)
$freq->{$i}
Truth table50%1/2
A
A= $freq->{$i}
Aresult
11
00
618 1652 $cf[$i] = $T;
619 1652 $T += $freq->{$i};
620 1652 $cf[$i + 1] = $T;
621 }
622  
623 158 return (\@cf, $T);
624 }
625  
626 79 ▶sub ac_encode ($symbols) {
Subroutine ac_encode: called CC=12 SCAR=25.3
Pod documented
627  
628 79 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 79 79
629 0 $symbols = string2symbols($symbols);
630 }
631  
632 79 my $enc = '';
633 79 ▶ my $EOF_SYMBOL = (max(@$symbols) // 0) + 1;
Condition100%2/2
&max(@$symbols) // 0
l!l
754
MC/DC100%1/1
&max(@$symbols) // 0
&max(@$symbols)
Truth table100%2/2
A
A= &max(@$symbols)
Aresult
11
00
634 79 my @bytes = (@$symbols, $EOF_SYMBOL);
635  
636 79 my $freq = frequencies(\@bytes);
637 79 my ($cf, $T) = _create_cfreq($freq);
638  
639 79 ▶ if ($T > MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($T > 4294967295) 0 79 79
640 0 confess "Too few bits: $T > ${\MAX}";
641 }
642  
643 79 my $low = 0;
644 79 my $high = MAX;
645 79 my $uf_count = 0;
646  
647 79 foreach my $c (@bytes) {
648  
649 4984 my $w = $high - $low + 1;
650  
651 4984 $high = ($low + int(($w * $cf->[$c + 1]) / $T) - 1) & MAX;
652 4984 $low = ($low + int(($w * $cf->[$c]) / $T)) & MAX;
653  
654 4984 ▶ if ($high > MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high > 4294967295) 0 4984 4984
655 0 confess "high > MAX: $high > ${\MAX}";
656 }
657  
658 0 ▶ if ($low >= $high) { confess "$low >= $high" }
Branch: 1 branch
BranchTrueFalseTotal
if ($low >= $high) 0 4984 4984
659  
660 4984 while (1) {
661  
662 19445 ▶ if (($high >> (BITS - 1)) == ($low >> (BITS - 1))) {
Branch: 2 branches
BranchTrueFalseTotal
if ($high >> 31 == $low >> 31) { } 11534 7911 19445
elsif (($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0) { } 2927 4984 7911
663  
664 11534 my $bit = $high >> (BITS - 1);
665 11534 $enc .= $bit;
666  
667 11534 ▶ if ($uf_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uf_count > 0) 1433 10101 11534
668 1433 $enc .= join('', 1 - $bit) x $uf_count;
669 1433 $uf_count = 0;
670 }
671  
672 11534 $low <<= 1;
673 11534 ($high <<= 1) |= 1;
674 }
675 ▶ elsif (((($low >> (BITS - 2)) & 0x1) == 1) && ((($high >> (BITS - 2)) & 0x1) == 0)) {
Condition100%3/3
($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0
!ll&&!rl&&r
370012842927
MC/DC100%2/2
($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0
($low >> 30 & 1) == 1 ($high >> 30 & 1) == 0
Truth table100%3/3
A and B
A= ($low >> 30 & 1) == 1B= ($high >> 30 & 1) == 0
ABresult
0X0
100
111
676 2927 ($high <<= 1) |= (1 << (BITS - 1));
677 2927 $high |= 1;
678 2927 ($low <<= 1) &= ((1 << (BITS - 1)) - 1);
679 2927 ++$uf_count;
680 }
681 else {
682 4984 last;
683 }
684  
685 14461 $low &= MAX;
686 14461 $high &= MAX;
687 }
688 }
689  
690 79 $enc .= '0';
691 79 $enc .= '1';
692  
693 79 ▶ while (length($enc) % 8 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($enc) % 8 != 0) 323 79 402
694 323 $enc .= '1';
695 }
696  
697 79 return ($enc, $freq);
698 }
699  
700 92 ▶sub ac_decode ($fh, $freq) {
Subroutine ac_decode: called CC=16 SCAR=27.9
Pod documented
701  
702 92 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 13 79 92
703 13 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 13 0 13
704 13 return __SUB__->($fh2, $freq);
705 }
706  
707 79 my ($cf, $T) = _create_cfreq($freq);
708  
709 79 my @dec;
710 79 my $low = 0;
711 79 my $high = MAX;
712 79 ▶ my $enc = oct('0b' . join '', map { getc($fh) // 1 } 1 .. BITS);
Condition100%2/2
getc $fh // 1
l!l
1808720
MC/DC100%1/1
getc $fh // 1
getc $fh
Truth table100%2/2
A
A= getc $fh
Aresult
11
00
713  
714 79 my @table;
715 79 foreach my $i (sort { $a <=> $b } keys %$freq) {
716 826 foreach my $j ($cf->[$i] .. $cf->[$i + 1] - 1) {
717 4984 $table[$j] = $i;
718 }
719 }
720  
721 79 ▶ my $EOF_SYMBOL = max(keys %$freq) // 0;
Condition50%1/2
&max(keys %$freq) // 0
l!l
790
MC/DC0%0/1
&max(keys %$freq) // 0
&max(keys %$freq)
Truth table50%1/2
A
A= &max(keys %$freq)
Aresult
11
00
722  
723 79 while (1) {
724  
725 4984 my $w = $high - $low + 1;
726 4984 my $ss = int((($T * ($enc - $low + 1)) - 1) / $w);
727  
728 4984 ▶ my $i = $table[$ss] // last;
Condition50%1/2
$table[$ss] // (last)
l!l
49840
MC/DC0%0/1
$table[$ss] // (last)
$table[$ss]
Truth table50%1/2
A
A= $table[$ss]
Aresult
11
00
729 4984 ▶ last if ($i == $EOF_SYMBOL);
Branch: 1 branch
BranchTrueFalseTotal
if $i == $EOF_SYMBOL 79 4905 4984
730  
731 4905 push @dec, $i;
732  
733 4905 $high = ($low + int(($w * $cf->[$i + 1]) / $T) - 1) & MAX;
734 4905 $low = ($low + int(($w * $cf->[$i]) / $T)) & MAX;
735  
736 4905 ▶ if ($high > MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high > 4294967295) 0 4905 4905
737 0 confess "error";
738 }
739  
740 0 ▶ if ($low >= $high) { confess "$low >= $high" }
Branch: 1 branch
BranchTrueFalseTotal
if ($low >= $high) 0 4905 4905
741  
742 4905 while (1) {
743  
744 19055 ▶ if (($high >> (BITS - 1)) == ($low >> (BITS - 1))) {
Branch: 2 branches
BranchTrueFalseTotal
if ($high >> 31 == $low >> 31) { } 11293 7762 19055
elsif (($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0) { } 2857 4905 7762
745 11293 ($high <<= 1) |= 1;
746 11293 $low <<= 1;
747 11293 ▶ ($enc <<= 1) |= (getc($fh) // 1);
Condition100%2/2
getc $fh // 1
l!l
10594699
MC/DC100%1/1
getc $fh // 1
getc $fh
Truth table100%2/2
A
A= getc $fh
Aresult
11
00
748 }
749 ▶ elsif (((($low >> (BITS - 2)) & 0x1) == 1) && ((($high >> (BITS - 2)) & 0x1) == 0)) {
Condition100%3/3
($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0
!ll&&!rl&&r
364512602857
MC/DC100%2/2
($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0
($low >> 30 & 1) == 1 ($high >> 30 & 1) == 0
Truth table100%3/3
A and B
A= ($low >> 30 & 1) == 1B= ($high >> 30 & 1) == 0
ABresult
0X0
100
111
750 2857 ($high <<= 1) |= (1 << (BITS - 1));
751 2857 $high |= 1;
752 2857 ($low <<= 1) &= ((1 << (BITS - 1)) - 1);
753 2857 ▶ $enc = (($enc >> (BITS - 1)) << (BITS - 1)) | (($enc & ((1 << (BITS - 2)) - 1)) << 1) | (getc($fh) // 1);
Condition100%2/2
getc $fh // 1
l!l
2470387
MC/DC100%1/1
getc $fh // 1
getc $fh
Truth table100%2/2
A
A= getc $fh
Aresult
11
00
754 }
755 else {
756 4905 last;
757 }
758  
759 14150 $low &= MAX;
760 14150 $high &= MAX;
761 14150 $enc &= MAX;
762 }
763 }
764  
765 79 return \@dec;
766 }
767  
768 #############################################
769 # Adaptive Arithemtic Coding (in fixed bits)
770 #############################################
771  
772 334 ▶sub _create_adaptive_cfreq ($freq_value, $alphabet_size) {
Subroutine _create_adaptive_cfreq: called CC=2 SCAR=6.9
773  
774 334 my $T = 0;
775 334 my (@cf, @freq);
776  
777 334 foreach my $i (0 .. $alphabet_size) {
778 2116 $freq[$i] = $freq_value;
779 2116 $cf[$i] = $T;
780 2116 $T += $freq_value;
781 2116 $cf[$i + 1] = $T;
782 }
783  
784 334 return (\@freq, \@cf, $T);
785 }
786  
787 9817 ▶sub _increment_freq ($c, $alphabet_size, $freq, $cf) {
Subroutine _increment_freq: called CC=2 SCAR=6.9
788  
789 9817 ++$freq->[$c];
790 9817 my $T = $cf->[$c];
791  
792 9817 foreach my $i ($c .. $alphabet_size) {
793 192645 $cf->[$i] = $T;
794 192645 $T += $freq->[$i];
795 192645 $cf->[$i + 1] = $T;
796 }
797  
798 9817 return $T;
799 }
800  
801 167 ▶sub adaptive_ac_encode ($symbols) {
Subroutine adaptive_ac_encode: called CC=12 SCAR=25.2
Pod documented
802  
803 167 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 167 167
804 0 $symbols = string2symbols($symbols);
805 }
806  
807 167 my $enc = '';
808 167 my @alphabet = sort { $a <=> $b } uniq(@$symbols);
809 167 ▶ my $EOF_SYMBOL = scalar(@alphabet) ? ($alphabet[-1] + 1) : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @alphabet ? : 143 24 167
810 167 push @alphabet, $EOF_SYMBOL;
811  
812 167 my $alphabet_size = $#alphabet;
813 167 my ($freq, $cf, $T) = _create_adaptive_cfreq(INITIAL_FREQ, $alphabet_size);
814  
815 167 my %table;
816 167 @table{@alphabet} = (0 .. $alphabet_size);
817  
818 167 ▶ if ($T > MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($T > 4294967295) 0 167 167
819 0 confess "Too few bits: $T > ${\MAX}";
820 }
821  
822 167 my $low = 0;
823 167 my $high = MAX;
824 167 my $uf_count = 0;
825  
826 167 foreach my $value (@$symbols, $EOF_SYMBOL) {
827  
828 4992 my $c = $table{$value};
829 4992 my $w = $high - $low + 1;
830  
831 4992 $high = ($low + int(($w * $cf->[$c + 1]) / $T) - 1) & MAX;
832 4992 $low = ($low + int(($w * $cf->[$c]) / $T)) & MAX;
833  
834 4992 $T = _increment_freq($c, $alphabet_size, $freq, $cf);
835  
836 4992 ▶ if ($high > MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high > 4294967295) 0 4992 4992
837 0 confess "high > MAX: $high > ${\MAX}";
838 }
839  
840 0 ▶ if ($low >= $high) { confess "$low >= $high" }
Branch: 1 branch
BranchTrueFalseTotal
if ($low >= $high) 0 4992 4992
841  
842 4992 while (1) {
843  
844 18603 ▶ if (($high >> (BITS - 1)) == ($low >> (BITS - 1))) {
Branch: 2 branches
BranchTrueFalseTotal
if ($high >> 31 == $low >> 31) { } 10730 7873 18603
elsif (($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0) { } 2881 4992 7873
845  
846 10730 my $bit = $high >> (BITS - 1);
847 10730 $enc .= $bit;
848  
849 10730 ▶ if ($uf_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uf_count > 0) 1372 9358 10730
850 1372 $enc .= join('', 1 - $bit) x $uf_count;
851 1372 $uf_count = 0;
852 }
853  
854 10730 $low <<= 1;
855 10730 ($high <<= 1) |= 1;
856 }
857 ▶ elsif (((($low >> (BITS - 2)) & 0x1) == 1) && ((($high >> (BITS - 2)) & 0x1) == 0)) {
Condition100%3/3
($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0
!ll&&!rl&&r
375912332881
MC/DC100%2/2
($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0
($low >> 30 & 1) == 1 ($high >> 30 & 1) == 0
Truth table100%3/3
A and B
A= ($low >> 30 & 1) == 1B= ($high >> 30 & 1) == 0
ABresult
0X0
100
111
858 2881 ($high <<= 1) |= (1 << (BITS - 1));
859 2881 $high |= 1;
860 2881 ($low <<= 1) &= ((1 << (BITS - 1)) - 1);
861 2881 ++$uf_count;
862 }
863 else {
864 4992 last;
865 }
866  
867 13611 $low &= MAX;
868 13611 $high &= MAX;
869 }
870 }
871  
872 167 $enc .= '0';
873 167 $enc .= '1';
874  
875 167 ▶ while (length($enc) % 8 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($enc) % 8 != 0) 697 167 864
876 697 $enc .= '1';
877 }
878  
879 167 return ($enc, \@alphabet);
880 }
881  
882 180 ▶sub adaptive_ac_decode ($fh, $alphabet) {
Subroutine adaptive_ac_decode: called CC=15 SCAR=27.2
Pod documented
883  
884 180 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 13 167 180
885 13 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 13 0 13
886 13 return __SUB__->($fh2, $alphabet);
887 }
888  
889 167 my @dec;
890 167 my $low = 0;
891 167 my $high = MAX;
892  
893 167 my $alphabet_size = $#{$alphabet};
894 167 my ($freq, $cf, $T) = _create_adaptive_cfreq(INITIAL_FREQ, $alphabet_size);
895  
896 167 ▶ my $enc = oct('0b' . join '', map { getc($fh) // 1 } 1 .. BITS);
Condition100%2/2
getc $fh // 1
l!l
32322112
MC/DC100%1/1
getc $fh // 1
getc $fh
Truth table100%2/2
A
A= getc $fh
Aresult
11
00
897  
898 167 while (1) {
899 4992 my $w = ($high + 1) - $low;
900 4992 my $ss = int((($T * ($enc - $low + 1)) - 1) / $w);
901  
902 4992 my $i = 0;
903 4992 foreach my $j (0 .. $alphabet_size) {
904 45509 ▶ if ($cf->[$j] <= $ss and $ss < $cf->[$j + 1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cf->[$j] <= $ss and $ss < $$cf[$j + 1]) 4992 40517 45509
Condition66%2/3
$cf->[$j] <= $ss and $ss < $$cf[$j + 1]
!ll&&!rl&&r
0405174992
MC/DC50%1/2
$cf->[$j] <= $ss and $ss < $$cf[$j + 1]
$cf->[$j] <= $ss $ss < $$cf[$j + 1]
Truth table66%2/3
A and B
A= $cf->[$j] <= $ssB= $ss < $$cf[$j + 1]
ABresult
0X0
100
111
905 4992 $i = $j;
906 4992 last;
907 }
908 }
909  
910 4992 ▶ last if ($i == $alphabet_size);
Branch: 1 branch
BranchTrueFalseTotal
if $i == $alphabet_size 167 4825 4992
911 4825 push @dec, $alphabet->[$i];
912  
913 4825 $high = ($low + int(($w * $cf->[$i + 1]) / $T) - 1) & MAX;
914 4825 $low = ($low + int(($w * $cf->[$i]) / $T)) & MAX;
915  
916 4825 $T = _increment_freq($i, $alphabet_size, $freq, $cf);
917  
918 4825 ▶ if ($high > MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high > 4294967295) 0 4825 4825
919 0 confess "high > MAX: ($high > ${\MAX})";
920 }
921  
922 0 ▶ if ($low >= $high) { confess "$low >= $high" }
Branch: 1 branch
BranchTrueFalseTotal
if ($low >= $high) 0 4825 4825
923  
924 4825 while (1) {
925  
926 17904 ▶ if (($high >> (BITS - 1)) == ($low >> (BITS - 1))) {
Branch: 2 branches
BranchTrueFalseTotal
if ($high >> 31 == $low >> 31) { } 10312 7592 17904
elsif (($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0) { } 2767 4825 7592
927 10312 ($high <<= 1) |= 1;
928 10312 $low <<= 1;
929 10312 ▶ ($enc <<= 1) |= (getc($fh) // 1);
Condition100%2/2
getc $fh // 1
l!l
90661246
MC/DC100%1/1
getc $fh // 1
getc $fh
Truth table100%2/2
A
A= getc $fh
Aresult
11
00
930 }
931 ▶ elsif (((($low >> (BITS - 2)) & 0x1) == 1) && ((($high >> (BITS - 2)) & 0x1) == 0)) {
Condition100%3/3
($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0
!ll&&!rl&&r
366611592767
MC/DC100%2/2
($low >> 30 & 1) == 1 and ($high >> 30 & 1) == 0
($low >> 30 & 1) == 1 ($high >> 30 & 1) == 0
Truth table100%3/3
A and B
A= ($low >> 30 & 1) == 1B= ($high >> 30 & 1) == 0
ABresult
0X0
100
111
932 2767 ($high <<= 1) |= (1 << (BITS - 1));
933 2767 $high |= 1;
934 2767 ($low <<= 1) &= ((1 << (BITS - 1)) - 1);
935 2767 ▶ $enc = (($enc >> (BITS - 1)) << (BITS - 1)) | (($enc & ((1 << (BITS - 2)) - 1)) << 1) | (getc($fh) // 1);
Condition100%2/2
getc $fh // 1
l!l
2230537
MC/DC100%1/1
getc $fh // 1
getc $fh
Truth table100%2/2
A
A= getc $fh
Aresult
11
00
936 }
937 else {
938 4825 last;
939 }
940  
941 13079 $low &= MAX;
942 13079 $high &= MAX;
943 13079 $enc &= MAX;
944 }
945 }
946  
947 167 return \@dec;
948 }
949  
950 #####################
951 # Generic run-length
952 #####################
953  
954 4318 ▶sub run_length ($arr, $max_run = undef) {
Subroutine run_length: called CC=7 SCAR=19.5
Pod documented
955  
956 4318 ▶ @$arr || return [];
Branch: 1 branch
BranchTrueFalseTotal
@$arr or return [] 4181 137 4318
957  
958 4181 my @result = [$arr->[0], 1];
959 4181 my $prev_value = $arr->[0];
960  
961 4181 foreach my $i (1 .. $#$arr) {
962  
963 495973 my $curr_value = $arr->[$i];
964  
965 495973 ▶ if ($curr_value == $prev_value and (defined($max_run) ? $result[-1][1] < $max_run : 1)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($curr_value == $prev_value and defined $max_run ? $result[-1][1] < $max_run : 1) { } 458453 37520 495973
defined $max_run ? : 39 458416 458455
Condition100%3/3
$curr_value == $prev_value and defined $max_run ? $result[-1][1] < $max_run : 1
!ll&&!rl&&r
375182458453
MC/DC100%2/2
$curr_value == $prev_value and defined $max_run ? $result[-1][1] < $max_run : 1
$curr_value == $prev_value defined $max_run ? $result[-1][1] < $max_run : 1
Truth table100%3/3
A and B
A= $curr_value == $prev_valueB= defined $max_run ? $result[-1][1] < $max_run : 1
ABresult
0X0
100
111
966 458453 ++$result[-1][1];
967 }
968 else {
969 37520 push(@result, [$curr_value, 1]);
970 }
971  
972 495973 $prev_value = $curr_value;
973 }
974  
975 4181 return \@result;
976 }
977  
978 ######################################
979 # Binary variable run-length encoding
980 ######################################
981  
982 1 ▶sub binary_vrl_encode ($bitstring) {
Subroutine binary_vrl_encode: called CC=3 SCAR=11.0
Pod documented
983  
984 1 my @bits = split(//, $bitstring);
985 1 my $encoded = $bits[0];
986  
987 1 foreach my $rle (@{run_length(\@bits)}) {
988 23 my ($c, $v) = @$rle;
989  
990 23 ▶ if ($v == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v == 1) { } 13 10 23
991 13 $encoded .= '0';
992 }
993 else {
994 10 my $t = sprintf('%b', $v - 1);
995 10 $encoded .= join('', '1' x length($t), '0', substr($t, 1));
996 }
997 }
998  
999 1 return $encoded;
1000 }
1001  
1002 1 ▶sub binary_vrl_decode ($bitstring) {
Subroutine binary_vrl_decode: called CC=5 SCAR=16.1
Pod documented
1003  
1004 1 my $decoded = '';
1005 1 my $bit = substr($bitstring, 0, 1, '');
1006  
1007 1 ▶ while ($bitstring ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitstring ne "") 23 1 24
1008  
1009 23 $decoded .= $bit;
1010  
1011 23 my $bl = 0;
1012 23 ▶ while (substr($bitstring, 0, 1, '') eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($bitstring, 0, 1, '') eq 1) 25 23 48
1013 25 ++$bl;
1014 }
1015  
1016 23 ▶ if ($bl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bl > 0) 10 13 23
1017 10 $decoded .= $bit x oct('0b1' . join('', map { substr($bitstring, 0, 1, '') } 1 .. $bl - 1));
1018 }
1019  
1020 23 ▶ $bit = ($bit eq '1' ? '0' : '1');
Branch: 1 branch
BranchTrueFalseTotal
$bit eq '1' ? : 12 11 23
1021 }
1022  
1023 1 return $decoded;
1024 }
1025  
1026 ############################
1027 # Burrows-Wheeler transform
1028 ############################
1029  
1030 50 ▶sub bwt_sort ($s, $LOOKAHEAD_LEN = 128) { # O(n * LOOKAHEAD_LEN) space (fast)
Subroutine bwt_sort: called CC=10 SCAR=23.9
Pod documented
1031 50 my $len = length($s);
1032  
1033 50 ▶ return [0 .. $len - 1] if $len <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $len <= 1 2 48 50
1034  
1035 # Fast-path for uniform strings ("A" x 10000)
1036 48 ▶ return [0 .. $len - 1] if $s =~ /^(.)\1*\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /^(.)\1*\z/su 0 48 48
1037  
1038 48 my $double_s = $s . $s; # Pre-compute doubled string
1039  
1040 # Schwartzian transform with optimized tie-breaking
1041 return [
1042 65396 map { $_->[1] }
1043 sort {
1044 ($a->[0] cmp $b->[0])
1045 797880 ▶ || do {
Condition66%2/3
$Compression::Util::a->[0] cmp $Compression::Util::b->[0] || do { my $p1 = $Compression::Util::a->[1]; my $p2 = $Compression::Util::b->[1]; my $offset = $LOOKAHEAD_LEN; my $chunk = $LOOKAHEAD_LEN << 5; my $cmp = 0; while ($offset < $len) { my $rem = $len - $offset; my $curr_chunk = $chunk < $rem ? $chunk : $rem; $cmp = substr($double_s, $p1 + $offset, $curr_chunk) cmp substr($double_s, $p2 + $offset, $curr_chunk); last if $cmp; $offset += $curr_chunk; $chunk <<= 1; } ; $cmp or $p1 <=> $p2 }
l!l&&r!l&&!r
79332845520
MC/DC0%0/2
$Compression::Util::a->[0] cmp $Compression::Util::b->[0] || do { my $p1 = $Compression::Util::a->[1]; my $p2 = $Compression::Util::b->[1]; my $offset = $LOOKAHEAD_LEN; my $chunk = $LOOKAHEAD_LEN << 5; my $cmp = 0; while ($offset < $len) { my $rem = $len - $offset; my $curr_chunk = $chunk < $rem ? $chunk : $rem; $cmp = substr($double_s, $p1 + $offset, $curr_chunk) cmp substr($double_s, $p2 + $offset, $curr_chunk); last if $cmp; $offset += $curr_chunk; $chunk <<= 1; } ; $cmp or $p1 <=> $p2 }
$Compression::Util::a->[0] cmp $Compression::Util::b->[0] do { my $p1 = $Compression::Util::a->[1]; my $p2 = $Compression::Util::b->[1]; my $offset = $LOOKAHEAD_LEN; my $chunk = $LOOKAHEAD_LEN << 5; my $cmp = 0; while ($offset < $len) { my $rem = $len - $offset; my $curr_chunk = $chunk < $rem ? $chunk : $rem; $cmp = substr($double_s, $p1 + $offset, $curr_chunk) cmp substr($double_s, $p2 + $offset, $curr_chunk); last if $cmp; $offset += $curr_chunk; $chunk <<= 1; } ; $cmp or $p1 <=> $p2 }
Truth table66%2/3
A || B
A= $Compression::Util::a->[0] cmp $Compression::Util::b->[0]B= do { my $p1 = $Compression::Util::a->[1]; my $p2 = $Compression::Util::b->[1]; my $offset = $LOOKAHEAD_LEN; my $chunk = $LOOKAHEAD_LEN << 5; my $cmp = 0; while ($offset < $len) { my $rem = $len - $offset; my $curr_chunk = $chunk < $rem ? $chunk : $rem; $cmp = substr($double_s, $p1 + $offset, $curr_chunk) cmp substr($double_s, $p2 + $offset, $curr_chunk); last if $cmp; $offset += $curr_chunk; $chunk <<= 1; } ; $cmp or $p1 <=> $p2 }
ABresult
1X1
011
000
1046 4552 my $p1 = $a->[1];
1047 4552 my $p2 = $b->[1];
1048 4552 my $offset = $LOOKAHEAD_LEN;
1049 4552 my $chunk = $LOOKAHEAD_LEN << 5;
1050 4552 my $cmp = 0;
1051  
1052 # Compare remaining characters in exponentially growing chunks
1053 4552 ▶ while ($offset < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $len) 4552 0 4552
1054 4552 my $rem = $len - $offset;
1055 4552 ▶ my $curr_chunk = ($chunk < $rem) ? $chunk : $rem;
Branch: 1 branch
BranchTrueFalseTotal
$chunk < $rem ? : 4552 0 4552
1056  
1057 4552 $cmp = substr($double_s, $p1 + $offset, $curr_chunk) cmp substr($double_s, $p2 + $offset, $curr_chunk);
1058  
1059 4552 ▶ last if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 4552 0 4552
1060  
1061 0 $offset += $curr_chunk;
1062 0 $chunk <<= 1;
1063 }
1064  
1065 # If identical up to string length, tie-break by index for stability
1066 4552 ▶ $cmp || ($p1 <=> $p2);
Condition33%1/3
$cmp || $p1 <=> $p2
l!l&&r!l&&!r
455200
MC/DC0%0/2
$cmp || $p1 <=> $p2
$cmp $p1 <=> $p2
Truth table33%1/3
A || B
A= $cmpB= $p1 <=> $p2
ABresult
1X1
011
000
1067 }
1068 }
1069 map {
1070 48 my $pos = $_;
1071 65396 my $end = $pos + $LOOKAHEAD_LEN;
1072  
1073 # Handle wraparound efficiently
1074 65396 ▶ my $t =
Branch: 1 branch
BranchTrueFalseTotal
$end <= $len ? : 63693 1703 65396
1075 ($end <= $len)
1076 ? substr($s, $pos, $LOOKAHEAD_LEN)
1077 : substr($double_s, $pos, $LOOKAHEAD_LEN);
1078  
1079 65396 [$t, $pos]
1080 } 0 .. $len - 1
1081 ];
1082 }
1083  
1084 50 ▶sub bwt_encode ($s, $LOOKAHEAD_LEN = 128) {
Subroutine bwt_encode: called CC=5 SCAR=16.2
Pod documented
1085  
1086 50 ▶ if (ref($s) ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s ne "") 0 50 50
1087 0 return bwt_encode_symbolic($s);
1088 }
1089  
1090 50 my $bwt = bwt_sort($s, $LOOKAHEAD_LEN);
1091  
1092 50 my $ret = '';
1093 50 my $idx = 0;
1094  
1095 50 my $i = 0;
1096 50 foreach my $pos (@$bwt) {
1097 65397 $ret .= substr($s, $pos - 1, 1);
1098 65397 ▶ $idx = $i if !$pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos 49 65348 65397
1099 65397 ++$i;
1100 }
1101  
1102 50 return ($ret, $idx);
1103 }
1104  
1105 52 ▶sub bwt_decode ($bwt, $idx) {
Subroutine bwt_decode: called CC=5 SCAR=16.1
Pod documented
1106 52 my @L = unpack('C*', $bwt);
1107 52 my $n = scalar @L;
1108  
1109 52 my @freq = (0) x 256;
1110 52 $freq[$_]++ for @L;
1111  
1112 52 my @cumul = (0) x 257;
1113 52 $cumul[$_ + 1] = $cumul[$_] + $freq[$_] for 0 .. 255;
1114  
1115 52 my @next;
1116 52 my @cnt = (0) x 256;
1117 52 for my $i (0 .. $n - 1) {
1118 65550 $next[$cumul[$L[$i]] + $cnt[$L[$i]]++] = $i;
1119 }
1120  
1121 52 my @dec;
1122 52 my $i = $idx;
1123 52 for (1 .. $n) {
1124 65550 $i = $next[$i];
1125 65550 push @dec, $L[$i];
1126 }
1127  
1128 52 return pack('C*', @dec);
1129 }
1130  
1131 ##############################################
1132 # Burrows-Wheeler transform (symbolic variant)
1133 ##############################################
1134  
1135 55 ▶sub bwt_sort_symbolic ($s) { # O(n * log(n)^2)
Subroutine bwt_sort_symbolic: called CC=8 SCAR=20.8
Pod documented
1136 55 my @cyclic = @$s;
1137 55 my $len = scalar(@cyclic);
1138 55 ▶ return [0 .. $len - 1] if $len <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $len <= 1 13 42 55
1139  
1140 42 my @rank = @cyclic;
1141 42 my @sa = (0 .. $len - 1);
1142 42 my $k = 1;
1143  
1144 42 while (1) {
1145 117 my @tmp_rank = @rank;
1146 117 ▶ @sa = sort { $tmp_rank[$a] <=> $tmp_rank[$b] || $tmp_rank[($a + $k) % $len] <=> $tmp_rank[($b + $k) % $len] } @sa;
Condition100%3/3
$tmp_rank[$a] <=> $tmp_rank[$b] || $tmp_rank[($a + $k) % $len] <=> $tmp_rank[($b + $k) % $len]
l!l&&r!l&&!r
36247110663893
MC/DC100%2/2
$tmp_rank[$a] <=> $tmp_rank[$b] || $tmp_rank[($a + $k) % $len] <=> $tmp_rank[($b + $k) % $len]
$tmp_rank[$a] <=> $tmp_rank[$b] $tmp_rank[($a + $k) % $len] <=> $tmp_rank[($b + $k) % $len]
Truth table100%3/3
A || B
A= $tmp_rank[$a] <=> $tmp_rank[$b]B= $tmp_rank[($a + $k) % $len] <=> $tmp_rank[($b + $k) % $len]
ABresult
1X1
011
000
1147  
1148 117 my @new_rank;
1149 117 $new_rank[$sa[0]] = 0;
1150 117 for my $i (1 .. $#sa) {
1151 15795 my ($prev, $cur) = ($sa[$i - 1], $sa[$i]);
1152 15795 ▶ my $same = ($tmp_rank[$prev] == $tmp_rank[$cur])
Condition100%3/3
$tmp_rank[$prev] == $tmp_rank[$cur] && $tmp_rank[($prev + $k) % $len] == $tmp_rank[($cur + $k) % $len]
!ll&&!rl&&r
1006230582675
MC/DC100%2/2
$tmp_rank[$prev] == $tmp_rank[$cur] && $tmp_rank[($prev + $k) % $len] == $tmp_rank[($cur + $k) % $len]
$tmp_rank[$prev] == $tmp_rank[$cur] $tmp_rank[($prev + $k) % $len] == $tmp_rank[($cur + $k) % $len]
Truth table100%3/3
A && B
A= $tmp_rank[$prev] == $tmp_rank[$cur]B= $tmp_rank[($prev + $k) % $len] == $tmp_rank[($cur + $k) % $len]
ABresult
0X0
100
111
1153 && ($tmp_rank[($prev + $k) % $len] == $tmp_rank[($cur + $k) % $len]);
1154 15795 ▶ $new_rank[$cur] = $new_rank[$prev] + ($same ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$same ? : 2675 13120 15795
1155 }
1156 117 @rank = @new_rank;
1157  
1158 117 ▶ last if $rank[$sa[-1]] == $len - 1; # all ranks distinct — done
Branch: 1 branch
BranchTrueFalseTotal
if $rank[$sa[-1]] == $len - 1 42 75 117
1159 75 ▶ last if $k >= $len; # full cycle covered — remaining ties are truly equal
Branch: 1 branch
BranchTrueFalseTotal
if $k >= $len 0 75 75
1160 75 $k *= 2;
1161 }
1162 42 return \@sa;
1163 }
1164  
1165 55 ▶sub bwt_encode_symbolic ($symbols) {
Subroutine bwt_encode_symbolic: called CC=4 SCAR=14.0
Pod documented
1166  
1167 55 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 55 55
1168 0 $symbols = string2symbols($symbols);
1169 }
1170  
1171 55 my $bwt = bwt_sort_symbolic($symbols);
1172 55 my @ret = map { $symbols->[$_ - 1] } @$bwt;
1173  
1174 55 my $idx = 0;
1175 55 foreach my $i (@$bwt) {
1176 1253 ▶ $i || last;
Branch: 1 branch
BranchTrueFalseTotal
$i or last 1202 51 1253
1177 1202 ++$idx;
1178 }
1179  
1180 55 return (\@ret, $idx);
1181 }
1182  
1183 55 ▶sub bwt_decode_symbolic ($bwt, $idx) { # fast inversion
Subroutine bwt_decode_symbolic: called CC=4 SCAR=13.9
Pod documented
1184  
1185 55 my @head = sort { $a <=> $b } @$bwt;
1186  
1187 55 my %indices;
1188 55 foreach my $i (0 .. $#head) {
1189 3789 push @{$indices{$bwt->[$i]}}, $i;
1190 }
1191  
1192 55 my @table;
1193 55 foreach my $v (@head) {
1194 3789 push @table, shift(@{$indices{$v}});
1195 }
1196  
1197 55 my @dec;
1198 55 my $i = $idx;
1199  
1200 55 for (1 .. scalar(@head)) {
1201 3789 push @dec, $head[$i];
1202 3789 $i = $table[$i];
1203 }
1204  
1205 55 return \@dec;
1206 }
1207  
1208 #####################
1209 # RLE4 used in Bzip2
1210 #####################
1211  
1212 2312 ▶sub rle4_encode ($symbols, $max_run = 255) { # RLE1
Subroutine rle4_encode: called CC=11 SCAR=24.0
Pod documented
1213  
1214 2312 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 22 2290 2312
1215 22 $symbols = string2symbols($symbols);
1216 }
1217  
1218 2312 my $end = $#{$symbols};
1219 2312 ▶ return [] if ($end < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $end < 0 82 2230 2312
1220  
1221 2230 my $prev = $symbols->[0];
1222 2230 my $run = 1;
1223 2230 my @rle = ($prev);
1224  
1225 2230 ▶ for (my $i = 1 ; $i <= $end ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end) 121574 2230 123804
1226  
1227 121574 ▶ if ($symbols->[$i] == $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols->[$i] == $prev) { } 20212 101362 121574
1228 20212 ++$run;
1229 }
1230 else {
1231 101362 $run = 1;
1232 101362 $prev = $symbols->[$i];
1233 }
1234  
1235 121574 push @rle, $prev;
1236  
1237 121574 ▶ if ($run >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run >= 4) 3365 118209 121574
1238  
1239 3365 $run = 0;
1240 3365 $i += 1;
1241  
1242 3365 ▶ while ($run < $max_run and $i <= $end and $symbols->[$i] == $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run < $max_run and $i <= $end and $symbols->[$i] == $prev) 439119 3365 442484
Condition100%6/6
$run < $max_run and $i <= $end and $symbols->[$i] == $prev
!ll&&!rl&&r
1223243439119
$run < $max_run and $i <= $end
!ll&&!rl&&r
2498442362
MC/DC100%3/3
$run < $max_run and $i <= $end and $symbols->[$i] == $prev
$run < $max_run $i <= $end $symbols->[$i] == $prev
Truth table100%4/4
A and B and C
A= $run < $max_runB= $i <= $endC= $symbols->[$i] == $prev
ABCresult
0XX0
10X0
1100
1111
1243 439119 ++$run;
1244 439119 ++$i;
1245 }
1246  
1247 3365 push @rle, $run;
1248 3365 $run = 1;
1249  
1250 3365 ▶ if ($i <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end) 3267 98 3365
1251 3267 $prev = $symbols->[$i];
1252 3267 push @rle, $symbols->[$i];
1253 }
1254 }
1255 }
1256  
1257 2230 return \@rle;
1258 }
1259  
1260 255 ▶sub rle4_decode ($symbols) { # RLE1
Subroutine rle4_decode: called CC=7 SCAR=19.5
Pod documented
1261  
1262 255 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 255 255
1263 0 $symbols = string2symbols($symbols);
1264 }
1265  
1266 255 my $end = $#{$symbols};
1267 255 ▶ return [] if ($end < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $end < 0 14 241 255
1268  
1269 241 my @dec = $symbols->[0];
1270 241 my $prev = $symbols->[0];
1271 241 my $run = 1;
1272  
1273 241 ▶ for (my $i = 1 ; $i <= $end ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end) 101398 241 101639
1274  
1275 101398 ▶ if ($symbols->[$i] == $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols->[$i] == $prev) { } 13835 87563 101398
1276 13835 ++$run;
1277 }
1278 else {
1279 87563 $run = 1;
1280 87563 $prev = $symbols->[$i];
1281 }
1282  
1283 101398 push @dec, $prev;
1284  
1285 101398 ▶ if ($run >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run >= 4) 2024 99374 101398
1286 2024 ▶ if (++$i <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i <= $end) 2024 0 2024
1287 2024 $run = $symbols->[$i];
1288 2024 push @dec, (($prev) x $run);
1289 }
1290  
1291 2024 $run = 0;
1292 }
1293 }
1294  
1295 241 return \@dec;
1296 }
1297  
1298 #######################
1299 # Delta encoding (+RLE)
1300 #######################
1301  
1302 4136 ▶sub _compute_elias_costs ($run_length) {
Subroutine _compute_elias_costs: called CC=7 SCAR=19.5
1303  
1304 # Check which method results in better compression
1305 4136 my $with_rle = 0;
1306 4136 my $without_rle = 0;
1307  
1308 4136 my $double_with_rle = 0;
1309 4136 my $double_without_rle = 0;
1310  
1311 # Check if there are any negative values or zero values
1312 4136 my $has_negative = 0;
1313 4136 my $has_zero = 0;
1314  
1315 4136 foreach my $pair (@$run_length) {
1316 39965 my ($c, $v) = @$pair;
1317  
1318 39965 ▶ if ($c < 0 and not $has_negative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < 0 and not $has_negative) 908 39057 39965
Condition100%3/3
$c < 0 and not $has_negative
!ll&&!rl&&r
347494308908
MC/DC100%2/2
$c < 0 and not $has_negative
$c < 0 not $has_negative
Truth table100%3/3
A and not(B)
A= $c < 0B= not $has_negative
ABresult
0X0
100
111
1319 908 $has_negative = 1;
1320 }
1321  
1322 39965 ▶ if ($c == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c == 0) { } 7409 32556 39965
1323 7409 $with_rle += 1;
1324 7409 $double_with_rle += 1;
1325 7409 $without_rle += $v;
1326 7409 $double_without_rle += $v;
1327 7409 ▶ $has_zero ||= 1;
Condition100%2/2
$has_zero ||= 1
l!l
48682541
MC/DC100%1/1
$has_zero ||= 1
$has_zero
Truth table100%2/2
A
A= $has_zero
Aresult
11
00
1328 }
1329 else {
1330  
1331 { # double
1332 32556 my $t = int(log(abs($c) + 1) / log(2) + 1);
1333 32556 my $l = int(log($t) / log(2) + 1);
1334 32556 my $len = 2 * ($l - 1) + ($t - 1) + 3;
1335  
1336 32556 $double_with_rle += $len;
1337 32556 $double_without_rle += $len * $v;
1338 }
1339  
1340 { # single
1341 32556 my $t = int(log(abs($c) + 1) / log(2) + 1);
1342 32556 my $len = 2 * ($t - 1) + 3;
1343 32556 $with_rle += $len;
1344 32556 $without_rle += $len * $v;
1345 }
1346 }
1347  
1348 39965 ▶ if ($v == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v == 1) { } 33299 6666 39965
1349 33299 $with_rle += 1;
1350 33299 $double_with_rle += 1;
1351 }
1352 else {
1353 6666 my $t = int(log($v) / log(2) + 1);
1354 6666 my $len = 2 * ($t - 1) + 1;
1355 6666 $with_rle += $len;
1356 6666 $double_with_rle += $len;
1357 }
1358 }
1359  
1360 scalar {
1361 4136 has_negative => $has_negative,
1362 has_zero => $has_zero,
1363 methods => {
1364 with_rle => $with_rle,
1365 without_rle => $without_rle,
1366 double_with_rle => $double_with_rle,
1367 double_without_rle => $double_without_rle,
1368 },
1369 };
1370 }
1371  
1372 4136 ▶sub _find_best_encoding_method ($integers) {
Subroutine _find_best_encoding_method: called CC=2 SCAR=7.0
1373 4136 my $rl = run_length($integers);
1374 4136 my $costs = _compute_elias_costs($rl);
1375 4136 my ($best_method) = sort { $costs->{methods}{$a} <=> $costs->{methods}{$b} } sort keys(%{$costs->{methods}});
1376 4136 ▶ $VERBOSE && say STDERR "$best_method --> $costs->{methods}{$best_method}";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR qq[$best_method --> $costs->{"methods"}{$best_method}] 0 4136 4136
1377 4136 return ($rl, $best_method, $costs);
1378 }
1379  
1380 1034 ▶sub delta_encode ($integers) {
Subroutine delta_encode: called CC=17 SCAR=28.3
Pod documented
1381  
1382 1034 my $deltas = deltas($integers);
1383  
1384 1034 my @methods = (
1385 [_find_best_encoding_method($integers), 0, 0],
1386 [_find_best_encoding_method($deltas), 1, 0],
1387 [_find_best_encoding_method(rle4_encode($integers, scalar(@$integers) + 1)), 0, 1],
1388 [_find_best_encoding_method(rle4_encode($deltas, scalar(@$integers) + 1)), 1, 1],
1389 );
1390  
1391 1034 my ($best) = sort { $a->[2]{methods}{$a->[1]} <=> $b->[2]{methods}{$b->[1]} } @methods;
1392  
1393 1034 my ($rl, $method, $stats, $with_deltas, $with_rle4) = @$best;
1394  
1395 1034 my $double = 0;
1396 1034 my $with_rle = 0;
1397 1034 my $has_negative = $stats->{has_negative};
1398  
1399 1034 ▶ if ($method eq 'with_rle') {
Branch: 4 branches
BranchTrueFalseTotal
if ($method eq 'with_rle') { } 248 786 1034
elsif ($method eq 'without_rle') { } 407 379 786
elsif ($method eq 'double_with_rle') { } 68 311 379
elsif ($method eq 'double_without_rle') { } 311 0 311
1400 248 $with_rle = 1;
1401 }
1402 elsif ($method eq 'without_rle') {
1403 ## ok
1404 }
1405 elsif ($method eq 'double_with_rle') {
1406 68 $with_rle = 1;
1407 68 $double = 1;
1408 }
1409 elsif ($method eq 'double_without_rle') {
1410 311 $double = 1;
1411 }
1412 else {
1413 0 confess "[BUG] Unknown encoding method: $method";
1414 }
1415  
1416 1034 my $code = '';
1417 1034 my $bitstring = join('', $double, $with_rle, $has_negative, $with_deltas, $with_rle4);
1418 1034 ▶ my $length = sum(map { $_->[1] } @$rl) // 0;
Condition100%2/2
&sum(map({$_->[1];} @$rl)) // 0
l!l
100034
MC/DC100%1/1
&sum(map({$_->[1];} @$rl)) // 0
&sum(map({$_->[1];} @$rl))
Truth table100%2/2
A
A= &sum(map({$_->[1];} @$rl))
Aresult
11
00
1419  
1420 1034 foreach my $pair ([$length, 1], @$rl) {
1421 10086 my ($d, $v) = @$pair;
1422  
1423 10086 ▶ if ($d == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d == 0) { } 2138 7948 10086
elsif ($double) { } 2699 5249 7948
1424 2138 $code = '0';
1425 }
1426 elsif ($double) {
1427 2699 my $t = sprintf('%b', abs($d) + 1);
1428 2699 my $l = sprintf('%b', length($t));
1429 2699 ▶ $code = ($has_negative ? ('1' . (($d < 0) ? '0' : '1')) : '') . ('1' x (length($l) - 1)) . '0' . substr($l, 1) . substr($t, 1);
Branch: 2 branches
BranchTrueFalseTotal
$has_negative ? : 1122 1577 2699
$d < 0 ? : 396 726 1122
1430 }
1431 else {
1432 5249 ▶ my $t = sprintf('%b', abs($d) + ($has_negative ? 0 : 1));
Branch: 1 branch
BranchTrueFalseTotal
$has_negative ? : 2283 2966 5249
1433 5249 ▶ $code = ($has_negative ? ('1' . (($d < 0) ? '0' : '1')) : '') . ('1' x (length($t) - 1)) . '0' . substr($t, 1);
Branch: 2 branches
BranchTrueFalseTotal
$has_negative ? : 2283 2966 5249
$d < 0 ? : 912 1371 2283
1434 }
1435  
1436 10086 $bitstring .= $code;
1437  
1438 10086 ▶ if (not $with_rle) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($with_rle) 6573 3513 10086
1439 6573 ▶ if ($v > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v > 1) 630 5943 6573
1440 630 $bitstring .= $code x ($v - 1);
1441 }
1442 6573 next;
1443 }
1444  
1445 3513 ▶ if ($v == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v == 1) { } 2369 1144 3513
1446 2369 $bitstring .= '0';
1447 }
1448 else {
1449 1144 my $t = sprintf('%b', $v);
1450 1144 $bitstring .= join('', '1' x (length($t) - 1), '0', substr($t, 1));
1451 }
1452 }
1453  
1454 1034 pack('B*', $bitstring);
1455 }
1456  
1457 1008 ▶sub delta_decode ($fh) {
Subroutine delta_decode: called CC=22 SCAR=30.9
Pod documented
1458  
1459 1008 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 13 995 1008
1460 13 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 13 0 13
1461 13 return __SUB__->($fh2);
1462 }
1463  
1464 995 my $buffer = '';
1465 995 my $double = read_bit($fh, \$buffer);
1466 995 my $with_rle = read_bit($fh, \$buffer);
1467 995 my $has_negative = read_bit($fh, \$buffer);
1468 995 my $with_deltas = read_bit($fh, \$buffer);
1469 995 my $with_rle4 = read_bit($fh, \$buffer);
1470  
1471 995 my @deltas;
1472 995 my $len = 0;
1473  
1474 995 ▶ for (my $k = 0 ; $k <= $len ; ++$k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $len) 10906 995 11901
1475  
1476 10906 my $bit = read_bit($fh, \$buffer);
1477  
1478 10906 ▶ if ($bit eq '0') {
Branch: 2 branches
BranchTrueFalseTotal
if ($bit eq '0') { } 3170 7736 10906
elsif ($double) { } 2678 5058 7736
1479 3170 push @deltas, 0;
1480 }
1481 elsif ($double) {
1482 2678 ▶ my $bit = $has_negative ? read_bit($fh, \$buffer) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$has_negative ? : 1152 1526 2678
1483  
1484 2678 ▶ my $bl = $has_negative ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$has_negative ? : 1152 1526 2678
1485 2678 ▶ ++$bl while (read_bit($fh, \$buffer) eq '1');
Branch: 1 branch
BranchTrueFalseTotal
read_bit($fh, \$buffer) eq 1 and do { ++$bl } 3069 2678 5747
1486  
1487 2678 my $bl2 = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $bl));
1488 2678 my $int = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. ($bl2 - 1)));
1489  
1490 2678 ▶ push @deltas, ($has_negative ? ($bit eq '1' ? 1 : -1) : 1) * ($int - 1);
Branch: 2 branches
BranchTrueFalseTotal
$has_negative ? : 1152 1526 2678
$bit eq '1' ? : 755 397 1152
1491 }
1492 else {
1493 5058 ▶ my $bit = $has_negative ? read_bit($fh, \$buffer) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$has_negative ? : 2360 2698 5058
1494 5058 ▶ my $n = $has_negative ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$has_negative ? : 2360 2698 5058
1495 5058 ▶ ++$n while (read_bit($fh, \$buffer) eq '1');
Branch: 1 branch
BranchTrueFalseTotal
read_bit($fh, \$buffer) eq 1 and do { ++$n } 5585 5058 10643
1496 5058 my $d = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $n));
1497 5058 ▶ push @deltas, $has_negative ? ($bit eq '1' ? $d : -$d) : ($d - 1);
Branch: 2 branches
BranchTrueFalseTotal
$has_negative ? : 2360 2698 5058
$bit eq '1' ? : 1415 945 2360
1498 }
1499  
1500 10906 ▶ if ($with_rle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_rle) 3160 7746 10906
1501  
1502 3160 my $bl = 0;
1503 3160 ▶ while (read_bit($fh, \$buffer) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (read_bit($fh, \$buffer) == 1) 2912 3160 6072
1504 2912 ++$bl;
1505 }
1506  
1507 3160 ▶ if ($bl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bl > 0) 1059 2101 3160
1508 1059 my $run = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $bl)) - 1;
1509 1059 $k += $run;
1510 1059 push @deltas, ($deltas[-1]) x $run;
1511 }
1512 }
1513  
1514 10906 ▶ if ($k == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 0) 995 9911 10906
1515 995 $len = pop(@deltas);
1516 }
1517 }
1518  
1519 995 my $decoded = \@deltas;
1520 995 ▶ $decoded = rle4_decode($decoded) if $with_rle4;
Branch: 1 branch
BranchTrueFalseTotal
if $with_rle4 9 986 995
1521 995 ▶ $decoded = accumulate($decoded) if $with_deltas;
Branch: 1 branch
BranchTrueFalseTotal
if $with_deltas 471 524 995
1522 995 return $decoded;
1523 }
1524  
1525 ################################
1526 # Alphabet encoding (from Bzip2)
1527 ################################
1528  
1529 216 ▶sub encode_alphabet_256 ($alphabet) {
Subroutine encode_alphabet_256: called CC=8 SCAR=20.8
Pod documented
1530  
1531 216 my %table;
1532 216 @table{@$alphabet} = ();
1533  
1534 216 my $populated = 0;
1535 216 my @marked;
1536  
1537 216 ▶ for (my $i = 0 ; $i <= 255 ; $i += 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 255) 3456 216 3672
1538  
1539 3456 my $enc = 0;
1540 3456 foreach my $j (0 .. 15) {
1541 55296 ▶ if (exists($table{$i + $j})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $table{$i + $j}) 1627 53669 55296
1542 1627 $enc |= 1 << $j;
1543 }
1544 }
1545  
1546 3456 $populated <<= 1;
1547  
1548 3456 ▶ if ($enc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc > 0) 490 2966 3456
1549 490 $populated |= 1;
1550 490 push @marked, $enc;
1551 }
1552 }
1553  
1554 216 my $bitstring = join('', map { int2bits_lsb($_, 16) } @marked);
1555  
1556 216 ▶ $VERBOSE && say STDERR "Populated : ", sprintf('%016b', $populated);
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Populated : ", sprintf("%016b", $populated) 0 216 216
1557 216 ▶ $VERBOSE && say STDERR "Marked : @marked";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Marked : @marked" 0 216 216
1558 216 ▶ $VERBOSE && say STDERR "Bits len : ", length($bitstring);
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Bits len : ", length $bitstring 0 216 216
1559  
1560 216 my $encoded = '';
1561 216 $encoded .= int2bytes($populated, 2);
1562 216 $encoded .= pack('B*', $bitstring);
1563 216 return $encoded;
1564 }
1565  
1566 21 ▶sub decode_alphabet_256 ($fh) {
Subroutine decode_alphabet_256: called CC=7 SCAR=20.5
Pod documented
1567  
1568 21 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 0 21 21
1569 0 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 0 0 0
1570 0 return __SUB__->($fh2);
1571 }
1572  
1573 21 my @alphabet;
1574 21 my $l1 = bytes2int($fh, 2);
1575  
1576 21 for my $i (0 .. 15) {
1577 336 ▶ if ($l1 & (0x8000 >> $i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l1 & 32768 >> $i) 95 241 336
1578 95 my $l2 = bytes2int($fh, 2);
1579 95 for my $j (0 .. 15) {
1580 1520 ▶ if ($l2 & (0x8000 >> $j)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l2 & 32768 >> $j) 671 849 1520
1581 671 push @alphabet, 16 * $i + $j;
1582 }
1583 }
1584 }
1585 }
1586  
1587 21 return \@alphabet;
1588 }
1589  
1590 208 ▶sub encode_alphabet ($alphabet) {
Subroutine encode_alphabet: called CC=4 SCAR=13.9
Pod documented
1591  
1592 208 ▶ my $max_symbol = $alphabet->[-1] // -1;
Condition100%2/2
$alphabet->[-1] // -1
l!l
19513
MC/DC100%1/1
$alphabet->[-1] // -1
$alphabet->[-1]
Truth table100%2/2
A
A= $alphabet->[-1]
Aresult
11
00
1593  
1594 208 ▶ if ($max_symbol <= 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_symbol <= 255) 194 14 208
1595  
1596 194 my $delta = delta_encode($alphabet);
1597 194 my $enc = encode_alphabet_256($alphabet);
1598  
1599 194 ▶ if (length($delta) < length($enc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $delta < length $enc) 173 21 194
1600 173 return (chr(0) . $delta);
1601 }
1602  
1603 21 return (chr(1) . $enc);
1604 }
1605  
1606 14 return (chr(0) . delta_encode($alphabet));
1607 }
1608  
1609 199 ▶sub decode_alphabet ($fh) {
Subroutine decode_alphabet: called CC=5 SCAR=19.4
Pod documented
1610  
1611 199 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 0 199 199
1612 0 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 0 0 0
1613 0 return __SUB__->($fh2);
1614 }
1615  
1616 199 ▶ if (ord(getc($fh) // confess "error") == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord(getc $fh // confess("error")) == 1) 21 178 199
Condition50%1/2
getc $fh // confess("error")
l!l
1990
MC/DC0%0/1
getc $fh // confess("error")
getc $fh
Truth table50%1/2
A
A= getc $fh
Aresult
11
00
1617 21 return decode_alphabet_256($fh);
1618 }
1619  
1620 178 return delta_decode($fh);
1621 }
1622  
1623 ##########################
1624 # Move to front transform
1625 ##########################
1626  
1627 258 ▶sub mtf_encode ($symbols, $alphabet = undef) {
Subroutine mtf_encode: called CC=10 SCAR=23.1
Pod documented
1628  
1629 258 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 22 236 258
1630 22 $symbols = string2symbols($symbols);
1631 }
1632  
1633 258 ▶ if (defined($alphabet) and ref($alphabet) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alphabet and ref $alphabet eq "") 0 258 258
Condition66%2/3
defined $alphabet and ref $alphabet eq ""
!ll&&!rl&&r
25710
MC/DC0%0/2
defined $alphabet and ref $alphabet eq ""
defined $alphabet ref $alphabet eq ""
Truth table66%2/3
A and B
A= defined $alphabetB= ref $alphabet eq ""
ABresult
0X0
100
111
1634 0 $alphabet = string2symbols($alphabet);
1635 }
1636  
1637 258 my (@C, @table);
1638  
1639 258 my @alphabet;
1640 258 my @alphabet_copy;
1641 258 my $return_alphabet = 0;
1642  
1643 258 ▶ if (defined($alphabet)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alphabet) { } 1 257 258
1644 1 @alphabet = @$alphabet;
1645 }
1646 else {
1647 257 @alphabet = sort { $a <=> $b } uniq(@$symbols);
1648 257 $return_alphabet = 1;
1649 257 @alphabet_copy = @alphabet;
1650 }
1651  
1652 258 my $index;
1653 258 my @indices = (0 .. $#alphabet);
1654  
1655 258 foreach my $c (@$symbols) {
1656  
1657 117439 foreach my $i (@indices) {
1658 812221 ▶ if ($alphabet[$i] == $c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alphabet[$i] == $c) 117439 694782 812221
1659 117439 $index = $i;
1660 117439 last;
1661 }
1662 }
1663  
1664 117439 push @C, $index;
1665 117439 unshift(@alphabet, splice(@alphabet, $index, 1));
1666 }
1667  
1668 258 ▶ $return_alphabet || return \@C;
Branch: 1 branch
BranchTrueFalseTotal
$return_alphabet or return \@C 257 1 258
1669 257 return (\@C, \@alphabet_copy);
1670 }
1671  
1672 306 ▶sub mtf_decode ($encoded, $alphabet) {
Subroutine mtf_decode: called CC=4 SCAR=14.8
Pod documented
1673  
1674 306 ▶ if (ref($encoded) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $encoded eq "") 0 306 306
1675 0 $encoded = string2symbols($encoded);
1676 }
1677  
1678 306 ▶ if (ref($alphabet) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $alphabet eq "") 0 306 306
1679 0 $alphabet = string2symbols($alphabet);
1680 }
1681  
1682 306 my @S;
1683 306 my @alpha = @$alphabet;
1684  
1685 306 foreach my $p (@$encoded) {
1686 118900 push @S, $alpha[$p];
1687 118900 unshift(@alpha, splice(@alpha, $p, 1));
1688 }
1689  
1690 306 return \@S;
1691 }
1692  
1693 ###########################
1694 # Zero Run-length encoding
1695 ###########################
1696  
1697 243 ▶sub zrle_encode ($symbols) { # RLE2
Subroutine zrle_encode: called CC=7 SCAR=19.5
Pod documented
1698  
1699 243 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 243 243
1700 0 $symbols = string2symbols($symbols);
1701 }
1702  
1703 243 my @rle;
1704 243 my $end = $#{$symbols};
1705  
1706 243 ▶ for (my $i = 0 ; $i <= $end ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end) 45793 243 46036
1707  
1708 45793 my $run = 0;
1709 45793 ▶ while ($i <= $end and $symbols->[$i] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end and $symbols->[$i] == 0) 70682 45793 116475
Condition100%3/3
$i <= $end and $symbols->[$i] == 0
!ll&&!rl&&r
1144567970682
MC/DC100%2/2
$i <= $end and $symbols->[$i] == 0
$i <= $end $symbols->[$i] == 0
Truth table100%3/3
A and B
A= $i <= $endB= $symbols->[$i] == 0
ABresult
0X0
100
111
1710 70682 ++$run;
1711 70682 ++$i;
1712 }
1713  
1714 45793 ▶ if ($run >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run >= 1) 9189 36604 45793
1715 9189 my $t = sprintf('%b', $run + 1);
1716 9189 push @rle, split(//, substr($t, 1));
1717 }
1718  
1719 45793 ▶ if ($i <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end) 45679 114 45793
1720 45679 push @rle, $symbols->[$i] + 1;
1721 }
1722 }
1723  
1724 243 return \@rle;
1725 }
1726  
1727 245 ▶sub zrle_decode ($rle) { # RLE2
Subroutine zrle_decode: called CC=9 SCAR=22.0
Pod documented
1728  
1729 245 ▶ if (ref($rle) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rle eq "") 0 245 245
1730 0 $rle = string2symbols($rle);
1731 }
1732  
1733 245 my @dec;
1734 245 my $end = $#{$rle};
1735  
1736 245 ▶ for (my $i = 0 ; $i <= $end ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end) 45887 245 46132
1737 45887 my $k = $rle->[$i];
1738  
1739 45887 ▶ if ($k == 0 or $k == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 0 or $k == 1) 9180 36707 45887
Condition100%3/3
$k == 0 or $k == 1
l!l&&r!l&&!r
6966221436707
MC/DC100%2/2
$k == 0 or $k == 1
$k == 0 $k == 1
Truth table100%3/3
A or B
A= $k == 0B= $k == 1
ABresult
1X1
011
000
1740 9180 my $run = 1;
1741 9180 ▶ while (($i <= $end) and ($k == 0 or $k == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end and $k == 0 || $k == 1) 16399 9180 25579
Condition100%6/6
$i <= $end and $k == 0 || $k == 1
!ll&&!rl&&r
115906516399
$k == 0 or $k == 1
l!l&&r!l&&!r
1146349369065
MC/DC100%3/3
$i <= $end and $k == 0 || $k == 1
$i <= $end $k == 0 $k == 1
Truth table100%4/4
A and B or C
A= $i <= $endB= $k == 0C= $k == 1
ABCresult
0XX0
1000
11X1
1011
1742 16399 ($run <<= 1) |= $k;
1743 16399 $k = $rle->[++$i];
1744 }
1745 9180 push @dec, (0) x ($run - 1);
1746 }
1747  
1748 45887 ▶ if ($i <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $end) 45772 115 45887
1749 45772 push @dec, $k - 1;
1750 }
1751 }
1752  
1753 245 return \@dec;
1754 }
1755  
1756 ################################################################
1757 # Move-to-front compression (MTF + RLE4 + ZRLE + Huffman coding)
1758 ################################################################
1759  
1760 125 ▶sub mrl_compress_symbolic ($symbols, $entropy_sub = \&create_huffman_entry) {
Subroutine mrl_compress_symbolic: called CC=3 SCAR=11.0
Pod documented
1761  
1762 125 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 21 104 125
1763 21 $symbols = string2symbols($symbols);
1764 }
1765  
1766 125 my ($mtf, $alphabet) = mtf_encode($symbols);
1767 125 my $rle = zrle_encode($mtf);
1768 125 my $rle4 = rle4_encode($rle, scalar(@$rle));
1769  
1770 125 encode_alphabet($alphabet) . $entropy_sub->($rle4);
1771 }
1772  
1773 48 *mrl_compress = \&mrl_compress_symbolic;
1774  
1775 234 ▶sub mrl_decompress_symbolic ($fh, $entropy_sub = \&decode_huffman_entry) {
Subroutine mrl_decompress_symbolic: called CC=5 SCAR=16.2
Pod documented
1776  
1777 234 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 109 125 234
1778 109 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 109 0 109
1779 109 return __SUB__->($fh2, $entropy_sub);
1780 }
1781  
1782 125 my $alphabet = decode_alphabet($fh);
1783  
1784 125 ▶ $VERBOSE && say STDERR "Alphabet size: ", scalar(@$alphabet);
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Alphabet size: ", scalar @$alphabet 0 125 125
1785  
1786 125 my $rle4 = $entropy_sub->($fh);
1787 125 my $rle = rle4_decode($rle4);
1788 125 my $mtf = zrle_decode($rle);
1789 125 my $symbols = mtf_decode($mtf, $alphabet);
1790  
1791 125 return $symbols;
1792 }
1793  
1794 10 ▶sub mrl_decompress($fh, $entropy_sub = \&decode_huffman_entry) {
Subroutine mrl_decompress: called CC=2 SCAR=6.9
Pod documented
1795 10 symbols2string(mrl_decompress_symbolic($fh, $entropy_sub));
1796 }
1797  
1798 ############################################################
1799 # BWT-based compression (BWT + MTF + ZRLE + Huffman coding)
1800 ############################################################
1801  
1802 28 ▶sub bwt_compress ($chunk, $entropy_sub = \&create_huffman_entry) {
Subroutine bwt_compress: called CC=4 SCAR=14.3
Pod documented
1803  
1804 28 ▶ if (ref($chunk) ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $chunk ne "") 0 28 28
1805 0 return bwt_compress_symbolic($chunk, $entropy_sub);
1806 }
1807  
1808 28 my $rle1 = rle4_encode(string2symbols($chunk));
1809 28 my ($bwt, $idx) = bwt_encode(pack('C*', @$rle1));
1810  
1811 28 ▶ $VERBOSE && say STDERR "BWT index = $idx";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "BWT index = $idx" 0 28 28
1812  
1813 28 my ($mtf, $alphabet) = mtf_encode(string2symbols($bwt));
1814 28 my $rle = zrle_encode($mtf);
1815  
1816 28 pack('N', $idx) . encode_alphabet($alphabet) . $entropy_sub->($rle);
1817 }
1818  
1819 37 ▶sub bwt_decompress ($fh, $entropy_sub = \&decode_huffman_entry) {
Subroutine bwt_decompress: called CC=6 SCAR=18.1
Pod documented
1820  
1821 37 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 18 19 37
1822 18 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 18 0 18
1823 18 return __SUB__->($fh2, $entropy_sub);
1824 }
1825  
1826 19 my $idx = bytes2int($fh, 4);
1827 19 my $alphabet = decode_alphabet($fh);
1828  
1829 19 ▶ $VERBOSE && say STDERR "BWT index = $idx";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "BWT index = $idx" 0 19 19
1830 19 ▶ $VERBOSE && say STDERR "Alphabet size: ", scalar(@$alphabet);
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Alphabet size: ", scalar @$alphabet 0 19 19
1831  
1832 19 my $rle = $entropy_sub->($fh);
1833 19 my $mtf = zrle_decode($rle);
1834 19 my $bwt = mtf_decode($mtf, $alphabet);
1835 19 my $rle4 = bwt_decode(pack('C*', @$bwt), $idx);
1836 19 my $data = rle4_decode(string2symbols($rle4));
1837  
1838 19 pack('C*', @$data);
1839 }
1840  
1841 ###########################################
1842 # BWT-based compression (symbolic variant)
1843 ###########################################
1844  
1845 55 ▶sub bwt_compress_symbolic ($symbols, $entropy_sub = \&create_huffman_entry) {
Subroutine bwt_compress_symbolic: called CC=6 SCAR=18.6
Pod documented
1846  
1847 55 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 1 54 55
1848 1 $symbols = string2symbols($symbols);
1849 }
1850  
1851 55 my $rle4 = rle4_encode($symbols);
1852 55 my ($bwt, $idx) = bwt_encode_symbolic($rle4);
1853  
1854 55 my ($mtf, $alphabet) = mtf_encode($bwt);
1855 55 my $rle = zrle_encode($mtf);
1856  
1857 55 ▶ $VERBOSE && say STDERR "BWT index = $idx";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "BWT index = $idx" 0 55 55
1858 55 ▶ $VERBOSE && say STDERR "Max symbol: ", max(@$alphabet) // 0;
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Max symbol: ", &max(@$alphabet) // 0 0 55 55
Condition0%0/2
&max(@$alphabet) // 0
l!l
00
MC/DC0%0/1
&max(@$alphabet) // 0
&max(@$alphabet)
Truth table0%0/2
A
A= &max(@$alphabet)
Aresult
11
00
1859  
1860 55 pack('N', $idx) . encode_alphabet($alphabet) . $entropy_sub->($rle);
1861 }
1862  
1863 104 ▶sub bwt_decompress_symbolic ($fh, $entropy_sub = \&decode_huffman_entry) {
Subroutine bwt_decompress_symbolic: called CC=6 SCAR=18.1
Pod documented
1864  
1865 104 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 49 55 104
1866 49 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 49 0 49
1867 49 return __SUB__->($fh2, $entropy_sub);
1868 }
1869  
1870 55 my $idx = bytes2int($fh, 4);
1871 55 my $alphabet = decode_alphabet($fh);
1872  
1873 55 ▶ $VERBOSE && say STDERR "BWT index = $idx";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "BWT index = $idx" 0 55 55
1874 55 ▶ $VERBOSE && say STDERR "Alphabet size: ", scalar(@$alphabet);
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Alphabet size: ", scalar @$alphabet 0 55 55
1875  
1876 55 my $rle = $entropy_sub->($fh);
1877 55 my $mtf = zrle_decode($rle);
1878 55 my $bwt = mtf_decode($mtf, $alphabet);
1879 55 my $rle4 = bwt_decode_symbolic($bwt, $idx);
1880 55 my $data = rle4_decode($rle4);
1881  
1882 55 return $data;
1883 }
1884  
1885 ###########################
1886 # Arithmetic Coding entries
1887 ###########################
1888  
1889 66 ▶sub create_ac_entry ($symbols) {
Subroutine create_ac_entry: called CC=5 SCAR=16.5
Pod documented
1890  
1891 66 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 66 66
1892 0 $symbols = string2symbols($symbols);
1893 }
1894  
1895 66 my ($enc, $freq) = ac_encode($symbols);
1896 66 ▶ my $max_symbol = max(keys %$freq) // 0;
Condition50%1/2
&max(keys %$freq) // 0
l!l
660
MC/DC0%0/1
&max(keys %$freq) // 0
&max(keys %$freq)
Truth table50%1/2
A
A= &max(keys %$freq)
Aresult
11
00
1897  
1898 66 my @freqs;
1899 66 foreach my $k (0 .. $max_symbol) {
1900 42165 ▶ push @freqs, $freq->{$k} // 0;
Condition100%2/2
$freq->{$k} // 0
l!l
75141414
MC/DC100%1/1
$freq->{$k} // 0
$freq->{$k}
Truth table100%2/2
A
A= $freq->{$k}
Aresult
11
00
1901 }
1902  
1903 66 push @freqs, length($enc) >> 3;
1904  
1905 66 delta_encode(\@freqs) . pack("B*", $enc);
1906 }
1907  
1908 67 ▶sub decode_ac_entry ($fh) {
Subroutine decode_ac_entry: called CC=7 SCAR=19.7
Pod documented
1909  
1910 67 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 1 66 67
1911 1 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 1 0 1
1912 1 return __SUB__->($fh2);
1913 }
1914  
1915 66 my @freqs = @{delta_decode($fh)};
1916 66 my $bits_len = pop(@freqs);
1917  
1918 66 my %freq;
1919 66 foreach my $i (0 .. $#freqs) {
1920 42165 ▶ if ($freqs[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($freqs[$i]) 751 41414 42165
1921 751 $freq{$i} = $freqs[$i];
1922 }
1923 }
1924  
1925 66 ▶ $VERBOSE && say STDERR "Encoded length: $bits_len";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Encoded length: $bits_len" 0 66 66
1926 66 my $bits = read_bits($fh, $bits_len << 3);
1927  
1928 66 ▶ if ($bits_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits_len > 0) 66 0 66
1929 66 open my $bits_fh, '<:raw', \$bits;
1930 66 return ac_decode($bits_fh, \%freq);
1931 }
1932  
1933 0 return [];
1934 }
1935  
1936 ####################################
1937 # Adaptive Arithmetic Coding entries
1938 ####################################
1939  
1940 154 ▶sub create_adaptive_ac_entry ($symbols) {
Subroutine create_adaptive_ac_entry: called CC=2 SCAR=7.6
Pod documented
1941  
1942 154 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 154 154
1943 0 $symbols = string2symbols($symbols);
1944 }
1945  
1946 154 my ($enc, $alphabet) = adaptive_ac_encode($symbols);
1947 154 delta_encode([@$alphabet, length($enc) >> 3]) . pack('B*', $enc);
1948 }
1949  
1950 155 ▶sub decode_adaptive_ac_entry ($fh) {
Subroutine decode_adaptive_ac_entry: called CC=4 SCAR=14.1
Pod documented
1951  
1952 155 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 1 154 155
1953 1 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 1 0 1
1954 1 return __SUB__->($fh2);
1955 }
1956  
1957 154 my $alphabet = delta_decode($fh);
1958 154 my $enc_len = pop(@$alphabet);
1959  
1960 154 ▶ if ($enc_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc_len > 0) 154 0 154
1961 154 my $bits = read_bits($fh, $enc_len << 3);
1962 154 open my $bits_fh, '<:raw', \$bits;
1963 154 return adaptive_ac_decode($bits_fh, $alphabet);
1964 }
1965  
1966 0 return [];
1967 }
1968  
1969 ###########################
1970 # Huffman Coding algorithm
1971 ###########################
1972  
1973 492 ▶sub huffman_encode ($symbols, $dict) {
Subroutine huffman_encode: called CC=1 SCAR=0.0
Pod documented
1974 492 join('', @{$dict}{@$symbols});
1975 }
1976  
1977 411 ▶sub _build_trie ($rev_dict) {
Subroutine _build_trie: called CC=4 SCAR=13.9
1978 411 my $root = {};
1979 411 for my $code (keys %$rev_dict) {
1980 4032 my $node = $root;
1981 4032 for my $bit (split //, $code) {
1982 23311 ▶ $node->{$bit} //= {};
Condition100%2/2
$node->{$bit} //= {}
l!l
159407371
MC/DC100%1/1
$node->{$bit} //= {}
$node->{$bit}
Truth table100%2/2
A
A= $node->{$bit}
Aresult
11
00
1983 23311 $node = $node->{$bit};
1984 }
1985 4032 $node->{sym} = $rev_dict->{$code};
1986 }
1987 411 return $root;
1988 }
1989  
1990 411 ▶sub huffman_decode ($bits, $rev_dict) {
Subroutine huffman_decode: called CC=3 SCAR=11.0
Pod documented
1991 411 my $root = _build_trie($rev_dict);
1992 411 my @result;
1993 411 my $node = $root;
1994 411 foreach my $i (0 .. length($bits) - 1) {
1995 392538 $node = $node->{substr($bits, $i, 1)};
1996 392538 ▶ if (exists $node->{sym}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{"sym"}) 79454 313084 392538
1997 79454 push @result, $node->{sym};
1998 79454 $node = $root;
1999 }
2000 }
2001 411 return \@result;
2002 }
2003  
2004 # produce encode and decode dictionary from a tree
2005 12061 ▶sub _huffman_walk_tree ($node, $code, $h) {
Subroutine _huffman_walk_tree: called CC=4 SCAR=13.9
2006  
2007 12061 ▶ my $c = $node->[0] // return $h;
Condition100%2/2
$node->[0] // (return $h)
l!l
11836225
MC/DC100%1/1
$node->[0] // (return $h)
$node->[0]
Truth table100%2/2
A
A= $node->[0]
Aresult
11
00
2008 11836 ▶ if (ref $c) { __SUB__->($c->[$_], $code . $_, $h) for ('0', '1') }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) { } 5649 6187 11836
2009 6187 else { $h->{$c} = $code }
2010  
2011 11836 return $h;
2012 }
2013  
2014 1953 ▶sub huffman_from_code_lengths ($code_lengths_table) {
Subroutine huffman_from_code_lengths: called CC=12 SCAR=25.1
Pod documented
2015  
2016 1953 ▶ if (ref($code_lengths_table) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code_lengths_table eq "ARRAY") 595 1358 1953
2017 595 ▶ my %table = map { (($code_lengths_table->[$_] > 0) ? ($_, $code_lengths_table->[$_]) : ()) } 0 .. $#{$code_lengths_table};
Branch: 1 branch
BranchTrueFalseTotal
$code_lengths_table->[$_] > 0 ? : 7309 175299 182608
2018 595 return __SUB__->(\%table);
2019 }
2020  
2021 # This algorithm is based on the pseudocode in RFC 1951 (Section 3.2.2)
2022 # (Steps are numbered as in the RFC)
2023  
2024 1358 my @code_lengths = map { [$_, $code_lengths_table->{$_}] } sort { $a <=> $b } keys %$code_lengths_table;
2025  
2026 # Step 1: Count the number of codes for each length
2027 1358 ▶ my $max_length = max(map { $_->[1] } @code_lengths) // 0;
Condition100%2/2
&max(map({$_->[1];} @code_lengths)) // 0
l!l
1203155
MC/DC100%1/1
&max(map({$_->[1];} @code_lengths)) // 0
&max(map({$_->[1];} @code_lengths))
Truth table100%2/2
A
A= &max(map({$_->[1];} @code_lengths))
Aresult
11
00
2028 1358 my @length_counts = (0) x ($max_length + 1);
2029  
2030 1358 foreach my $length (map { $_->[1] } @code_lengths) {
2031  
2032 # Treat undef or negative lengths as 0 (unused)
2033 13496 ▶ if (defined($length) and $length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length and $length > 0) 13496 0 13496
Condition33%1/3
defined $length and $length > 0
!ll&&!rl&&r
0013496
MC/DC0%0/2
defined $length and $length > 0
defined $length $length > 0
Truth table33%1/3
A and B
A= defined $lengthB= $length > 0
ABresult
0X0
100
111
2034 13496 ++$length_counts[$length];
2035 }
2036 }
2037  
2038 # Step 2: Generate the starting numerical value for each length
2039 1358 my $code = 0;
2040 1358 $length_counts[0] = 0;
2041 1358 my @next_code = (0) x ($max_length + 1);
2042  
2043 1358 foreach my $bits (1 .. $max_length) {
2044 3733 $code = ($code + $length_counts[$bits - 1]) << 1;
2045 3733 $next_code[$bits] = $code;
2046 }
2047  
2048 # Step 3: Assign numerical values to all codes
2049 1358 my %dict;
2050 my %rev_dict;
2051 1358 foreach my $pair (@code_lengths) {
2052 13496 my ($key, $length) = @$pair;
2053  
2054 # Skip zero-length codes (unused symbols)
2055 13496 ▶ if (defined($length) and $length != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length and $length != 0) 13496 0 13496
Condition33%1/3
defined $length and $length != 0
!ll&&!rl&&r
0013496
MC/DC0%0/2
defined $length and $length != 0
defined $length $length != 0
Truth table33%1/3
A and B
A= defined $lengthB= $length != 0
ABresult
0X0
100
111
2056  
2057 # Format the integer code as a binary string with $length bits
2058 13496 my $binary_code = sprintf('%0*b', $length, $next_code[$length]);
2059  
2060 13496 $dict{$key} = $binary_code;
2061 13496 $rev_dict{$binary_code} = $key;
2062  
2063 # Increment the code for the next symbol of this length
2064 13496 ++$next_code[$length];
2065 }
2066 }
2067  
2068 1358 ▶ return (wantarray ? (\%dict, \%rev_dict) : \%dict);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 866 492 1358
2069 }
2070  
2071 11691 ▶sub _heap_push ($heap, $item) {
Subroutine _heap_push: called CC=3 SCAR=11.0
2072 11691 push @$heap, $item;
2073 11691 my $i = $#$heap;
2074 11691 ▶ while ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 15636 1908 17544
2075 15636 my $p = ($i - 1) >> 1;
2076 15636 ▶ last if ($heap->[$p][1] <= $heap->[$i][1]);
Branch: 1 branch
BranchTrueFalseTotal
if $heap->[$p][1] <= $heap->[$i][1] 9783 5853 15636
2077 5853 @{$heap}[$p, $i] = @{$heap}[$i, $p];
2078 5853 $i = $p;
2079 }
2080 }
2081  
2082 11008 ▶sub _heap_pop ($heap) {
Subroutine _heap_pop: called CC=7 SCAR=19.5
2083 11008 ▶ return pop @$heap if (@$heap == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @$heap == 1 538 10470 11008
2084 10470 my $top = $heap->[0];
2085 10470 $heap->[0] = pop @$heap;
2086 10470 my $n = scalar @$heap;
2087 10470 my $i = 0;
2088 10470 while (1) {
2089 38429 my $s = $i;
2090 38429 my $l = 2 * $i + 1;
2091 38429 my $r = $l + 1;
2092 38429 ▶ $s = $l if ($l < $n && $heap->[$l][1] < $heap->[$s][1]);
Branch: 1 branch
BranchTrueFalseTotal
if $l < $n and $heap->[$l][1] < $heap->[$s][1] 25326 13103 38429
Condition100%3/3
$l < $n and $heap->[$l][1] < $heap->[$s][1]
!ll&&!rl&&r
7555554825326
MC/DC100%2/2
$l < $n and $heap->[$l][1] < $heap->[$s][1]
$l < $n $heap->[$l][1] < $heap->[$s][1]
Truth table100%3/3
A and B
A= $l < $nB= $heap->[$l][1] < $heap->[$s][1]
ABresult
0X0
100
111
2093 38429 ▶ $s = $r if ($r < $n && $heap->[$r][1] < $heap->[$s][1]);
Branch: 1 branch
BranchTrueFalseTotal
if $r < $n and $heap->[$r][1] < $heap->[$s][1] 12340 26089 38429
Condition100%3/3
$r < $n and $heap->[$r][1] < $heap->[$s][1]
!ll&&!rl&&r
90991699012340
MC/DC100%2/2
$r < $n and $heap->[$r][1] < $heap->[$s][1]
$r < $n $heap->[$r][1] < $heap->[$s][1]
Truth table100%3/3
A and B
A= $r < $nB= $heap->[$r][1] < $heap->[$s][1]
ABresult
0X0
100
111
2094 38429 ▶ last if $s == $i;
Branch: 1 branch
BranchTrueFalseTotal
if $s == $i 10470 27959 38429
2095 27959 @{$heap}[$i, $s] = @{$heap}[$s, $i];
2096 27959 $i = $s;
2097 }
2098 10470 return $top;
2099 }
2100  
2101 763 ▶sub huffman_from_freq($freq) {
Subroutine huffman_from_freq: called CC=6 SCAR=17.9
Pod documented
2102  
2103 # Initialize Heap
2104 # Structure: [ [symbol_or_children], frequency ]
2105 763 my @heap;
2106 763 foreach my $k (sort { $a <=> $b } keys %$freq) {
2107 6187 _heap_push(\@heap, [$k, $freq->{$k}]);
2108 }
2109  
2110 # Build Huffman Tree
2111 763 ▶ while (@heap > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@heap > 1) 5504 763 6267
2112 5504 my $x = _heap_pop(\@heap);
2113 5504 my $y = _heap_pop(\@heap);
2114 5504 _heap_push(\@heap, [[$x, $y], $x->[1] + $y->[1]]);
2115 }
2116  
2117 763 ▶ if (@heap == 1 && !ref $heap[0][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@heap == 1 and not ref $heap[0][0]) 145 618 763
Condition100%3/3
@heap == 1 and not ref $heap[0][0]
!ll&&!rl&&r
80538145
MC/DC100%2/2
@heap == 1 and not ref $heap[0][0]
@heap == 1 not ref $heap[0][0]
Truth table100%3/3
A and not(B)
A= @heap == 1B= not ref $heap[0][0]
ABresult
0X0
100
111
2118 145 @heap = ([[$heap[0]], $heap[0][1]]);
2119 }
2120  
2121 # Generate Codes
2122 763 my $h = _huffman_walk_tree($heap[0], '', {});
2123  
2124 763 my %code_lengths;
2125 763 foreach my $i (keys %$freq) {
2126 6187 $code_lengths{$i} = length($h->{$i});
2127 }
2128  
2129 763 huffman_from_code_lengths(\%code_lengths);
2130 }
2131  
2132 680 ▶sub huffman_from_symbols ($symbols) {
Subroutine huffman_from_symbols: called CC=2 SCAR=8.1
Pod documented
2133  
2134 680 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 680 680
2135 0 $symbols = string2symbols($symbols);
2136 }
2137  
2138 680 huffman_from_freq(frequencies($symbols));
2139 }
2140  
2141 ########################
2142 # Huffman Coding entries
2143 ########################
2144  
2145 492 ▶sub create_huffman_entry ($symbols) {
Subroutine create_huffman_entry: called CC=6 SCAR=18.1
Pod documented
2146  
2147 492 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 492 492
2148 0 $symbols = string2symbols($symbols);
2149 }
2150  
2151 492 my $dict = huffman_from_symbols($symbols);
2152 492 my $enc = huffman_encode($symbols, $dict);
2153  
2154 492 ▶ my $max_symbol = max(keys %$dict) // 0;
Condition100%2/2
&max(keys %$dict) // 0
l!l
42072
MC/DC100%1/1
&max(keys %$dict) // 0
&max(keys %$dict)
Truth table100%2/2
A
A= &max(keys %$dict)
Aresult
11
00
2155 492 ▶ $VERBOSE && say STDERR "Max symbol: $max_symbol\n";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Max symbol: $max_symbol\n" 0 492 492
2156  
2157 492 my @code_lengths;
2158 492 foreach my $i (0 .. $max_symbol) {
2159 176611 ▶ if (exists($dict->{$i})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dict->{$i}) { } 4076 172535 176611
2160 4076 $code_lengths[$i] = length($dict->{$i});
2161 }
2162 else {
2163 172535 $code_lengths[$i] = 0;
2164 }
2165 }
2166  
2167 492 delta_encode(\@code_lengths) . pack("N", length($enc)) . pack("B*", $enc);
2168 }
2169  
2170 484 ▶sub decode_huffman_entry ($fh) {
Subroutine decode_huffman_entry: called CC=5 SCAR=16.2
Pod documented
2171  
2172 484 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 1 483 484
2173 1 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 1 0 1
2174 1 return __SUB__->($fh2);
2175 }
2176  
2177 483 my $code_lengths = delta_decode($fh);
2178 483 my (undef, $rev_dict) = huffman_from_code_lengths($code_lengths);
2179  
2180 483 my $enc_len = bytes2int($fh, 4);
2181 483 ▶ $VERBOSE && say STDERR "Encoded length: $enc_len\n";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Encoded length: $enc_len\n" 0 483 483
2182  
2183 483 ▶ if ($enc_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc_len > 0) 411 72 483
2184 411 return huffman_decode(read_bits($fh, $enc_len), $rev_dict);
2185 }
2186  
2187 72 return [];
2188 }
2189  
2190 ###################################################################################
2191 # DEFLATE-like encoding of literals and backreferences produced by the LZSS methods
2192 ###################################################################################
2193  
2194 714 ▶sub make_deflate_tables ($max_dist = $LZ_MAX_DIST, $max_len = $LZ_MAX_LEN) {
Subroutine make_deflate_tables: called CC=9 SCAR=22.0
Pod documented
2195  
2196 # [distance value, offset bits]
2197 714 my @DISTANCE_SYMBOLS = map { [$_, 0] } (0 .. 4);
2198  
2199 714 ▶ until ($DISTANCE_SYMBOLS[-1][0] > $max_dist) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DISTANCE_SYMBOLS[-1][0] > $max_dist) 1966 714 2680
2200 1966 push @DISTANCE_SYMBOLS, [int($DISTANCE_SYMBOLS[-1][0] * (4 / 3)), $DISTANCE_SYMBOLS[-1][1] + 1];
2201 1966 push @DISTANCE_SYMBOLS, [int($DISTANCE_SYMBOLS[-1][0] * (3 / 2)), $DISTANCE_SYMBOLS[-1][1]];
2202 }
2203  
2204 # [length, offset bits]
2205 714 my @LENGTH_SYMBOLS = ((map { [$_, 0] } (1 .. 10)));
2206  
2207 {
2208 714 my $delta = 1;
2209 714 ▶ until ($LENGTH_SYMBOLS[-1][0] > $max_len) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LENGTH_SYMBOLS[-1][0] > $max_len) 288 714 1002
2210 288 push @LENGTH_SYMBOLS, [$LENGTH_SYMBOLS[-1][0] + $delta, $LENGTH_SYMBOLS[-1][1] + 1];
2211 288 $delta *= 2;
2212 288 push @LENGTH_SYMBOLS, [$LENGTH_SYMBOLS[-1][0] + $delta, $LENGTH_SYMBOLS[-1][1]];
2213 288 push @LENGTH_SYMBOLS, [$LENGTH_SYMBOLS[-1][0] + $delta, $LENGTH_SYMBOLS[-1][1]];
2214 288 push @LENGTH_SYMBOLS, [$LENGTH_SYMBOLS[-1][0] + $delta, $LENGTH_SYMBOLS[-1][1]];
2215 }
2216 714 ▶ while (@LENGTH_SYMBOLS and $LENGTH_SYMBOLS[-1][0] >= $max_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (@LENGTH_SYMBOLS and $LENGTH_SYMBOLS[-1][0] >= $max_len) 6192 714 6906
Condition100%3/3
@LENGTH_SYMBOLS and $LENGTH_SYMBOLS[-1][0] >= $max_len
!ll&&!rl&&r
5561586192
MC/DC100%2/2
@LENGTH_SYMBOLS and $LENGTH_SYMBOLS[-1][0] >= $max_len
@LENGTH_SYMBOLS $LENGTH_SYMBOLS[-1][0] >= $max_len
Truth table100%3/3
A and B
A= @LENGTH_SYMBOLSB= $LENGTH_SYMBOLS[-1][0] >= $max_len
ABresult
0X0
100
111
2217 6192 pop @LENGTH_SYMBOLS;
2218 }
2219 714 push @LENGTH_SYMBOLS, [$max_len, 0];
2220 }
2221  
2222 714 my @LENGTH_INDICES;
2223  
2224 714 foreach my $i (0 .. $#LENGTH_SYMBOLS) {
2225 2814 my ($min, $bits) = @{$LENGTH_SYMBOLS[$i]};
2226 2814 foreach my $k ($min .. $min + (1 << $bits) - 1) {
2227 14150 $LENGTH_INDICES[$k] = $i;
2228 }
2229 }
2230  
2231 714 return (\@DISTANCE_SYMBOLS, \@LENGTH_SYMBOLS, \@LENGTH_INDICES);
2232 }
2233  
2234 22340 ▶sub find_deflate_index ($value, $table) {
Subroutine find_deflate_index: called CC=3 SCAR=11.1
Pod documented
2235 22340 foreach my $i (0 .. $#{$table}) {
2236 386960 ▶ if ($table->[$i][0] > $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table->[$i][0] > $value) 22340 364620 386960
2237 22340 return $i - 1;
2238 }
2239 }
2240 0 confess "error";
2241 }
2242  
2243 97 ▶sub deflate_encode ($literals, $distances, $lengths, $entropy_sub = \&create_huffman_entry) {
Subroutine deflate_encode: called CC=9 SCAR=22.0
Pod documented
2244  
2245 97 ▶ my $max_dist = max(@$distances) // 0;
Condition100%2/2
&max(@$distances) // 0
l!l
907
MC/DC100%1/1
&max(@$distances) // 0
&max(@$distances)
Truth table100%2/2
A
A= &max(@$distances)
Aresult
11
00
2246 97 ▶ my $max_len = max(@$lengths) // 0;
Condition100%2/2
&max(@$lengths) // 0
l!l
907
MC/DC100%1/1
&max(@$lengths) // 0
&max(@$lengths)
Truth table100%2/2
A
A= &max(@$lengths)
Aresult
11
00
2247 97 ▶ my $max_symbol = (max(grep { defined($_) } @$literals) // -1) + 1;
Condition100%2/2
&max(grep({defined $_;} @$literals)) // -1
l!l
907
MC/DC100%1/1
&max(grep({defined $_;} @$literals)) // -1
&max(grep({defined $_;} @$literals))
Truth table100%2/2
A
A= &max(grep({defined $_;} @$literals))
Aresult
11
00
2248  
2249 97 my ($DISTANCE_SYMBOLS, $LENGTH_SYMBOLS, $LENGTH_INDICES) = make_deflate_tables($max_dist, $max_len);
2250  
2251 97 my @len_symbols;
2252 my @dist_symbols;
2253 97 my $offset_bits = '';
2254  
2255 97 foreach my $k (0 .. $#$literals) {
2256  
2257 12941 ▶ if ($lengths->[$k] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lengths->[$k] == 0) 8833 4108 12941
2258 8833 push @len_symbols, $literals->[$k];
2259 8833 next;
2260 }
2261  
2262 4108 my $len = $lengths->[$k];
2263 4108 my $dist = $distances->[$k];
2264  
2265 {
2266 4108 my $len_idx = $LENGTH_INDICES->[$len];
2267 4108 my ($min, $bits) = @{$LENGTH_SYMBOLS->[$len_idx]};
2268  
2269 4108 push @len_symbols, $len_idx + $max_symbol;
2270  
2271 4108 ▶ if ($bits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits > 0) 1565 2543 4108
2272 1565 $offset_bits .= sprintf('%0*b', $bits, $len - $min);
2273 }
2274 }
2275  
2276 {
2277 4108 my $dist_idx = find_deflate_index($dist, $DISTANCE_SYMBOLS);
2278 4108 my ($min, $bits) = @{$DISTANCE_SYMBOLS->[$dist_idx]};
2279  
2280 4108 push @dist_symbols, $dist_idx;
2281  
2282 4108 ▶ if ($bits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits > 0) 3902 206 4108
2283 3902 $offset_bits .= sprintf('%0*b', $bits, $dist - $min);
2284 }
2285 }
2286 }
2287  
2288 97 fibonacci_encode([$max_symbol, $max_dist, $max_len]) . $entropy_sub->(\@len_symbols) . $entropy_sub->(\@dist_symbols) . pack('B*', $offset_bits);
2289 }
2290  
2291 97 ▶sub deflate_decode ($fh, $entropy_sub = \&decode_huffman_entry) {
Subroutine deflate_decode: called CC=9 SCAR=22.2
Pod documented
2292  
2293 97 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 0 97 97
2294 0 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 0 0 0
2295 0 return __SUB__->($fh2, $entropy_sub);
2296 }
2297  
2298 97 my ($max_symbol, $max_dist, $max_len) = @{fibonacci_decode($fh)};
2299 97 my ($DISTANCE_SYMBOLS, $LENGTH_SYMBOLS) = make_deflate_tables($max_dist, $max_len);
2300  
2301 97 my $len_symbols = $entropy_sub->($fh);
2302 97 my $dist_symbols = $entropy_sub->($fh);
2303  
2304 97 my $bits_len = 0;
2305  
2306 97 foreach my $i (@$dist_symbols) {
2307 4108 $bits_len += $DISTANCE_SYMBOLS->[$i][1];
2308 }
2309  
2310 97 foreach my $i (@$len_symbols) {
2311 12941 ▶ if ($i >= $max_symbol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $max_symbol) 4108 8833 12941
2312 4108 $bits_len += $LENGTH_SYMBOLS->[$i - $max_symbol][1];
2313 }
2314 }
2315  
2316 97 my $bits = read_bits($fh, $bits_len);
2317  
2318 97 my @literals;
2319 my @lengths;
2320 97 my @distances;
2321  
2322 97 my $j = 0;
2323  
2324 97 foreach my $i (@$len_symbols) {
2325 12941 ▶ if ($i >= $max_symbol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $max_symbol) { } 4108 8833 12941
2326 4108 my $dist = $dist_symbols->[$j++];
2327 4108 push @literals, undef;
2328 4108 push @lengths, $LENGTH_SYMBOLS->[$i - $max_symbol][0] + oct('0b' . substr($bits, 0, $LENGTH_SYMBOLS->[$i - $max_symbol][1], ''));
2329 4108 push @distances, $DISTANCE_SYMBOLS->[$dist][0] + oct('0b' . substr($bits, 0, $DISTANCE_SYMBOLS->[$dist][1], ''));
2330 }
2331 else {
2332 8833 push @literals, $i;
2333 8833 push @lengths, 0;
2334 8833 push @distances, 0;
2335 }
2336 }
2337  
2338 97 return (\@literals, \@distances, \@lengths);
2339 }
2340  
2341 #####################
2342 # Elias gamma coding
2343 #####################
2344  
2345 15 ▶sub elias_gamma_encode ($integers) {
Subroutine elias_gamma_encode: called CC=2 SCAR=6.9
Pod documented
2346  
2347 15 my $bitstring = '';
2348 15 foreach my $k (scalar(@$integers), @$integers) {
2349 1322 my $t = sprintf('%b', $k + 1);
2350 1322 $bitstring .= ('1' x (length($t) - 1)) . '0' . substr($t, 1);
2351 }
2352  
2353 15 pack('B*', $bitstring);
2354 }
2355  
2356 28 ▶sub elias_gamma_decode ($fh) {
Subroutine elias_gamma_decode: called CC=6 SCAR=17.9
Pod documented
2357  
2358 28 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 13 15 28
2359 13 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 13 0 13
2360 13 return __SUB__->($fh2);
2361 }
2362  
2363 15 my @ints;
2364 15 my $len = 0;
2365 15 my $buffer = '';
2366  
2367 15 ▶ for (my $k = 0 ; $k <= $len ; ++$k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $len) 1322 15 1337
2368  
2369 1322 my $n = 0;
2370 1322 ▶ ++$n while (read_bit($fh, \$buffer) eq '1');
Branch: 1 branch
BranchTrueFalseTotal
read_bit($fh, \$buffer) eq 1 and do { ++$n } 8136 1322 9458
2371  
2372 1322 push @ints, oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $n)) - 1;
2373  
2374 1322 ▶ if ($k == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 0) 15 1307 1322
2375 15 $len = pop(@ints);
2376 }
2377 }
2378  
2379 15 return \@ints;
2380 }
2381  
2382 #####################
2383 # Elias delta coding
2384 #####################
2385  
2386 15 ▶sub elias_delta_encode ($integers) {
Subroutine elias_delta_encode: called CC=2 SCAR=6.9
Pod documented
2387  
2388 15 my $bitstring = '';
2389 15 foreach my $k (scalar(@$integers), @$integers) {
2390 1341 my $t = sprintf('%b', $k + 1);
2391 1341 my $l = length($t);
2392 1341 my $L = sprintf('%b', $l);
2393 1341 $bitstring .= ('1' x (length($L) - 1)) . '0' . substr($L, 1) . substr($t, 1);
2394 }
2395  
2396 15 pack('B*', $bitstring);
2397 }
2398  
2399 28 ▶sub elias_delta_decode ($fh) {
Subroutine elias_delta_decode: called CC=6 SCAR=17.9
Pod documented
2400  
2401 28 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 13 15 28
2402 13 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 13 0 13
2403 13 return __SUB__->($fh2);
2404 }
2405  
2406 15 my @ints;
2407 15 my $len = 0;
2408 15 my $buffer = '';
2409  
2410 15 ▶ for (my $k = 0 ; $k <= $len ; ++$k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $len) 1341 15 1356
2411  
2412 1341 my $bl = 0;
2413 1341 ▶ ++$bl while (read_bit($fh, \$buffer) eq '1');
Branch: 1 branch
BranchTrueFalseTotal
read_bit($fh, \$buffer) eq 1 and do { ++$bl } 2808 1341 4149
2414  
2415 1341 my $bl2 = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $bl));
2416 1341 my $int = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. ($bl2 - 1))) - 1;
2417  
2418 1341 push @ints, $int;
2419  
2420 1341 ▶ if ($k == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 0) 15 1326 1341
2421 15 $len = pop(@ints);
2422 }
2423 }
2424  
2425 15 return \@ints;
2426 }
2427  
2428 #####################
2429 # Elias omega coding
2430 #####################
2431  
2432 15 ▶sub elias_omega_encode ($integers) {
Subroutine elias_omega_encode: called CC=3 SCAR=11.0
Pod documented
2433  
2434 15 my $bitstring = '';
2435  
2436 15 foreach my $k (scalar(@$integers), @$integers) {
2437  
2438 1342 my $n = $k + 1; # shift by one, so 0 can be encoded
2439 1342 my $code = '0'; # terminator bit
2440  
2441 1342 ▶ while ($n != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != 1) 4013 1342 5355
2442 4013 my $t = sprintf('%b', $n);
2443 4013 $code = $t . $code;
2444 4013 $n = length($t) - 1;
2445 }
2446  
2447 1342 $bitstring .= $code;
2448 }
2449  
2450 15 pack('B*', $bitstring);
2451 }
2452  
2453 28 ▶sub elias_omega_decode ($fh) {
Subroutine elias_omega_decode: called CC=6 SCAR=17.9
Pod documented
2454  
2455 28 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 13 15 28
2456 13 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 13 0 13
2457 13 return __SUB__->($fh2);
2458 }
2459  
2460 15 my @ints;
2461 15 my $len = 0;
2462 15 my $buffer = '';
2463  
2464 15 ▶ for (my $k = 0 ; $k <= $len ; ++$k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $len) 1342 15 1357
2465  
2466 1342 my $n = 1;
2467  
2468 1342 ▶ while (read_bit($fh, \$buffer) eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (read_bit($fh, \$buffer) eq 1) 4013 1342 5355
2469 4013 $n = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $n));
2470 }
2471  
2472 1342 push @ints, $n - 1;
2473  
2474 1342 ▶ if ($k == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 0) 15 1327 1342
2475 15 $len = pop(@ints);
2476 }
2477 }
2478  
2479 15 return \@ints;
2480 }
2481  
2482 ########################################
2483 # Golomb-Rice coding
2484 ########################################
2485  
2486 # Picks a good Rice parameter for a geometrically-distributed sample,
2487 # using the standard approximation k = ceil(log2(mean * ln(2))).
2488 # Private helper -- not exported.
2489 13 ▶sub _golomb_rice_optimal_k ($integers) {
Subroutine _golomb_rice_optimal_k: called CC=4 SCAR=13.9
2490  
2491 13 ▶ return 0 if !@$integers;
Branch: 1 branch
BranchTrueFalseTotal
unless @$integers 1 12 13
2492  
2493 12 my $mean = sum(@$integers) / scalar(@$integers);
2494 12 ▶ return 0 if $mean < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $mean < 1 1 11 12
2495  
2496 11 my ($k, $m) = (0, 1);
2497 11 ▶ while ($m < $mean * log(2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m < $mean * "0.693147180559945") 80 11 91
2498 80 $m <<= 1;
2499 80 ++$k;
2500 }
2501  
2502 11 return $k;
2503 }
2504  
2505 13 ▶sub golomb_rice_encode ($integers, $k = undef) {
Subroutine golomb_rice_encode: called CC=7 SCAR=19.7
Pod documented
2506  
2507 13 ▶ $k = _golomb_rice_optimal_k($integers) if !defined($k);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 13 0 13
2508 13 ▶ $k >= 0 or confess "error: \$k must be non-negative";
Branch: 1 branch
BranchTrueFalseTotal
$k >= 0 or confess("error: \$k must be non-negative") 13 0 13
2509  
2510 13 my $header = fibonacci_encode([scalar(@$integers), $k]);
2511  
2512 13 my $bitstring = '';
2513 13 my $mask = (1 << $k) - 1;
2514  
2515 13 foreach my $n (@$integers) {
2516 1052 ▶ $n >= 0 or confess "error: golomb_rice_encode() requires non-negative integers";
Branch: 1 branch
BranchTrueFalseTotal
$n >= 0 or confess("error: golomb_rice_encode() requires non-negative integers") 1052 0 1052
2517 1052 my $q = $n >> $k;
2518 1052 $bitstring .= ('1' x $q) . '0';
2519 1052 ▶ $bitstring .= sprintf('%0*b', $k, $n & $mask) if $k > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $k > 0 1050 2 1052
2520 }
2521  
2522 13 return $header . pack('B*', $bitstring);
2523 }
2524  
2525 26 ▶sub golomb_rice_decode ($fh) {
Subroutine golomb_rice_decode: called CC=6 SCAR=17.9
Pod documented
2526  
2527 26 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 13 13 26
2528 13 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 13 0 13
2529 13 return __SUB__->($fh2);
2530 }
2531  
2532 13 my ($len, $k) = @{fibonacci_decode($fh)};
2533  
2534 13 my @integers;
2535 13 my $buffer = '';
2536  
2537 13 foreach (1 .. $len) {
2538 1052 my $q = 0;
2539 1052 ▶ ++$q while (read_bit($fh, \$buffer) eq '1');
Branch: 1 branch
BranchTrueFalseTotal
read_bit($fh, \$buffer) eq 1 and do { ++$q } 43 1052 1095
2540 1052 ▶ my $r = $k > 0 ? bits2int($fh, $k, \$buffer) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$k > 0 ? : 1050 2 1052
2541 1052 push @integers, ($q << $k) | $r;
2542 }
2543  
2544 13 return \@integers;
2545 }
2546  
2547 ###################
2548 # LZSS SYMBOLIC
2549 ###################
2550  
2551 153 ▶sub lzss_encode_symbolic($symbols, %params) {
Subroutine lzss_encode_symbolic: called CC=26 SCAR=34.0
Pod documented
2552  
2553 153 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 153 153
2554 0 return lzss_encode($symbols, %params);
2555 }
2556  
2557 153 ▶ my $min_len = $params{min_len} // $LZ_MIN_LEN;
Condition33%1/3
$params{"min_len"} // $Compression::Util::LZ_MIN_LEN
l!l&&r!l&&!r
01530
MC/DC0%0/2
$params{"min_len"} // $Compression::Util::LZ_MIN_LEN
$params{"min_len"} $Compression::Util::LZ_MIN_LEN
Truth table33%1/3
A // B
A= $params{"min_len"}B= $Compression::Util::LZ_MIN_LEN
ABresult
1X1
011
000
2558 153 ▶ my $max_len = $params{max_len} // $LZ_MAX_LEN;
Condition33%1/3
$params{"max_len"} // $Compression::Util::LZ_MAX_LEN
l!l&&r!l&&!r
01530
MC/DC0%0/2
$params{"max_len"} // $Compression::Util::LZ_MAX_LEN
$params{"max_len"} $Compression::Util::LZ_MAX_LEN
Truth table33%1/3
A // B
A= $params{"max_len"}B= $Compression::Util::LZ_MAX_LEN
ABresult
1X1
011
000
2559 153 ▶ my $max_dist = $params{max_dist} // $LZ_MAX_DIST;
Condition33%1/3
$params{"max_dist"} // $Compression::Util::LZ_MAX_DIST
l!l&&r!l&&!r
01530
MC/DC0%0/2
$params{"max_dist"} // $Compression::Util::LZ_MAX_DIST
$params{"max_dist"} $Compression::Util::LZ_MAX_DIST
Truth table33%1/3
A // B
A= $params{"max_dist"}B= $Compression::Util::LZ_MAX_DIST
ABresult
1X1
011
000
2560 153 ▶ my $max_chain_len = $params{max_chain_len} // $LZ_MAX_CHAIN_LEN;
Condition33%1/3
$params{"max_chain_len"} // $Compression::Util::LZ_MAX_CHAIN_LEN
l!l&&r!l&&!r
01530
MC/DC0%0/2
$params{"max_chain_len"} // $Compression::Util::LZ_MAX_CHAIN_LEN
$params{"max_chain_len"} $Compression::Util::LZ_MAX_CHAIN_LEN
Truth table33%1/3
A // B
A= $params{"max_chain_len"}B= $Compression::Util::LZ_MAX_CHAIN_LEN
ABresult
1X1
011
000
2561 153 ▶ my $max_chain_width = $params{max_chain_width} // $LZ_MAX_CHAIN_WIDTH;
Condition33%1/3
$params{"max_chain_width"} // $Compression::Util::LZ_MAX_CHAIN_WIDTH
l!l&&r!l&&!r
01530
MC/DC0%0/2
$params{"max_chain_width"} // $Compression::Util::LZ_MAX_CHAIN_WIDTH
$params{"max_chain_width"} $Compression::Util::LZ_MAX_CHAIN_WIDTH
Truth table33%1/3
A // B
A= $params{"max_chain_width"}B= $Compression::Util::LZ_MAX_CHAIN_WIDTH
ABresult
1X1
011
000
2562  
2563 153 ▶ $min_len = 1 if $min_len < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $min_len < 1 0 153 153
2564  
2565 153 my $end = $#$symbols;
2566 153 my (@literals, @distances, @lengths, %table);
2567  
2568 153 ▶ for (my $la = 0 ; $la <= $end ;) {
Branch: 1 branch
BranchTrueFalseTotal
if ($la <= $end) 4032 153 4185
2569 4032 my $best_n = 1;
2570 4032 my $best_p = $la;
2571  
2572 4032 my $upto = $la + $min_len - 1;
2573 4032 ▶ my $lookahead = join(' ', @{$symbols}[$la .. ($upto > $end ? $end : $upto)]);
Branch: 1 branch
BranchTrueFalseTotal
$upto > $end ? : 276 3756 4032
2574  
2575 4032 ▶ if (exists $table{$lookahead}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $table{$lookahead}) 439 3593 4032
2576  
2577 439 foreach my $p (@{$table{$lookahead}}) {
2578  
2579 846 ▶ last if ($la - $p > $max_dist);
Branch: 1 branch
BranchTrueFalseTotal
if $la - $p > $max_dist 0 846 846
2580  
2581 846 my $n = $min_len;
2582  
2583 846 ▶ ++$n while ($la + $n <= $end and $symbols->[$la + $n - 1] == $symbols->[$p + $n - 1] and $n <= $max_len);
Branch: 1 branch
BranchTrueFalseTotal
$la + $n <= $end and $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1] and $n <= $max_len and do { ++$n } 11784 846 12630
Condition83%5/6
$la + $n <= $end and $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1] and $n <= $max_len
!ll&&!rl&&r
846011784
$la + $n <= $end and $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1]
!ll&&!rl&&r
4380311784
MC/DC66%2/3
$la + $n <= $end and $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1] and $n <= $max_len
$la + $n <= $end $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1] $n <= $max_len
Truth table75%3/4
A and B and C
A= $la + $n <= $endB= $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1]C= $n <= $max_len
ABCresult
0XX0
10X0
1100
1111
2584  
2585 846 ▶ if ($n > $best_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $best_n) 458 388 846
2586 458 $best_n = $n;
2587 458 $best_p = $p;
2588 458 ▶ last if ($n > $max_len);
Branch: 1 branch
BranchTrueFalseTotal
if $n > $max_len 0 458 458
2589 }
2590 }
2591 }
2592  
2593 4032 ▶ if ($best_n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best_n == 1) { } 3593 439 4032
2594 3593 $table{$lookahead} = [$la];
2595 }
2596 else {
2597 439 my @matched = @{$symbols}[$la .. $la + $best_n - 1];
2598 439 my @key_arr = @matched[0 .. $min_len - 1];
2599  
2600 439 my $insert_limit = $best_n - $min_len;
2601 439 ▶ $insert_limit = $best_n - 2 if $insert_limit > $best_n - 2;
Branch: 1 branch
BranchTrueFalseTotal
if $insert_limit > $best_n - 2 0 439 439
2602 439 ▶ $insert_limit = $max_chain_width if $insert_limit > $max_chain_width;
Branch: 1 branch
BranchTrueFalseTotal
if $insert_limit > $max_chain_width 0 439 439
2603  
2604 439 my %seen;
2605 439 foreach my $i (0 .. $insert_limit) {
2606  
2607 10647 my $key = join(' ', @key_arr);
2608  
2609 10647 ▶ if (!$seen{$key}++) { # prefer earlier matches
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$key}++) 1695 8952 10647
2610 1695 unshift @{$table{$key}}, $la + $i;
2611 0 ▶ pop @{$table{$key}} if (@{$table{$key}} > $max_chain_len);
Branch: 1 branch
BranchTrueFalseTotal
if @{$table{$key};} > $max_chain_len 0 1695 1695
2612 }
2613  
2614 10647 shift(@key_arr);
2615 10647 push @key_arr, $matched[$i + $min_len];
2616 }
2617 }
2618  
2619 4032 ▶ if ($best_n > $min_len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($best_n > $min_len) { } 428 3604 4032
elsif ($best_n == 1) { } 3593 11 3604
2620  
2621 428 push @lengths, $best_n - 1;
2622 428 push @distances, $la - $best_p;
2623 428 push @literals, undef;
2624  
2625 428 $la += $best_n - 1;
2626 }
2627 elsif ($best_n == 1) {
2628 3593 push @lengths, 0;
2629 3593 push @distances, 0;
2630 3593 push @literals, $symbols->[$la++];
2631 }
2632 else {
2633  
2634 11 push @lengths, (0) x $best_n;
2635 11 push @distances, (0) x $best_n;
2636 11 push @literals, @{$symbols}[$la .. $la + $best_n - 1];
2637  
2638 11 $la += $best_n;
2639 }
2640 }
2641  
2642 153 return (\@literals, \@distances, \@lengths);
2643 }
2644  
2645 90 ▶sub lzss_decode_symbolic ($literals, $distances, $lengths) {
Subroutine lzss_decode_symbolic: called CC=8 SCAR=20.8
Pod documented
2646  
2647 90 my @data;
2648 90 my $data_len = 0;
2649  
2650 90 foreach my $i (0 .. $#$lengths) {
2651  
2652 3219 ▶ if ($lengths->[$i] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lengths->[$i] == 0) 2822 397 3219
2653 2822 push @data, $literals->[$i];
2654 2822 $data_len += 1;
2655 2822 next;
2656 }
2657  
2658 397 ▶ my $length = $lengths->[$i] // confess "bad input";
Condition50%1/2
$lengths->[$i] // confess("bad input")
l!l
3970
MC/DC0%0/1
$lengths->[$i] // confess("bad input")
$lengths->[$i]
Truth table50%1/2
A
A= $lengths->[$i]
Aresult
11
00
2659 397 ▶ my $dist = $distances->[$i] // confess "bad input";
Condition50%1/2
$distances->[$i] // confess("bad input")
l!l
3970
MC/DC0%0/1
$distances->[$i] // confess("bad input")
$distances->[$i]
Truth table50%1/2
A
A= $distances->[$i]
Aresult
11
00
2660  
2661 397 ▶ if ($dist >= $length) { # non-overlapping matches
Branch: 2 branches
BranchTrueFalseTotal
if ($dist >= $length) { } 284 113 397
elsif ($dist == 1) { } 34 79 113
2662 284 push @data, @data[$data_len - $dist .. $data_len - $dist + $length - 1];
2663 }
2664 elsif ($dist == 1) { # run-length of last character
2665 34 push @data, ($data[-1]) x $length;
2666 }
2667 else { # overlapping matches
2668 79 foreach my $j (1 .. $length) {
2669 453 push @data, $data[$data_len + $j - $dist - 1];
2670 }
2671 }
2672  
2673 397 $data_len += $length;
2674 }
2675  
2676 90 return \@data;
2677 }
2678  
2679 ###################
2680 # LZSS Encoding
2681 ###################
2682  
2683 308 ▶sub lzss_encode ($str, %params) {
Subroutine lzss_encode: called CC=24 SCAR=32.0
Pod documented
2684  
2685 308 ▶ if (ref($str) ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $str ne "") 140 168 308
2686 140 return lzss_encode_symbolic($str, %params);
2687 }
2688  
2689 168 ▶ my $min_len = $params{min_len} // $LZ_MIN_LEN;
Condition66%2/3
$params{"min_len"} // $Compression::Util::LZ_MIN_LEN
l!l&&r!l&&!r
41640
MC/DC0%0/2
$params{"min_len"} // $Compression::Util::LZ_MIN_LEN
$params{"min_len"} $Compression::Util::LZ_MIN_LEN
Truth table66%2/3
A // B
A= $params{"min_len"}B= $Compression::Util::LZ_MIN_LEN
ABresult
1X1
011
000
2690 168 ▶ my $max_len = $params{max_len} // $LZ_MAX_LEN;
Condition66%2/3
$params{"max_len"} // $Compression::Util::LZ_MAX_LEN
l!l&&r!l&&!r
41640
MC/DC0%0/2
$params{"max_len"} // $Compression::Util::LZ_MAX_LEN
$params{"max_len"} $Compression::Util::LZ_MAX_LEN
Truth table66%2/3
A // B
A= $params{"max_len"}B= $Compression::Util::LZ_MAX_LEN
ABresult
1X1
011
000
2691 168 ▶ my $max_dist = $params{max_dist} // $LZ_MAX_DIST;
Condition66%2/3
$params{"max_dist"} // $Compression::Util::LZ_MAX_DIST
l!l&&r!l&&!r
41640
MC/DC0%0/2
$params{"max_dist"} // $Compression::Util::LZ_MAX_DIST
$params{"max_dist"} $Compression::Util::LZ_MAX_DIST
Truth table66%2/3
A // B
A= $params{"max_dist"}B= $Compression::Util::LZ_MAX_DIST
ABresult
1X1
011
000
2692 168 ▶ my $max_chain_len = $params{max_chain_len} // $LZ_MAX_CHAIN_LEN;
Condition33%1/3
$params{"max_chain_len"} // $Compression::Util::LZ_MAX_CHAIN_LEN
l!l&&r!l&&!r
01680
MC/DC0%0/2
$params{"max_chain_len"} // $Compression::Util::LZ_MAX_CHAIN_LEN
$params{"max_chain_len"} $Compression::Util::LZ_MAX_CHAIN_LEN
Truth table33%1/3
A // B
A= $params{"max_chain_len"}B= $Compression::Util::LZ_MAX_CHAIN_LEN
ABresult
1X1
011
000
2693 168 ▶ my $max_chain_width = $params{max_chain_width} // $LZ_MAX_CHAIN_WIDTH;
Condition33%1/3
$params{"max_chain_width"} // $Compression::Util::LZ_MAX_CHAIN_WIDTH
l!l&&r!l&&!r
01680
MC/DC0%0/2
$params{"max_chain_width"} // $Compression::Util::LZ_MAX_CHAIN_WIDTH
$params{"max_chain_width"} $Compression::Util::LZ_MAX_CHAIN_WIDTH
Truth table33%1/3
A // B
A= $params{"max_chain_width"}B= $Compression::Util::LZ_MAX_CHAIN_WIDTH
ABresult
1X1
011
000
2694  
2695 168 ▶ $min_len = 1 if $min_len < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $min_len < 1 0 168 168
2696  
2697 168 my @symbols = unpack('C*', $str);
2698 168 my $end = $#symbols;
2699  
2700 168 my (@literals, @distances, @lengths, %table);
2701  
2702 168 ▶ for (my $la = 0 ; $la <= $end ;) {
Branch: 1 branch
BranchTrueFalseTotal
if ($la <= $end) 26691 168 26859
2703  
2704 26691 my $best_n = 1;
2705 26691 my $best_p = $la;
2706  
2707 26691 my $lookahead = substr($str, $la, $min_len);
2708  
2709 26691 ▶ if (exists $table{$lookahead}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $table{$lookahead}) 8471 18220 26691
2710 8471 foreach my $p (@{$table{$lookahead}}) {
2711  
2712 87389 ▶ last if ($la - $p > $max_dist);
Branch: 1 branch
BranchTrueFalseTotal
if $la - $p > $max_dist 0 87389 87389
2713  
2714 87389 my $n = $min_len;
2715  
2716 87389 ▶ ++$n while ($la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1] and $n <= $max_len);
Branch: 1 branch
BranchTrueFalseTotal
$la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1] and $n <= $max_len and do { ++$n } 652032 87389 739421
Condition100%6/6
$la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1] and $n <= $max_len
!ll&&!rl&&r
8737118652032
$la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1]
!ll&&!rl&&r
6787304652050
MC/DC100%3/3
$la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1] and $n <= $max_len
$la + $n <= $end $symbols[$la + $n - 1] == $symbols[$p + $n - 1] $n <= $max_len
Truth table100%4/4
A and B and C
A= $la + $n <= $endB= $symbols[$la + $n - 1] == $symbols[$p + $n - 1]C= $n <= $max_len
ABCresult
0XX0
10X0
1100
1111
2717  
2718 87389 ▶ if ($n > $best_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $best_n) 11842 75547 87389
2719 11842 $best_p = $p;
2720 11842 $best_n = $n;
2721 11842 ▶ last if ($best_n > $max_len);
Branch: 1 branch
BranchTrueFalseTotal
if $best_n > $max_len 19 11823 11842
2722 }
2723 }
2724 }
2725  
2726 26691 ▶ if ($best_n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best_n == 1) { } 18220 8471 26691
2727 18220 $table{$lookahead} = [$la];
2728 }
2729 else {
2730  
2731 8471 my $matched = substr($str, $la, $best_n);
2732 8471 my $insert_limit = min($best_n - $min_len, $best_n - 2);
2733 8471 ▶ $insert_limit = $max_chain_width if $insert_limit > $max_chain_width;
Branch: 1 branch
BranchTrueFalseTotal
if $insert_limit > $max_chain_width 0 8471 8471
2734  
2735 8471 my %seen;
2736 8471 foreach my $i (0 .. $insert_limit) {
2737 161380 my $key = substr($matched, $i, $min_len);
2738 161380 ▶ next if $seen{$key}++; # prefer earlier matches
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key}++ 51130 110250 161380
2739 110250 unshift @{$table{$key}}, $la + $i;
2740 110250 ▶ pop(@{$table{$key}}) if (@{$table{$key}} > $max_chain_len);
Branch: 1 branch
BranchTrueFalseTotal
if @{$table{$key};} > $max_chain_len 26592 83658 110250
2741 }
2742 }
2743  
2744 26691 ▶ if ($best_n > $min_len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($best_n > $min_len) { } 8459 18232 26691
elsif ($best_n == 1) { } 18220 12 18232
2745  
2746 8459 push @lengths, $best_n - 1;
2747 8459 push @distances, $la - $best_p;
2748 8459 push @literals, undef;
2749  
2750 8459 $la += $best_n - 1;
2751 }
2752 elsif ($best_n == 1) {
2753 18220 push @lengths, 0;
2754 18220 push @distances, 0;
2755 18220 push @literals, $symbols[$la++];
2756 }
2757 else {
2758  
2759 12 push @lengths, (0) x $best_n;
2760 12 push @distances, (0) x $best_n;
2761 12 push @literals, @symbols[$la .. $la + $best_n - 1];
2762  
2763 12 $la += $best_n;
2764 }
2765 }
2766  
2767 168 return (\@literals, \@distances, \@lengths);
2768 }
2769  
2770 56 ▶sub lzss_decode ($literals, $distances, $lengths) {
Subroutine lzss_decode: called CC=10 SCAR=23.4
Pod documented
2771  
2772 56 my $data = '';
2773 56 my $data_len = 0;
2774  
2775 56 foreach my $i (0 .. $#$lengths) {
2776  
2777 11396 ▶ if ($lengths->[$i] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lengths->[$i] == 0) 7471 3925 11396
2778 7471 $data .= chr($literals->[$i]);
2779 7471 ++$data_len;
2780 7471 next;
2781 }
2782  
2783 3925 ▶ my $length = $lengths->[$i] // confess "bad input";
Condition50%1/2
$lengths->[$i] // confess("bad input")
l!l
39250
MC/DC0%0/1
$lengths->[$i] // confess("bad input")
$lengths->[$i]
Truth table50%1/2
A
A= $lengths->[$i]
Aresult
11
00
2784 3925 ▶ my $dist = $distances->[$i] // confess "bad input";
Condition50%1/2
$distances->[$i] // confess("bad input")
l!l
39250
MC/DC0%0/1
$distances->[$i] // confess("bad input")
$distances->[$i]
Truth table50%1/2
A
A= $distances->[$i]
Aresult
11
00
2785  
2786 3925 ▶ if ($dist >= $length) { # non-overlapping matches
Branch: 2 branches
BranchTrueFalseTotal
if ($dist >= $length) { } 3751 174 3925
elsif ($dist == 1) { } 37 137 174
2787 3751 ▶ $data .= substr($data, $data_len - $dist, $length) // confess "bad input";
Condition50%1/2
substr($data, $data_len - $dist, $length) // confess("bad input")
l!l
37510
MC/DC0%0/1
substr($data, $data_len - $dist, $length) // confess("bad input")
substr($data, $data_len - $dist, $length)
Truth table50%1/2
A
A= substr($data, $data_len - $dist, $length)
Aresult
11
00
2788 }
2789 elsif ($dist == 1) { # run-length of last character
2790 37 $data .= substr($data, -1) x $length;
2791 }
2792 else { # overlapping matches
2793 137 ▶ my $pattern = substr($data, $data_len - $dist, $dist) // confess "bad input";
Condition50%1/2
substr($data, $data_len - $dist, $dist) // confess("bad input")
l!l
1370
MC/DC0%0/1
substr($data, $data_len - $dist, $dist) // confess("bad input")
substr($data, $data_len - $dist, $dist)
Truth table50%1/2
A
A= substr($data, $data_len - $dist, $dist)
Aresult
11
00
2794 137 my $full_reps = int(($length + $dist - 1) / $dist);
2795 137 ▶ $data .= substr($pattern x $full_reps, 0, $length) // confess "bad input";
Condition50%1/2
substr($pattern x $full_reps, 0, $length) // confess("bad input")
l!l
1370
MC/DC0%0/1
substr($pattern x $full_reps, 0, $length) // confess("bad input")
substr($pattern x $full_reps, 0, $length)
Truth table50%1/2
A
A= substr($pattern x $full_reps, 0, $length)
Aresult
11
00
2796 }
2797  
2798 3925 $data_len += $length;
2799 }
2800  
2801 56 return $data;
2802 }
2803  
2804 ###################
2805 # LZSSF Compression
2806 ###################
2807  
2808 19 ▶sub lzss_encode_fast_symbolic ($symbols, %params) {
Subroutine lzss_encode_fast_symbolic: called CC=15 SCAR=27.6
Pod documented
2809  
2810 19 ▶ if (ref($symbols) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbols eq "") 0 19 19
2811 0 return lzss_encode_fast($symbols, %params);
2812 }
2813  
2814 19 my $la = 0;
2815 19 my $end = $#$symbols;
2816  
2817 19 ▶ my $min_len = $params{min_len} // $LZ_MIN_LEN; # minimum match length
Condition33%1/3
$params{"min_len"} // $Compression::Util::LZ_MIN_LEN
l!l&&r!l&&!r
0190
MC/DC0%0/2
$params{"min_len"} // $Compression::Util::LZ_MIN_LEN
$params{"min_len"} $Compression::Util::LZ_MIN_LEN
Truth table33%1/3
A // B
A= $params{"min_len"}B= $Compression::Util::LZ_MIN_LEN
ABresult
1X1
011
000
2818 19 ▶ my $max_len = $params{max_len} // $LZ_MAX_LEN; # maximum match length
Condition33%1/3
$params{"max_len"} // $Compression::Util::LZ_MAX_LEN
l!l&&r!l&&!r
0190
MC/DC0%0/2
$params{"max_len"} // $Compression::Util::LZ_MAX_LEN
$params{"max_len"} $Compression::Util::LZ_MAX_LEN
Truth table33%1/3
A // B
A= $params{"max_len"}B= $Compression::Util::LZ_MAX_LEN
ABresult
1X1
011
000
2819 19 ▶ my $max_dist = $params{max_dist} // $LZ_MAX_DIST; # maximum offset distance
Condition33%1/3
$params{"max_dist"} // $Compression::Util::LZ_MAX_DIST
l!l&&r!l&&!r
0190
MC/DC0%0/2
$params{"max_dist"} // $Compression::Util::LZ_MAX_DIST
$params{"max_dist"} $Compression::Util::LZ_MAX_DIST
Truth table33%1/3
A // B
A= $params{"max_dist"}B= $Compression::Util::LZ_MAX_DIST
ABresult
1X1
011
000
2820  
2821 19 ▶ $min_len = 1 if $min_len < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $min_len < 1 0 19 19
2822  
2823 19 my (@literals, @distances, @lengths, %table);
2824  
2825 19 ▶ while ($la <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($la <= $end) 2765 19 2784
2826  
2827 2765 my $best_n = 1;
2828 2765 my $best_p = $la;
2829  
2830 2765 my $upto = $la + $min_len - 1;
2831 2765 ▶ my $lookahead = join(' ', @{$symbols}[$la .. ($upto > $end ? $end : $upto)]);
Branch: 1 branch
BranchTrueFalseTotal
$upto > $end ? : 41 2724 2765
2832  
2833 2765 ▶ if (exists($table{$lookahead}) and $la - $table{$lookahead} <= $max_dist) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $table{$lookahead} and $la - $table{$lookahead} <= $max_dist) 374 2391 2765
Condition66%2/3
exists $table{$lookahead} and $la - $table{$lookahead} <= $max_dist
!ll&&!rl&&r
23910374
MC/DC50%1/2
exists $table{$lookahead} and $la - $table{$lookahead} <= $max_dist
exists $table{$lookahead} $la - $table{$lookahead} <= $max_dist
Truth table66%2/3
A and B
A= exists $table{$lookahead}B= $la - $table{$lookahead} <= $max_dist
ABresult
0X0
100
111
2834  
2835 374 my $p = $table{$lookahead};
2836 374 my $n = $min_len;
2837  
2838 374 ▶ ++$n while ($la + $n <= $end and $symbols->[$la + $n - 1] == $symbols->[$p + $n - 1] and $n <= $max_len);
Branch: 1 branch
BranchTrueFalseTotal
$la + $n <= $end and $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1] and $n <= $max_len and do { ++$n } 3218 374 3592
Condition83%5/6
$la + $n <= $end and $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1] and $n <= $max_len
!ll&&!rl&&r
37403218
$la + $n <= $end and $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1]
!ll&&!rl&&r
43703218
MC/DC66%2/3
$la + $n <= $end and $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1] and $n <= $max_len
$la + $n <= $end $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1] $n <= $max_len
Truth table75%3/4
A and B and C
A= $la + $n <= $endB= $$symbols[$la + $n - 1] == $$symbols[$p + $n - 1]C= $n <= $max_len
ABCresult
0XX0
10X0
1100
1111
2839  
2840 374 $best_p = $p;
2841 374 $best_n = $n;
2842 }
2843  
2844 2765 $table{$lookahead} = $la;
2845  
2846 2765 ▶ if ($best_n > $min_len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($best_n > $min_len) { } 373 2392 2765
elsif ($best_n == 1) { } 2391 1 2392
2847  
2848 373 push @lengths, $best_n - 1;
2849 373 push @distances, $la - $best_p;
2850 373 push @literals, undef;
2851  
2852 373 $la += $best_n - 1;
2853 }
2854 elsif ($best_n == 1) {
2855 2391 push @lengths, 0;
2856 2391 push @distances, 0;
2857 2391 push @literals, $symbols->[$la++];
2858 }
2859 else {
2860  
2861 1 push @lengths, (0) x $best_n;
2862 1 push @distances, (0) x $best_n;
2863 1 push @literals, @{$symbols}[$la .. $la + $best_n - 1];
2864  
2865 1 $la += $best_n;
2866 }
2867 }
2868  
2869 19 return (\@literals, \@distances, \@lengths);
2870 }
2871  
2872 104 ▶sub lzss_encode_fast($str, %params) {
Subroutine lzss_encode_fast: called CC=14 SCAR=26.6
Pod documented
2873  
2874 104 ▶ if (ref($str) ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $str ne "") 19 85 104
2875 19 return lzss_encode_fast_symbolic($str, %params);
2876 }
2877  
2878 85 my @symbols = unpack('C*', $str);
2879  
2880 85 my $la = 0;
2881 85 my $end = $#symbols;
2882  
2883 85 ▶ my $min_len = $params{min_len} // $LZ_MIN_LEN; # minimum match length
Condition33%1/3
$params{"min_len"} // $Compression::Util::LZ_MIN_LEN
l!l&&r!l&&!r
0850
MC/DC0%0/2
$params{"min_len"} // $Compression::Util::LZ_MIN_LEN
$params{"min_len"} $Compression::Util::LZ_MIN_LEN
Truth table33%1/3
A // B
A= $params{"min_len"}B= $Compression::Util::LZ_MIN_LEN
ABresult
1X1
011
000
2884 85 ▶ my $max_len = $params{max_len} // $LZ_MAX_LEN; # maximum match length
Condition33%1/3
$params{"max_len"} // $Compression::Util::LZ_MAX_LEN
l!l&&r!l&&!r
0850
MC/DC0%0/2
$params{"max_len"} // $Compression::Util::LZ_MAX_LEN
$params{"max_len"} $Compression::Util::LZ_MAX_LEN
Truth table33%1/3
A // B
A= $params{"max_len"}B= $Compression::Util::LZ_MAX_LEN
ABresult
1X1
011
000
2885 85 ▶ my $max_dist = $params{max_dist} // $LZ_MAX_DIST; # maximum offset distance
Condition33%1/3
$params{"max_dist"} // $Compression::Util::LZ_MAX_DIST
l!l&&r!l&&!r
0850
MC/DC0%0/2
$params{"max_dist"} // $Compression::Util::LZ_MAX_DIST
$params{"max_dist"} $Compression::Util::LZ_MAX_DIST
Truth table33%1/3
A // B
A= $params{"max_dist"}B= $Compression::Util::LZ_MAX_DIST
ABresult
1X1
011
000
2886  
2887 85 ▶ $min_len = 1 if $min_len < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $min_len < 1 0 85 85
2888  
2889 85 my (@literals, @distances, @lengths, %table);
2890  
2891 85 ▶ while ($la <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($la <= $end) 26331 85 26416
2892  
2893 26331 my $best_n = 1;
2894 26331 my $best_p = $la;
2895  
2896 26331 my $lookahead = substr($str, $la, $min_len);
2897  
2898 26331 ▶ if (exists($table{$lookahead}) and $la - $table{$lookahead} <= $max_dist) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $table{$lookahead} and $la - $table{$lookahead} <= $max_dist) 10857 15474 26331
Condition66%2/3
exists $table{$lookahead} and $la - $table{$lookahead} <= $max_dist
!ll&&!rl&&r
15474010857
MC/DC50%1/2
exists $table{$lookahead} and $la - $table{$lookahead} <= $max_dist
exists $table{$lookahead} $la - $table{$lookahead} <= $max_dist
Truth table66%2/3
A and B
A= exists $table{$lookahead}B= $la - $table{$lookahead} <= $max_dist
ABresult
0X0
100
111
2899  
2900 10857 my $p = $table{$lookahead};
2901 10857 my $n = $min_len;
2902  
2903 10857 ▶ ++$n while ($la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1] and $n <= $max_len);
Branch: 1 branch
BranchTrueFalseTotal
$la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1] and $n <= $max_len and do { ++$n } 135147 10857 146004
Condition100%6/6
$la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1] and $n <= $max_len
!ll&&!rl&&r
108498135147
$la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1]
!ll&&!rl&&r
3110818135155
MC/DC100%3/3
$la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1] and $n <= $max_len
$la + $n <= $end $symbols[$la + $n - 1] == $symbols[$p + $n - 1] $n <= $max_len
Truth table100%4/4
A and B and C
A= $la + $n <= $endB= $symbols[$la + $n - 1] == $symbols[$p + $n - 1]C= $n <= $max_len
ABCresult
0XX0
10X0
1100
1111
2904  
2905 10857 $best_p = $p;
2906 10857 $best_n = $n;
2907 }
2908  
2909 26331 $table{$lookahead} = $la;
2910  
2911 26331 ▶ if ($best_n > $min_len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($best_n > $min_len) { } 10851 15480 26331
elsif ($best_n == 1) { } 15474 6 15480
2912  
2913 10851 push @lengths, $best_n - 1;
2914 10851 push @distances, $la - $best_p;
2915 10851 push @literals, undef;
2916  
2917 10851 $la += $best_n - 1;
2918 }
2919 elsif ($best_n == 1) {
2920 15474 push @lengths, 0;
2921 15474 push @distances, 0;
2922 15474 push @literals, $symbols[$la++];
2923 }
2924 else {
2925  
2926 6 push @lengths, (0) x $best_n;
2927 6 push @distances, (0) x $best_n;
2928 6 push @literals, @symbols[$la .. $la + $best_n - 1];
2929  
2930 6 $la += $best_n;
2931 }
2932 }
2933  
2934 85 return (\@literals, \@distances, \@lengths);
2935 }
2936  
2937 ################################
2938 # LZ77 encoding, inspired by LZ4
2939 ################################
2940  
2941 130 ▶sub lz77_encode($chunk, $lzss_encoding_sub = \&lzss_encode) {
Subroutine lz77_encode: called CC=14 SCAR=26.4
Pod documented
2942  
2943 130 local $LZ_MAX_LEN = ~0; # maximum match length
2944  
2945 130 my ($literals, $distances, $lengths) = $lzss_encoding_sub->($chunk);
2946  
2947 130 my $literals_end = $#{$literals};
2948 130 my (@symbols, @len_symbols, @match_symbols, @dist_symbols);
2949  
2950 130 ▶ for (my $i = 0 ; $i <= $literals_end ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $literals_end) 4573 130 4703
2951  
2952 4573 my $j = $i;
2953 4573 ▶ while ($i <= $literals_end and defined($literals->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $literals_end and defined $literals->[$i]) 11343 4573 15916
Condition100%3/3
$i <= $literals_end and defined $literals->[$i]
!ll&&!rl&&r
124444911343
MC/DC100%2/2
$i <= $literals_end and defined $literals->[$i]
$i <= $literals_end defined $literals->[$i]
Truth table100%3/3
A and B
A= $i <= $literals_endB= defined $literals->[$i]
ABresult
0X0
100
111
2954 11343 ++$i;
2955 }
2956  
2957 4573 my $literals_length = $i - $j;
2958 4573 ▶ my $match_len = $lengths->[$i] // 0;
Condition100%2/2
$lengths->[$i] // 0
l!l
4449124
MC/DC100%1/1
$lengths->[$i] // 0
$lengths->[$i]
Truth table100%2/2
A
A= $lengths->[$i]
Aresult
11
00
2959  
2960 4573 ▶ push @match_symbols, (($literals_length >= 7 ? 7 : $literals_length) << 5) | ($match_len >= 31 ? 31 : $match_len);
Branch: 2 branches
BranchTrueFalseTotal
$literals_length >= 7 ? : 474 4099 4573
$match_len >= 31 ? : 646 3927 4573
2961  
2962 4573 $literals_length -= 7;
2963 4573 $match_len -= 31;
2964  
2965 4573 ▶ while ($literals_length >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length >= 0) 474 4573 5047
2966 474 ▶ push @len_symbols, ($literals_length >= 255 ? 255 : $literals_length);
Branch: 1 branch
BranchTrueFalseTotal
$literals_length >= 255 ? : 0 474 474
2967 474 $literals_length -= 255;
2968 }
2969  
2970 4573 ▶ if ($i > $j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $j) 1749 2824 4573
2971 1749 push @symbols, @{$literals}[$j .. $i - 1];
2972 }
2973  
2974 4573 ▶ while ($match_len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_len >= 0) 668 4573 5241
2975 668 ▶ push @match_symbols, ($match_len >= 255 ? 255 : $match_len);
Branch: 1 branch
BranchTrueFalseTotal
$match_len >= 255 ? : 22 646 668
2976 668 $match_len -= 255;
2977 }
2978  
2979 4573 ▶ push @dist_symbols, $distances->[$i] // 0;
Condition100%2/2
$distances->[$i] // 0
l!l
4449124
MC/DC100%1/1
$distances->[$i] // 0
$distances->[$i]
Truth table100%2/2
A
A= $distances->[$i]
Aresult
11
00
2980 }
2981  
2982 130 return (\@symbols, \@dist_symbols, \@len_symbols, \@match_symbols);
2983 }
2984  
2985 48 *lz77_encode_symbolic = \&lz77_encode;
2986  
2987 44 ▶sub lz77_decode($symbols, $dist_symbols, $len_symbols, $match_symbols) {
Subroutine lz77_decode: called CC=16 SCAR=28.1
Pod documented
2988  
2989 44 my $data = '';
2990 44 my $data_len = 0;
2991  
2992 44 my @symbols = @$symbols;
2993 44 my @len_symbols = @$len_symbols;
2994 44 my @match_symbols = @$match_symbols;
2995 44 my @dist_symbols = @$dist_symbols;
2996  
2997 44 ▶ while (@symbols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@symbols) 4027 44 4071
2998  
2999 4027 ▶ my $len_byte = shift(@match_symbols) // confess "bad input";
Condition50%1/2
shift @match_symbols // confess("bad input")
l!l
40270
MC/DC0%0/1
shift @match_symbols // confess("bad input")
shift @match_symbols
Truth table50%1/2
A
A= shift @match_symbols
Aresult
11
00
3000  
3001 4027 my $literals_length = $len_byte >> 5;
3002 4027 my $match_len = $len_byte & 0b11111;
3003  
3004 4027 ▶ if ($literals_length == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length == 7) 328 3699 4027
3005 328 while (1) {
3006 328 ▶ my $byte_len = shift(@len_symbols) // confess "bad input";
Condition50%1/2
shift @len_symbols // confess("bad input")
l!l
3280
MC/DC0%0/1
shift @len_symbols // confess("bad input")
shift @len_symbols
Truth table50%1/2
A
A= shift @len_symbols
Aresult
11
00
3007 328 $literals_length += $byte_len;
3008 328 ▶ last if $byte_len != 255;
Branch: 1 branch
BranchTrueFalseTotal
if $byte_len != 255 328 0 328
3009 }
3010 }
3011  
3012 4027 ▶ if ($literals_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length > 0) 1348 2679 4027
3013 1348 $data .= pack("C*", splice(@symbols, 0, $literals_length));
3014 1348 $data_len += $literals_length;
3015 }
3016  
3017 4027 ▶ if ($match_len == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_len == 31) 618 3409 4027
3018 618 while (1) {
3019 631 ▶ my $byte_len = shift(@match_symbols) // confess "bad input";
Condition50%1/2
shift @match_symbols // confess("bad input")
l!l
6310
MC/DC0%0/1
shift @match_symbols // confess("bad input")
shift @match_symbols
Truth table50%1/2
A
A= shift @match_symbols
Aresult
11
00
3020 631 $match_len += $byte_len;
3021 631 ▶ last if $byte_len != 255;
Branch: 1 branch
BranchTrueFalseTotal
if $byte_len != 255 618 13 631
3022 }
3023 }
3024  
3025 4027 ▶ my $dist = shift(@dist_symbols) // confess "bad input";
Condition50%1/2
shift @dist_symbols // confess("bad input")
l!l
40270
MC/DC0%0/1
shift @dist_symbols // confess("bad input")
shift @dist_symbols
Truth table50%1/2
A
A= shift @dist_symbols
Aresult
11
00
3026  
3027 4027 ▶ if ($dist >= $match_len) { # non-overlapping matches
Branch: 2 branches
BranchTrueFalseTotal
if ($dist >= $match_len) { } 3864 163 4027
elsif ($dist == 1) { } 33 130 163
3028 3864 ▶ $data .= substr($data, $data_len - $dist, $match_len) // confess "bad input";
Condition50%1/2
substr($data, $data_len - $dist, $match_len) // confess("bad input")
l!l
38640
MC/DC0%0/1
substr($data, $data_len - $dist, $match_len) // confess("bad input")
substr($data, $data_len - $dist, $match_len)
Truth table50%1/2
A
A= substr($data, $data_len - $dist, $match_len)
Aresult
11
00
3029 }
3030 elsif ($dist == 1) { # run-length of last character
3031 33 $data .= substr($data, -1) x $match_len;
3032 }
3033 else { # overlapping matches
3034 130 ▶ my $pattern = substr($data, $data_len - $dist, $dist) // confess "bad input";
Condition50%1/2
substr($data, $data_len - $dist, $dist) // confess("bad input")
l!l
1300
MC/DC0%0/1
substr($data, $data_len - $dist, $dist) // confess("bad input")
substr($data, $data_len - $dist, $dist)
Truth table50%1/2
A
A= substr($data, $data_len - $dist, $dist)
Aresult
11
00
3035 130 my $full_reps = int(($match_len + $dist - 1) / $dist);
3036 130 ▶ $data .= substr($pattern x $full_reps, 0, $match_len) // confess "bad input";
Condition50%1/2
substr($pattern x $full_reps, 0, $match_len) // confess("bad input")
l!l
1300
MC/DC0%0/1
substr($pattern x $full_reps, 0, $match_len) // confess("bad input")
substr($pattern x $full_reps, 0, $match_len)
Truth table50%1/2
A
A= substr($pattern x $full_reps, 0, $match_len)
Aresult
11
00
3037 }
3038  
3039 4027 $data_len += $match_len;
3040 }
3041  
3042 44 return $data;
3043 }
3044  
3045 86 ▶sub lz77_decode_symbolic($symbols, $dist_symbols, $len_symbols, $match_symbols) {
Subroutine lz77_decode_symbolic: called CC=14 SCAR=26.5
Pod documented
3046  
3047 86 my @data;
3048 86 my $data_len = 0;
3049  
3050 86 my @symbols = @$symbols;
3051 86 my @len_symbols = @$len_symbols;
3052 86 my @match_symbols = @$match_symbols;
3053 86 my @dist_symbols = @$dist_symbols;
3054  
3055 86 ▶ while (@symbols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@symbols) 546 86 632
3056  
3057 546 ▶ my $len_byte = shift(@match_symbols) // confess "bad input";
Condition50%1/2
shift @match_symbols // confess("bad input")
l!l
5460
MC/DC0%0/1
shift @match_symbols // confess("bad input")
shift @match_symbols
Truth table50%1/2
A
A= shift @match_symbols
Aresult
11
00
3058  
3059 546 my $literals_length = $len_byte >> 5;
3060 546 my $match_len = $len_byte & 0b11111;
3061  
3062 546 ▶ if ($literals_length == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length == 7) 146 400 546
3063 146 while (1) {
3064 146 ▶ my $byte_len = shift(@len_symbols) // confess "bad input";
Condition50%1/2
shift @len_symbols // confess("bad input")
l!l
1460
MC/DC0%0/1
shift @len_symbols // confess("bad input")
shift @len_symbols
Truth table50%1/2
A
A= shift @len_symbols
Aresult
11
00
3065 146 $literals_length += $byte_len;
3066 146 ▶ last if $byte_len != 255;
Branch: 1 branch
BranchTrueFalseTotal
if $byte_len != 255 146 0 146
3067 }
3068 }
3069  
3070 546 ▶ if ($literals_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length > 0) 401 145 546
3071 401 push @data, splice(@symbols, 0, $literals_length);
3072 401 $data_len += $literals_length;
3073 }
3074  
3075 546 ▶ if ($match_len == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_len == 31) 28 518 546
3076 28 while (1) {
3077 37 ▶ my $byte_len = shift(@match_symbols) // confess "bad input";
Condition50%1/2
shift @match_symbols // confess("bad input")
l!l
370
MC/DC0%0/1
shift @match_symbols // confess("bad input")
shift @match_symbols
Truth table50%1/2
A
A= shift @match_symbols
Aresult
11
00
3078 37 $match_len += $byte_len;
3079 37 ▶ last if $byte_len != 255;
Branch: 1 branch
BranchTrueFalseTotal
if $byte_len != 255 28 9 37
3080 }
3081 }
3082  
3083 546 ▶ my $dist = shift(@dist_symbols) // confess "bad input";
Condition50%1/2
shift @dist_symbols // confess("bad input")
l!l
5460
MC/DC0%0/1
shift @dist_symbols // confess("bad input")
shift @dist_symbols
Truth table50%1/2
A
A= shift @dist_symbols
Aresult
11
00
3084  
3085 546 ▶ if ($dist >= $match_len) { # non-overlapping matches
Branch: 2 branches
BranchTrueFalseTotal
if ($dist >= $match_len) { } 441 105 546
elsif ($dist == 1) { } 33 72 105
3086 441 push @data, @data[scalar(@data) - $dist .. scalar(@data) - $dist + $match_len - 1];
3087 }
3088 elsif ($dist == 1) { # run-length of last character
3089 33 push @data, ($data[-1]) x $match_len;
3090 }
3091 else { # overlapping matches
3092 72 foreach my $j (1 .. $match_len) {
3093 411 push @data, $data[$data_len + $j - $dist - 1];
3094 }
3095 }
3096  
3097 546 $data_len += $match_len;
3098 }
3099  
3100 86 return \@data;
3101 }
3102  
3103 93 ▶sub lz77_compress($chunk, $entropy_sub = \&create_huffman_entry, $lzss_encoding_sub = \&lzss_encode) {
Subroutine lz77_compress: called CC=3 SCAR=11.0
Pod documented
3104 93 my ($symbols, $dist_symbols, $len_symbols, $match_symbols) = lz77_encode($chunk, $lzss_encoding_sub);
3105 93 $entropy_sub->($symbols) . $entropy_sub->($len_symbols) . $entropy_sub->($match_symbols) . obh_encode($dist_symbols, $entropy_sub);
3106 }
3107  
3108 48 *lz77_compress_symbolic = \&lz77_compress;
3109  
3110 46 ▶sub lz77_decompress($fh, $entropy_sub = \&decode_huffman_entry) {
Subroutine lz77_decompress: called CC=4 SCAR=13.9
Pod documented
3111  
3112 46 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 23 23 46
3113 23 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 23 0 23
3114 23 return __SUB__->($fh2, $entropy_sub);
3115 }
3116  
3117 23 my $symbols = $entropy_sub->($fh);
3118 23 my $len_symbols = $entropy_sub->($fh);
3119 23 my $match_symbols = $entropy_sub->($fh);
3120 23 my $dist_symbols = obh_decode($fh, $entropy_sub);
3121  
3122 23 lz77_decode($symbols, $dist_symbols, $len_symbols, $match_symbols);
3123 }
3124  
3125 134 ▶sub lz77_decompress_symbolic($fh, $entropy_sub = \&decode_huffman_entry) {
Subroutine lz77_decompress_symbolic: called CC=4 SCAR=13.9
Pod documented
3126  
3127 134 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 64 70 134
3128 64 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 64 0 64
3129 64 return __SUB__->($fh2, $entropy_sub);
3130 }
3131  
3132 70 my $symbols = $entropy_sub->($fh);
3133 70 my $len_symbols = $entropy_sub->($fh);
3134 70 my $match_symbols = $entropy_sub->($fh);
3135 70 my $dist_symbols = obh_decode($fh, $entropy_sub);
3136  
3137 70 lz77_decode_symbolic($symbols, $dist_symbols, $len_symbols, $match_symbols);
3138 }
3139  
3140 #########################
3141 # LZSS + DEFLATE encoding
3142 #########################
3143  
3144 97 ▶sub lzss_compress($chunk, $entropy_sub = \&create_huffman_entry, $lzss_encoding_sub = \&lzss_encode) {
Subroutine lzss_compress: called CC=3 SCAR=11.0
Pod documented
3145 97 my ($literals, $distances, $lengths) = $lzss_encoding_sub->($chunk);
3146 97 deflate_encode($literals, $distances, $lengths, $entropy_sub);
3147 }
3148  
3149 48 *lzss_compress_symbolic = \&lzss_compress;
3150  
3151 68 ▶sub lzss_decompress($fh, $entropy_sub = \&decode_huffman_entry) {
Subroutine lzss_decompress: called CC=4 SCAR=13.9
Pod documented
3152  
3153 68 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 34 34 68
3154 34 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 34 0 34
3155 34 return __SUB__->($fh2, $entropy_sub);
3156 }
3157  
3158 34 my ($literals, $distances, $lengths) = deflate_decode($fh, $entropy_sub);
3159 34 lzss_decode($literals, $distances, $lengths);
3160 }
3161  
3162 126 ▶sub lzss_decompress_symbolic($fh, $entropy_sub = \&decode_huffman_entry) {
Subroutine lzss_decompress_symbolic: called CC=4 SCAR=13.9
Pod documented
3163  
3164 126 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 63 63 126
3165 63 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 63 0 63
3166 63 return __SUB__->($fh2, $entropy_sub);
3167 }
3168  
3169 63 my ($literals, $distances, $lengths) = deflate_decode($fh, $entropy_sub);
3170 63 lzss_decode_symbolic($literals, $distances, $lengths);
3171 }
3172  
3173 #########################################
3174 # LZB -- LZSS with byte-oriented encoding
3175 #########################################
3176  
3177 28 ▶sub lzb_compress ($chunk, $lzss_encoding_sub = \&lzss_encode) {
Subroutine lzb_compress: called CC=14 SCAR=26.4
Pod documented
3178  
3179 28 my ($literals, $distances, $lengths) = do {
3180 28 local $LZ_MAX_DIST = (1 << 16) - 1;
3181 28 local $LZ_MAX_LEN = ~0;
3182 28 $lzss_encoding_sub->($chunk);
3183 };
3184  
3185 28 my $literals_end = $#{$literals};
3186 28 my $data = '';
3187  
3188 28 ▶ for (my $i = 0 ; $i <= $literals_end ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $literals_end) 3729 28 3757
3189  
3190 3729 my $j = $i;
3191 3729 ▶ while ($i <= $literals_end and defined($literals->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $literals_end and defined $literals->[$i]) 5642 3729 9371
Condition100%3/3
$i <= $literals_end and defined $literals->[$i]
!ll&&!rl&&r
2637035642
MC/DC100%2/2
$i <= $literals_end and defined $literals->[$i]
$i <= $literals_end defined $literals->[$i]
Truth table100%3/3
A and B
A= $i <= $literals_endB= defined $literals->[$i]
ABresult
0X0
100
111
3192 5642 ++$i;
3193 }
3194  
3195 3729 my $literals_length = $i - $j;
3196 3729 ▶ my $match_len = $lengths->[$i] // 0;
Condition100%2/2
$lengths->[$i] // 0
l!l
370326
MC/DC100%1/1
$lengths->[$i] // 0
$lengths->[$i]
Truth table100%2/2
A
A= $lengths->[$i]
Aresult
11
00
3197  
3198 3729 ▶ $data .= chr((($literals_length >= 7 ? 7 : $literals_length) << 5) | ($match_len >= 31 ? 31 : $match_len));
Branch: 2 branches
BranchTrueFalseTotal
$literals_length >= 7 ? : 239 3490 3729
$match_len >= 31 ? : 611 3118 3729
3199  
3200 3729 $literals_length -= 7;
3201 3729 $match_len -= 31;
3202  
3203 3729 ▶ while ($literals_length >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length >= 0) 239 3729 3968
3204 239 ▶ $data .= $literals_length >= 255 ? "\xff" : chr($literals_length);
Branch: 1 branch
BranchTrueFalseTotal
$literals_length >= 255 ? : 0 239 239
3205 239 $literals_length -= 255;
3206 }
3207  
3208 3729 ▶ if ($i > $j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $j) 1142 2587 3729
3209 1142 $data .= pack('C*', @{$literals}[$j .. $i - 1]);
3210 }
3211  
3212 3729 ▶ while ($match_len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_len >= 0) 622 3729 4351
3213 622 ▶ $data .= $match_len >= 255 ? "\xff" : chr($match_len);
Branch: 1 branch
BranchTrueFalseTotal
$match_len >= 255 ? : 11 611 622
3214 622 $match_len -= 255;
3215 }
3216  
3217 3729 ▶ $data .= pack('B*', sprintf('%016b', $distances->[$i] // 0));
Condition100%2/2
$distances->[$i] // 0
l!l
370326
MC/DC100%1/1
$distances->[$i] // 0
$distances->[$i]
Truth table100%2/2
A
A= $distances->[$i]
Aresult
11
00
3218 }
3219  
3220 28 return fibonacci_encode([length $data]) . $data;
3221 }
3222  
3223 56 ▶sub lzb_decompress($fh) {
Subroutine lzb_decompress: called CC=17 SCAR=28.6
Pod documented
3224  
3225 56 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 28 28 56
3226 28 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 28 0 28
3227 28 return __SUB__->($fh2);
3228 }
3229  
3230 28 my $data = '';
3231 28 my $search_window = '';
3232 28 my $search_window_size = 1 << 16;
3233  
3234 28 ▶ my $block_size = fibonacci_decode($fh)->[0] // confess "decompression error";
Condition50%1/2
fibonacci_decode($fh)->[0] // confess("decompression error")
l!l
280
MC/DC0%0/1
fibonacci_decode($fh)->[0] // confess("decompression error")
fibonacci_decode($fh)->[0]
Truth table50%1/2
A
A= fibonacci_decode($fh)->[0]
Aresult
11
00
3235  
3236 28 ▶ read($fh, (my $block), $block_size) // confess "Read error: $!";
Condition50%1/2
read($fh, my $block, $block_size) // confess("Read error: $!")
l!l
280
MC/DC0%0/1
read($fh, my $block, $block_size) // confess("Read error: $!")
read($fh, my $block, $block_size)
Truth table50%1/2
A
A= read($fh, my $block, $block_size)
Aresult
11
00
3237  
3238 28 ▶ while ($block ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($block ne "") 3729 28 3757
3239  
3240 3729 my $len_byte = ord substr($block, 0, 1, '');
3241  
3242 3729 my $literals_length = $len_byte >> 5;
3243 3729 my $match_len = $len_byte & 0b11111;
3244  
3245 3729 ▶ if ($literals_length == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length == 7) 239 3490 3729
3246 239 while (1) {
3247 239 my $byte_len = ord substr($block, 0, 1, '');
3248 239 $literals_length += $byte_len;
3249 239 ▶ last if $byte_len != 255;
Branch: 1 branch
BranchTrueFalseTotal
if $byte_len != 255 239 0 239
3250 }
3251 }
3252  
3253 3729 ▶ if ($literals_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length > 0) 1142 2587 3729
3254 1142 $search_window .= substr($block, 0, $literals_length, '');
3255 }
3256  
3257 3729 ▶ if ($match_len == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_len == 31) 611 3118 3729
3258 611 while (1) {
3259 622 my $byte_len = ord substr($block, 0, 1, '');
3260 622 $match_len += $byte_len;
3261 622 ▶ last if $byte_len != 255;
Branch: 1 branch
BranchTrueFalseTotal
if $byte_len != 255 611 11 622
3262 }
3263 }
3264  
3265 3729 my $offset = oct('0b' . unpack('B*', substr($block, 0, 2, '')));
3266  
3267 3729 ▶ if ($offset >= $match_len) { # non-overlapping matches
Branch: 2 branches
BranchTrueFalseTotal
if ($offset >= $match_len) { } 3593 136 3729
elsif ($offset == 1) { } 20 116 136
3268 3593 ▶ $search_window .= substr($search_window, length($search_window) - $offset, $match_len) // confess "bad input";
Condition50%1/2
substr($search_window, length($search_window) - $offset, $match_len) // confess("bad input")
l!l
35930
MC/DC0%0/1
substr($search_window, length($search_window) - $offset, $match_len) // confess("bad input")
substr($search_window, length($search_window) - $offset, $match_len)
Truth table50%1/2
A
A= substr($search_window, length($search_window) - $offset, $match_len)
Aresult
11
00
3269 }
3270 elsif ($offset == 1) { # run-length of last character
3271 20 $search_window .= substr($search_window, -1) x $match_len;
3272 }
3273 else { # overlapping matches
3274 116 ▶ my $pattern = substr($search_window, length($search_window) - $offset, $offset) // confess "bad input";
Condition50%1/2
substr($search_window, length($search_window) - $offset, $offset) // confess("bad input")
l!l
1160
MC/DC0%0/1
substr($search_window, length($search_window) - $offset, $offset) // confess("bad input")
substr($search_window, length($search_window) - $offset, $offset)
Truth table50%1/2
A
A= substr($search_window, length($search_window) - $offset, $offset)
Aresult
11
00
3275 116 my $full_reps = int(($match_len + $offset - 1) / $offset);
3276 116 ▶ $search_window .= substr($pattern x $full_reps, 0, $match_len) // confess "bad input";
Condition50%1/2
substr($pattern x $full_reps, 0, $match_len) // confess("bad input")
l!l
1160
MC/DC0%0/1
substr($pattern x $full_reps, 0, $match_len) // confess("bad input")
substr($pattern x $full_reps, 0, $match_len)
Truth table50%1/2
A
A= substr($pattern x $full_reps, 0, $match_len)
Aresult
11
00
3277 }
3278  
3279 3729 $data .= substr($search_window, -($match_len + $literals_length));
3280 3729 ▶ $search_window = substr($search_window, -$search_window_size) if (length($search_window) > 2 * $search_window_size);
Branch: 1 branch
BranchTrueFalseTotal
if length $search_window > 2 * $search_window_size 0 3729 3729
3281 }
3282  
3283 28 return $data;
3284 }
3285  
3286 ################################################################
3287 # Encode a list of symbols, using offset bits and huffman coding
3288 ################################################################
3289  
3290 254 ▶sub obh_encode ($distances, $entropy_sub = \&create_huffman_entry) {
Subroutine obh_encode: called CC=5 SCAR=16.1
Pod documented
3291  
3292 254 ▶ my $max_dist = max(@$distances) // 0;
Condition100%2/2
&max(@$distances) // 0
l!l
22430
MC/DC100%1/1
&max(@$distances) // 0
&max(@$distances)
Truth table100%2/2
A
A= &max(@$distances)
Aresult
11
00
3293 254 my ($DISTANCE_SYMBOLS) = make_deflate_tables($max_dist, 0);
3294  
3295 254 my @symbols;
3296 254 my $offset_bits = '';
3297  
3298 254 foreach my $dist (@$distances) {
3299  
3300 10378 my $i = find_deflate_index($dist, $DISTANCE_SYMBOLS);
3301 10378 my ($min, $bits) = @{$DISTANCE_SYMBOLS->[$i]};
3302  
3303 10378 push @symbols, $i;
3304  
3305 10378 ▶ if ($bits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits > 0) 9847 531 10378
3306 9847 $offset_bits .= sprintf('%0*b', $bits, $dist - $min);
3307 }
3308 }
3309  
3310 254 fibonacci_encode([$max_dist]) . $entropy_sub->(\@symbols) . pack('B*', $offset_bits);
3311 }
3312  
3313 306 ▶sub obh_decode ($fh, $entropy_sub = \&decode_huffman_entry) {
Subroutine obh_decode: called CC=6 SCAR=17.9
Pod documented
3314  
3315 306 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 52 254 306
3316 52 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 52 0 52
3317 52 return __SUB__->($fh2, $entropy_sub);
3318 }
3319  
3320 254 my $max_dist = fibonacci_decode($fh)->[0];
3321 254 my ($DISTANCE_SYMBOLS) = make_deflate_tables($max_dist, 0);
3322  
3323 254 my $symbols = $entropy_sub->($fh);
3324 254 my $bits_len = 0;
3325  
3326 254 foreach my $i (@$symbols) {
3327 10378 $bits_len += $DISTANCE_SYMBOLS->[$i][1];
3328 }
3329  
3330 254 my $bits = read_bits($fh, $bits_len);
3331  
3332 254 my @distances;
3333 254 foreach my $i (@$symbols) {
3334 10378 push @distances, $DISTANCE_SYMBOLS->[$i][0] + oct('0b' . substr($bits, 0, $DISTANCE_SYMBOLS->[$i][1], ''));
3335 }
3336  
3337 254 return \@distances;
3338 }
3339  
3340 #################
3341 # LZW Compression
3342 #################
3343  
3344 27 ▶sub lzw_encode ($uncompressed) {
Subroutine lzw_encode: called CC=5 SCAR=16.1
Pod documented
3345  
3346 # Build the dictionary
3347 27 my $dict_size = 256;
3348 27 my %dictionary;
3349  
3350 27 foreach my $i (0 .. $dict_size - 1) {
3351 6912 $dictionary{chr($i)} = $i;
3352 }
3353  
3354 27 my $w = '';
3355 27 my @result;
3356  
3357 27 foreach my $c (split(//, $uncompressed)) {
3358 38429 my $wc = $w . $c;
3359 38429 ▶ if (exists $dictionary{$wc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dictionary{$wc}) { } 29025 9404 38429
3360 29025 $w = $wc;
3361 }
3362 else {
3363 9404 push @result, $dictionary{$w};
3364  
3365 # Add wc to the dictionary
3366 9404 $dictionary{$wc} = $dict_size++;
3367 9404 $w = $c;
3368 }
3369 }
3370  
3371 # Output the code for w
3372 27 ▶ if ($w ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($w ne "") 26 1 27
3373 26 push @result, $dictionary{$w};
3374 }
3375  
3376 27 return \@result;
3377 }
3378  
3379 27 ▶sub lzw_decode ($compressed) {
Subroutine lzw_decode: called CC=5 SCAR=16.1
Pod documented
3380  
3381 27 ▶ @$compressed || return '';
Branch: 1 branch
BranchTrueFalseTotal
@$compressed or return "" 26 1 27
3382  
3383 # Build the dictionary
3384 26 my $dict_size = 256;
3385 26 my @dictionary = map { chr($_) } 0 .. $dict_size - 1;
3386  
3387 26 my $w = $dictionary[$compressed->[0]];
3388 26 my $result = $w;
3389  
3390 26 foreach my $j (1 .. $#$compressed) {
3391 9404 my $k = $compressed->[$j];
3392  
3393 9404 ▶ my $entry =
Branch: 2 branches
BranchTrueFalseTotal
$k < $dict_size ? : 9162 242 9404
$k == $dict_size ? : 242 0 242
3394 ($k < $dict_size) ? $dictionary[$k]
3395 : ($k == $dict_size) ? ($w . substr($w, 0, 1))
3396 : confess "Bad compressed k: $k";
3397  
3398 9404 $result .= $entry;
3399  
3400 # Add w+entry[0] to the dictionary
3401 9404 push @dictionary, $w . substr($entry, 0, 1);
3402 9404 ++$dict_size;
3403 9404 $w = $entry;
3404 }
3405  
3406 26 return $result;
3407 }
3408  
3409 27 ▶sub lzw_compress ($chunk, $enc_method = \&abc_encode) {
Subroutine lzw_compress: called CC=2 SCAR=6.9
Pod documented
3410 27 $enc_method->(lzw_encode($chunk));
3411 }
3412  
3413 54 ▶sub lzw_decompress ($fh, $dec_method = \&abc_decode) {
Subroutine lzw_decompress: called CC=4 SCAR=13.9
Pod documented
3414  
3415 54 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 27 27 54
3416 27 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 27 0 27
3417 27 return __SUB__->($fh2, $dec_method);
3418 }
3419  
3420 27 lzw_decode($dec_method->($fh));
3421 }
3422  
3423 ###################################
3424 # CRC-32 Pure Perl implementation
3425 ###################################
3426  
3427 4 ▶sub _create_crc32_table {
Subroutine _create_crc32_table: called CC=4 SCAR=13.9
3428 4 my @table;
3429 4 for my $i (0 .. 255) {
3430 1024 my $k = $i;
3431 1024 for (0 .. 7) {
3432 8192 ▶ if ($k & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k & 1) { } 4096 4096 8192
3433 4096 $k >>= 1;
3434 4096 $k ^= 0xedb88320;
3435 }
3436 else {
3437 4096 $k >>= 1;
3438 }
3439 }
3440 1024 push(@table, $k & 0xffffffff);
3441 }
3442 4 return \@table;
3443 }
3444  
3445 166 ▶sub crc32($str, $crc = 0) {
Subroutine crc32: called CC=3 SCAR=11.0
Pod documented
3446 166 state $crc_table = _create_crc32_table();
3447 166 $crc &= 0xffffffff;
3448 166 $crc ^= 0xffffffff;
3449 166 foreach my $c (unpack("C*", $str)) {
3450 223074 $crc = (($crc >> 8) ^ $crc_table->[($crc & 0xff) ^ $c]);
3451 }
3452 166 return (($crc & 0xffffffff) ^ 0xffffffff);
3453 }
3454  
3455 48 ▶use constant ADLER_NMAX => 5552;
Subroutine BEGIN: called CC=1 SCAR=0.0
3456  
3457 76 ▶sub adler32 ($str, $adler = 1) {
Subroutine adler32: called CC=5 SCAR=16.1
Pod documented
3458 76 my $s1 = $adler & 0xffff;
3459 76 my $s2 = ($adler >> 16) & 0xffff;
3460  
3461 76 my $len = length($str);
3462 76 my $pos = 0;
3463 76 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 74 76 150
3464 74 ▶ my $k = $len < ADLER_NMAX ? $len : ADLER_NMAX;
Branch: 1 branch
BranchTrueFalseTotal
$len < 5552 ? : 74 0 74
3465 74 $len -= $k;
3466 74 foreach my $c (unpack('C*', substr($str, $pos, $k))) {
3467 7216 $s1 += $c;
3468 7216 $s2 += $s1;
3469 }
3470 74 $pos += $k;
3471 74 $s1 %= 65521;
3472 74 $s2 %= 65521;
3473 }
3474 76 return (($s2 << 16) + $s1);
3475 }
3476  
3477 #############################
3478 # Bzip2 compression
3479 #############################
3480  
3481 22 ▶sub _bzip2_encode_code_lengths($dict) {
Subroutine _bzip2_encode_code_lengths: called CC=9 SCAR=23.6
3482 22 my @lengths;
3483  
3484 22 ▶ foreach my $symbol (0 .. max(keys %$dict) // 0) {
Condition50%1/2
&max(keys %$dict) // 0
l!l
220
MC/DC0%0/1
&max(keys %$dict) // 0
&max(keys %$dict)
Truth table50%1/2
A
A= &max(keys %$dict)
Aresult
11
00
3485 309 ▶ if (exists($dict->{$symbol})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dict->{$symbol}) { } 309 0 309
3486 309 push @lengths, length($dict->{$symbol});
3487 }
3488 else {
3489 0 confess "Incomplete Huffman tree not supported";
3490 0 push @lengths, 0;
3491 }
3492 }
3493  
3494 22 my $deltas = deltas(\@lengths);
3495  
3496 22 ▶ $VERBOSE && say STDERR "Code lengths: (@lengths)";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Code lengths: (@lengths)" 0 22 22
3497 22 ▶ $VERBOSE && say STDERR "Code lengths deltas: (@$deltas)";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Code lengths deltas: (@$deltas)" 0 22 22
3498  
3499 22 my $bitstring = int2bits(shift(@$deltas), 5) . '0';
3500  
3501 22 foreach my $d (@$deltas) {
3502 287 ▶ $bitstring .= (($d > 0) ? ('10' x $d) : ('11' x abs($d))) . '0';
Branch: 1 branch
BranchTrueFalseTotal
$d > 0 ? : 109 178 287
3503 }
3504  
3505 22 ▶ $VERBOSE && say STDERR "Deltas bitstring: $bitstring";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Deltas bitstring: $bitstring" 0 22 22
3506  
3507 22 return $bitstring;
3508 }
3509  
3510 46 ▶sub bzip2_compress($fh) {
Subroutine bzip2_compress: called CC=9 SCAR=22.1
Pod documented
3511  
3512 46 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 23 23 46
3513 23 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 23 0 23
3514 23 return __SUB__->($fh2);
3515 }
3516  
3517 23 my $level = 9;
3518  
3519 # There is a CRC32 issue on some non-compressible inputs, when using very large chunk sizes
3520 ## my $CHUNK_SIZE = 100_000 * $level;
3521 23 my $CHUNK_SIZE = 1 << 17;
3522  
3523 23 my $compressed = "BZh" . $level;
3524  
3525 23 state $block_header_bitstring = unpack("B48", "1AY&SY");
3526 23 state $block_footer_bitstring = unpack("B48", "\27rE8P\x90");
3527  
3528 23 my $bitstring = '';
3529 23 my $stream_crc32 = 0;
3530  
3531 23 ▶ while (read($fh, (my $chunk), $CHUNK_SIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $chunk, $CHUNK_SIZE) 22 23 45
3532  
3533 22 $bitstring .= $block_header_bitstring;
3534  
3535 22 my $crc32 = crc32(pack('b*', unpack('B*', $chunk)));
3536 22 ▶ $VERBOSE && say STDERR "CRC32: $crc32";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "CRC32: $crc32" 0 22 22
3537  
3538 22 $crc32 = oct('0b' . int2bits_lsb($crc32, 32));
3539 22 ▶ $VERBOSE && say STDERR "Bzip2-CRC32: $crc32";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Bzip2-CRC32: $crc32" 0 22 22
3540  
3541 22 $stream_crc32 = ($crc32 ^ (0xffffffff & ((0xffffffff & ($stream_crc32 << 1)) | (($stream_crc32 >> 31) & 0x1)))) & 0xffffffff;
3542  
3543 22 $bitstring .= int2bits($crc32, 32);
3544 22 $bitstring .= '0'; # not randomized
3545  
3546 22 my $rle4 = rle4_encode($chunk);
3547 22 my ($bwt, $bwt_idx) = bwt_encode(symbols2string($rle4));
3548  
3549 22 $bitstring .= int2bits($bwt_idx, 24);
3550  
3551 22 my ($mtf, $alphabet) = mtf_encode($bwt);
3552 22 ▶ $VERBOSE && say STDERR "Alphabet: (@$alphabet)";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Alphabet: (@$alphabet)" 0 22 22
3553  
3554 22 $bitstring .= unpack('B*', encode_alphabet_256($alphabet));
3555  
3556 22 my @zrle = reverse @{zrle_encode([reverse @$mtf])};
3557  
3558 22 my $eob = scalar(@$alphabet) + 1; # end-of-block symbol
3559 22 ▶ $VERBOSE && say STDERR "EOB symbol: $eob";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "EOB symbol: $eob" 0 22 22
3560 22 push @zrle, $eob;
3561  
3562 22 my ($dict) = huffman_from_symbols([@zrle, 0 .. $eob - 1]);
3563 22 my $num_sels = int(sprintf('%.0f', 0.5 + (scalar(@zrle) / 50))); # ceil(|zrle| / 50)
3564 22 ▶ $VERBOSE && say STDERR "Number of selectors: $num_sels";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Number of selectors: $num_sels" 0 22 22
3565  
3566 22 $bitstring .= int2bits(2, 3);
3567 22 $bitstring .= int2bits($num_sels, 15);
3568 22 $bitstring .= '0' x $num_sels;
3569  
3570 22 $bitstring .= _bzip2_encode_code_lengths($dict) x 2;
3571 22 $bitstring .= join('', @{$dict}{@zrle});
3572  
3573 22 $compressed .= pack('B*', substr($bitstring, 0, length($bitstring) - (length($bitstring) % 8), ''));
3574 }
3575  
3576 23 $bitstring .= $block_footer_bitstring;
3577 23 $bitstring .= int2bits($stream_crc32, 32);
3578 23 $compressed .= pack('B*', $bitstring);
3579  
3580 23 return $compressed;
3581 }
3582  
3583 #################################
3584 # Bzip2 decompression
3585 #################################
3586  
3587 64 ▶sub bzip2_decompress($fh) {
Subroutine bzip2_decompress: called CC=49 SCAR=42.0
Pod documented
3588  
3589 64 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 32 32 64
3590 32 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 32 0 32
3591 32 return __SUB__->($fh2);
3592 }
3593  
3594 32 state $MaxHuffmanBits = 20;
3595 32 my $decompressed = '';
3596  
3597 32 ▶ while (!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 35 32 67
3598  
3599 35 my $buffer = '';
3600  
3601 35 ▶ (bytes2int($fh, 2) == 0x425a and getc($fh) eq 'h')
Branch: 1 branch
BranchTrueFalseTotal
bytes2int($fh, 2) == 16986 and getc $fh eq "h" or confess("Not a valid Bzip2 archive") 35 0 35
Condition33%1/3
bytes2int($fh, 2) == 16986 and getc $fh eq "h"
!ll&&!rl&&r
0035
MC/DC0%0/2
bytes2int($fh, 2) == 16986 and getc $fh eq "h"
bytes2int($fh, 2) == 16986 getc $fh eq "h"
Truth table33%1/3
A and B
A= bytes2int($fh, 2) == 16986B= getc $fh eq "h"
ABresult
0X0
100
111
3602 or confess "Not a valid Bzip2 archive";
3603  
3604 35 my $level = getc($fh);
3605  
3606 35 ▶ if ($level !~ /^[1-9]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level =~ /^[1-9]\z/u) 0 35 35
3607 0 confess "Invalid level: $level";
3608 }
3609  
3610 35 ▶ $VERBOSE && say STDERR "Compression level: $level";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Compression level: $level" 0 35 35
3611  
3612 35 my $stream_crc32 = 0;
3613  
3614 35 ▶ while (!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 68 0 68
3615  
3616 68 my $block_magic = pack "B48", join('', map { read_bit($fh, \$buffer) } 1 .. 48);
3617  
3618 68 ▶ if ($block_magic eq "1AY&SY") { # BlockHeader
Branch: 2 branches
BranchTrueFalseTotal
if ($block_magic eq '1AY&SY') { } 33 35 68
elsif ($block_magic eq "\cWrE8P\220") { } 35 0 35
3619 33 ▶ $VERBOSE && say STDERR "Block header detected";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Block header detected" 0 33 33
3620  
3621 33 my $crc32 = bits2int($fh, 32, \$buffer);
3622 33 ▶ $VERBOSE && say STDERR "CRC32 = $crc32";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "CRC32 = $crc32" 0 33 33
3623  
3624 33 $stream_crc32 = ($crc32 ^ (0xffffffff & ((0xffffffff & ($stream_crc32 << 1)) | (($stream_crc32 >> 31) & 0x1)))) & 0xffffffff;
3625  
3626 33 my $randomized = read_bit($fh, \$buffer);
3627 33 ▶ $randomized == 0 or confess "randomized not supported";
Branch: 1 branch
BranchTrueFalseTotal
$randomized == 0 or confess("randomized not supported") 33 0 33
3628  
3629 33 my $bwt_idx = bits2int($fh, 24, \$buffer);
3630 33 ▶ $VERBOSE && say STDERR "BWT index: $bwt_idx";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "BWT index: $bwt_idx" 0 33 33
3631  
3632 33 my @alphabet;
3633 33 my $l1 = bits2int($fh, 16, \$buffer);
3634 33 for my $i (0 .. 15) {
3635 528 ▶ if ($l1 & (0x8000 >> $i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l1 & 32768 >> $i) 105 423 528
3636 105 my $l2 = bits2int($fh, 16, \$buffer);
3637 105 for my $j (0 .. 15) {
3638 1680 ▶ if ($l2 & (0x8000 >> $j)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l2 & 32768 >> $j) 337 1343 1680
3639 337 push @alphabet, 16 * $i + $j;
3640 }
3641 }
3642 }
3643 }
3644  
3645 33 ▶ $VERBOSE && say STDERR "MTF alphabet: (@alphabet)";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "MTF alphabet: (@alphabet)" 0 33 33
3646  
3647 33 my $num_trees = bits2int($fh, 3, \$buffer);
3648 33 ▶ $VERBOSE && say STDERR "Number or trees: $num_trees";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Number or trees: $num_trees" 0 33 33
3649  
3650 33 my $num_sels = bits2int($fh, 15, \$buffer);
3651 33 ▶ $VERBOSE && say STDERR "Number of selectors: $num_sels";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Number of selectors: $num_sels" 0 33 33
3652  
3653 33 my @idxs;
3654 33 for (1 .. $num_sels) {
3655 256 my $i = 0;
3656 256 ▶ while (read_bit($fh, \$buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read_bit($fh, \$buffer)) 2 256 258
3657 2 $i += 1;
3658 2 ▶ ($i < $num_trees) or confess "error";
Branch: 1 branch
BranchTrueFalseTotal
$i < $num_trees or confess("error") 2 0 2
3659 }
3660 256 push @idxs, $i;
3661 }
3662  
3663 33 my $sels = mtf_decode(\@idxs, [0 .. $num_trees - 1]);
3664 33 ▶ $VERBOSE && say STDERR "Selectors: (@$sels)";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Selectors: (@$sels)" 0 33 33
3665  
3666 33 my $num_syms = scalar(@alphabet) + 2;
3667  
3668 33 my @trees;
3669 33 for (1 .. $num_trees) {
3670 66 my @clens;
3671 66 my $clen = bits2int($fh, 5, \$buffer);
3672 66 for (1 .. $num_syms) {
3673 806 while (1) {
3674  
3675 1339 ▶ ($clen > 0 and $clen <= $MaxHuffmanBits) or confess "invalid code length: $clen";
Branch: 1 branch
BranchTrueFalseTotal
$clen > 0 and $clen <= $MaxHuffmanBits or confess("invalid code length: $clen") 1339 0 1339
Condition33%1/3
$clen > 0 and $clen <= $MaxHuffmanBits
!ll&&!rl&&r
001339
MC/DC0%0/2
$clen > 0 and $clen <= $MaxHuffmanBits
$clen > 0 $clen <= $MaxHuffmanBits
Truth table33%1/3
A and B
A= $clen > 0B= $clen <= $MaxHuffmanBits
ABresult
0X0
100
111
3676  
3677 1339 ▶ if (not read_bit($fh, \$buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (read_bit($fh, \$buffer)) 806 533 1339
3678 806 last;
3679 }
3680  
3681 533 ▶ $clen -= read_bit($fh, \$buffer) ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
read_bit($fh, \$buffer) ? : 213 320 533
3682 }
3683  
3684 806 push @clens, $clen;
3685 }
3686 66 push @trees, \@clens;
3687 66 ▶ $VERBOSE && say STDERR "Code lengths: (@clens)";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Code lengths: (@clens)" 0 66 66
3688 }
3689  
3690 33 foreach my $tree (@trees) {
3691 66 my $maxLen = max(@$tree);
3692 66 my $sum = 1 << $maxLen;
3693 66 for my $clen (@$tree) {
3694 806 $sum -= (1 << $maxLen) >> $clen;
3695 }
3696 66 ▶ $sum == 0 or confess "incomplete tree not supported: (@$tree)";
Branch: 1 branch
BranchTrueFalseTotal
$sum == 0 or confess("incomplete tree not supported: (@$tree)") 66 0 66
3697 }
3698  
3699 33 my @huffman_trees = map { (huffman_from_code_lengths($_))[1] } @trees;
3700  
3701 33 my $eob = @alphabet + 1;
3702  
3703 33 my @zrle;
3704 33 my $code = '';
3705  
3706 33 my $sel_idx = 0;
3707 33 my $tree = $huffman_trees[$sels->[$sel_idx]];
3708 33 my $decoded = 50;
3709  
3710 33 ▶ while (!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 44158 0 44158
3711 44158 $code .= read_bit($fh, \$buffer);
3712  
3713 44158 ▶ if (length($code) > $MaxHuffmanBits) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $code > $MaxHuffmanBits) 0 44158 44158
3714 0 confess "[!] Something went wrong: length of code `$code` is > $MaxHuffmanBits.";
3715 }
3716  
3717 44158 ▶ if (exists($tree->{$code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tree->{$code}) 11631 32527 44158
3718  
3719 11631 my $sym = $tree->{$code};
3720  
3721 11631 ▶ if ($sym == $eob) { # end of block marker
Branch: 1 branch
BranchTrueFalseTotal
if ($sym == $eob) 33 11598 11631
3722 33 ▶ $VERBOSE && say STDERR "EOB detected: $sym";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "EOB detected: $sym" 0 33 33
3723 33 last;
3724 }
3725  
3726 11598 push @zrle, $sym;
3727 11598 $code = '';
3728  
3729 11598 ▶ if (--$decoded <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$decoded <= 0) 223 11375 11598
3730 223 ▶ if (++$sel_idx <= $#$sels) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$sel_idx <= $sels->$#*) { } 223 0 223
3731 223 $tree = $huffman_trees[$sels->[$sel_idx]];
3732 }
3733 else {
3734 0 confess "No more selectors"; # should not happen
3735 }
3736 223 $decoded = 50;
3737 }
3738 }
3739 }
3740  
3741 33 my @mtf = reverse @{zrle_decode([reverse @zrle])};
3742 33 my $bwt = symbols2string mtf_decode(\@mtf, \@alphabet);
3743  
3744 33 my $rle4 = string2symbols bwt_decode($bwt, $bwt_idx);
3745 33 my $data = rle4_decode($rle4);
3746 33 my $dec = symbols2string($data);
3747  
3748 33 my $new_crc32 = oct('0b' . int2bits_lsb(crc32(pack('b*', unpack('B*', $dec))), 32));
3749  
3750 33 ▶ $VERBOSE && say STDERR "Computed CRC32: $new_crc32";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Computed CRC32: $new_crc32" 0 33 33
3751  
3752 33 ▶ if ($crc32 != $new_crc32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crc32 != $new_crc32) 0 33 33
3753 0 confess "CRC32 error: $crc32 (stored) != $new_crc32 (actual)";
3754 }
3755  
3756 33 $decompressed .= $dec;
3757 }
3758 elsif ($block_magic eq "\27rE8P\x90") { # BlockFooter
3759 35 ▶ $VERBOSE && say STDERR "Block footer detected";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Block footer detected" 0 35 35
3760 35 my $stored_stream_crc32 = bits2int($fh, 32, \$buffer);
3761 35 ▶ $VERBOSE && say STDERR "Stream CRC: $stored_stream_crc32";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Stream CRC: $stored_stream_crc32" 0 35 35
3762  
3763 35 ▶ if ($stored_stream_crc32 != $stream_crc32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stored_stream_crc32 != $stream_crc32) 0 35 35
3764 0 confess "Stream CRC32 error: $stored_stream_crc32 (stored) != $stream_crc32 (actual)";
3765 }
3766  
3767 35 $buffer = '';
3768 35 last;
3769 }
3770 else {
3771 0 confess "Unknown block magic: $block_magic";
3772 }
3773 }
3774  
3775 35 ▶ $VERBOSE && say STDERR "End of container";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "End of container" 0 35 35
3776 }
3777  
3778 32 return $decompressed;
3779 }
3780  
3781 ########################################
3782 # GZIP compressor
3783 ########################################
3784  
3785 166 ▶sub _code_length_encoding ($dict) {
Subroutine _code_length_encoding: called CC=14 SCAR=26.4
3786  
3787 166 my @lengths;
3788  
3789 166 ▶ foreach my $symbol (0 .. max(keys %$dict) // 0) {
Condition100%2/2
&max(keys %$dict) // 0
l!l
1588
MC/DC100%1/1
&max(keys %$dict) // 0
&max(keys %$dict)
Truth table100%2/2
A
A= &max(keys %$dict)
Aresult
11
00
3790 22726 ▶ if (exists($dict->{$symbol})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dict->{$symbol}) { } 1289 21437 22726
3791 1289 push @lengths, length($dict->{$symbol});
3792 }
3793 else {
3794 21437 push @lengths, 0;
3795 }
3796 }
3797  
3798 166 my $size = scalar(@lengths);
3799 166 my $rl = run_length(\@lengths);
3800 166 my $offset_bits = '';
3801  
3802 166 my @CL_symbols;
3803  
3804 166 foreach my $pair (@$rl) {
3805 1585 my ($v, $run) = @$pair;
3806  
3807 1585 ▶ while ($v == 0 and $run >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v == 0 and $run >= 3) 363 1585 1948
Condition100%3/3
$v == 0 and $run >= 3
!ll&&!rl&&r
1031554363
MC/DC100%2/2
$v == 0 and $run >= 3
$v == 0 $run >= 3
Truth table100%3/3
A and B
A= $v == 0B= $run >= 3
ABresult
0X0
100
111
3808  
3809 363 ▶ if ($run >= 11) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run >= 11) 278 85 363
3810 278 push @CL_symbols, 18;
3811 278 $run -= 11;
3812 278 $offset_bits .= int2bits_lsb(min($run, 127), 7);
3813 278 $run -= 127;
3814 }
3815  
3816 363 ▶ if ($run >= 3 and $run < 11) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run >= 3 and $run < 11) 89 274 363
Condition100%3/3
$run >= 3 and $run < 11
!ll&&!rl&&r
2482689
MC/DC100%2/2
$run >= 3 and $run < 11
$run >= 3 $run < 11
Truth table100%3/3
A and B
A= $run >= 3B= $run < 11
ABresult
0X0
100
111
3817 89 push @CL_symbols, 17;
3818 89 $run -= 3;
3819 89 $offset_bits .= int2bits_lsb(min($run, 7), 3);
3820 89 $run -= 7;
3821 }
3822 }
3823  
3824 1585 ▶ if ($v == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v == 0) 554 1031 1585
3825 554 ▶ push(@CL_symbols, (0) x $run) if ($run > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $run > 0 217 337 554
3826 554 next;
3827 }
3828  
3829 1031 push @CL_symbols, $v;
3830 1031 $run -= 1;
3831  
3832 1031 ▶ while ($run >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run >= 3) 17 1031 1048
3833 17 push @CL_symbols, 16;
3834 17 $run -= 3;
3835 17 $offset_bits .= int2bits_lsb(min($run, 3), 2);
3836 17 $run -= 3;
3837 }
3838  
3839 1031 ▶ push(@CL_symbols, ($v) x $run) if ($run > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $run > 0 171 860 1031
3840 }
3841  
3842 166 return (\@CL_symbols, $size, $offset_bits);
3843 }
3844  
3845 166 ▶sub _cl_encoded_bitstring ($cl_dict, $cl_symbols, $offset_bits) {
Subroutine _cl_encoded_bitstring: called CC=5 SCAR=16.1
3846  
3847 166 my $bitstring = '';
3848 166 foreach my $cl_symbol (@$cl_symbols) {
3849 1937 $bitstring .= $cl_dict->{$cl_symbol};
3850 1937 ▶ if ($cl_symbol == 16) {
Branch: 3 branches
BranchTrueFalseTotal
if ($cl_symbol == 16) { } 17 1920 1937
elsif ($cl_symbol == 17) { } 89 1831 1920
elsif ($cl_symbol == 18) { } 278 1553 1831
3851 17 $bitstring .= substr($offset_bits, 0, 2, '');
3852 }
3853 elsif ($cl_symbol == 17) {
3854 89 $bitstring .= substr($offset_bits, 0, 3, '');
3855 }
3856 elsif ($cl_symbol == 18) {
3857 278 $bitstring .= substr($offset_bits, 0, 7, '');
3858 }
3859 }
3860  
3861 166 return $bitstring;
3862 }
3863  
3864 83 ▶sub _create_cl_dictionary (@cl_symbols) {
Subroutine _create_cl_dictionary: called CC=5 SCAR=24.2
3865  
3866 83 my @keys;
3867 83 my $freq = frequencies(\@cl_symbols);
3868  
3869 83 while (1) {
3870 83 my ($cl_dict) = huffman_from_freq($freq);
3871  
3872 # The CL codes must have at most 7 bits
3873 83 ▶ return $cl_dict if all { length($_) <= 7 } values %$cl_dict;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &all(sub { length $_ <= 7; } , values %$cl_dict) 83 0 83
3874  
3875 0 ▶ if (scalar(@keys) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys == 0) 0 0 0
3876 0 @keys = sort { $freq->{$b} <=> $freq->{$a} } keys %$freq;
3877 }
3878  
3879 # Scale down the frequencies and try again
3880 0 foreach my $k (@keys) {
3881 0 ▶ if ($freq->{$k} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($freq->{$k} > 1) { } 0 0 0
3882 0 $freq->{$k} >>= 1;
3883 }
3884 else {
3885 0 last;
3886 }
3887 }
3888 }
3889 }
3890  
3891 83 ▶sub deflate_create_block_type_2 ($literals, $distances, $lengths) {
Subroutine deflate_create_block_type_2: called CC=15 SCAR=27.1
Pod documented
3892  
3893 83 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4); # minimum match length in LZ parsing
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 83 83
3894 83 local $LZ_MAX_LEN = 258; # maximum match length in LZ parsing
3895 83 local $LZ_MAX_DIST = (1 << 15) - 1; # maximum allowed back-reference distance in LZ parsing
3896  
3897 83 state $deflate_tables = [make_deflate_tables()];
3898 83 my ($DISTANCE_SYMBOLS, $LENGTH_SYMBOLS, $LENGTH_INDICES) = @$deflate_tables;
3899  
3900 83 my @CL_order = (16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15);
3901  
3902 83 my $bitstring = '01';
3903  
3904 83 my @len_symbols;
3905 my @dist_symbols;
3906 83 my $offset_bits = '';
3907  
3908 83 foreach my $k (0 .. $#$literals) {
3909  
3910 10779 ▶ if ($lengths->[$k] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lengths->[$k] == 0) 6852 3927 10779
3911 6852 push @len_symbols, $literals->[$k];
3912 6852 next;
3913 }
3914  
3915 3927 my $len = $lengths->[$k];
3916 3927 my $dist = $distances->[$k];
3917  
3918 {
3919 3927 my $len_idx = $LENGTH_INDICES->[$len];
3920 3927 my ($min, $bits) = @{$LENGTH_SYMBOLS->[$len_idx]};
3921  
3922 3927 push @len_symbols, [$len_idx + 256 - 1, $bits];
3923 3927 ▶ $offset_bits .= int2bits_lsb($len - $min, $bits) if ($bits > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $bits > 0 1569 2358 3927
3924 }
3925  
3926 {
3927 3927 my $dist_idx = find_deflate_index($dist, $DISTANCE_SYMBOLS);
3928 3927 my ($min, $bits) = @{$DISTANCE_SYMBOLS->[$dist_idx]};
3929  
3930 3927 push @dist_symbols, [$dist_idx - 1, $bits];
3931 3927 ▶ $offset_bits .= int2bits_lsb($dist - $min, $bits) if ($bits > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $bits > 0 3722 205 3927
3932 }
3933 }
3934  
3935 83 push @len_symbols, 256; # end-of-block marker
3936  
3937 83 ▶ my ($dict) = huffman_from_symbols([map { ref($_) eq 'ARRAY' ? $_->[0] : $_ } @len_symbols]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 3927 6935 10862
3938 83 my ($dist_dict) = huffman_from_symbols([map { $_->[0] } @dist_symbols]);
3939  
3940 83 my ($LL_code_lengths, $LL_cl_len, $LL_offset_bits) = _code_length_encoding($dict);
3941 83 my ($distance_code_lengths, $distance_cl_len, $distance_offset_bits) = _code_length_encoding($dist_dict);
3942  
3943 83 my $cl_dict = _create_cl_dictionary(@$LL_code_lengths, @$distance_code_lengths);
3944  
3945 83 my @CL_code_lenghts;
3946 83 foreach my $symbol (0 .. 18) {
3947 1577 ▶ if (exists($cl_dict->{$symbol})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cl_dict->{$symbol}) { } 513 1064 1577
3948 513 push @CL_code_lenghts, length($cl_dict->{$symbol});
3949 }
3950 else {
3951 1064 push @CL_code_lenghts, 0;
3952 }
3953 }
3954  
3955 # Put the CL codes in the required order
3956 83 @CL_code_lenghts = @CL_code_lenghts[@CL_order];
3957  
3958 83 ▶ while (scalar(@CL_code_lenghts) > 4 and $CL_code_lenghts[-1] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @CL_code_lenghts > 4 and $CL_code_lenghts[-1] == 0) 119 83 202
Condition66%2/3
scalar @CL_code_lenghts > 4 and $CL_code_lenghts[-1] == 0
!ll&&!rl&&r
083119
MC/DC50%1/2
scalar @CL_code_lenghts > 4 and $CL_code_lenghts[-1] == 0
scalar @CL_code_lenghts > 4 $CL_code_lenghts[-1] == 0
Truth table66%2/3
A and B
A= scalar @CL_code_lenghts > 4B= $CL_code_lenghts[-1] == 0
ABresult
0X0
100
111
3959 119 pop @CL_code_lenghts;
3960 }
3961  
3962 83 my $CL_code_lengths_bitstring = join('', map { int2bits_lsb($_, 3) } @CL_code_lenghts);
3963  
3964 83 my $LL_code_lengths_bitstring = _cl_encoded_bitstring($cl_dict, $LL_code_lengths, $LL_offset_bits);
3965 83 my $distance_code_lengths_bitstring = _cl_encoded_bitstring($cl_dict, $distance_code_lengths, $distance_offset_bits);
3966  
3967 # (5 bits) HLIT = (number of LL code entries present) - 257
3968 83 my $HLIT = $LL_cl_len - 257;
3969  
3970 # (5 bits) HDIST = (number of distance code entries present) - 1
3971 83 my $HDIST = $distance_cl_len - 1;
3972  
3973 # (4 bits) HCLEN = (number of CL code entries present) - 4
3974 83 my $HCLEN = scalar(@CL_code_lenghts) - 4;
3975  
3976 83 $bitstring .= int2bits_lsb($HLIT, 5);
3977 83 $bitstring .= int2bits_lsb($HDIST, 5);
3978 83 $bitstring .= int2bits_lsb($HCLEN, 4);
3979  
3980 83 $bitstring .= $CL_code_lengths_bitstring;
3981 83 $bitstring .= $LL_code_lengths_bitstring;
3982 83 $bitstring .= $distance_code_lengths_bitstring;
3983  
3984 83 foreach my $symbol (@len_symbols) {
3985 10862 ▶ if (ref($symbol) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $symbol eq 'ARRAY') { } 3927 6935 10862
3986  
3987 3927 my ($len, $len_offset) = @$symbol;
3988 3927 $bitstring .= $dict->{$len};
3989 3927 ▶ $bitstring .= substr($offset_bits, 0, $len_offset, '') if ($len_offset > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $len_offset > 0 1569 2358 3927
3990  
3991 3927 my ($dist, $dist_offset) = @{shift(@dist_symbols)};
3992 3927 $bitstring .= $dist_dict->{$dist};
3993 3927 ▶ $bitstring .= substr($offset_bits, 0, $dist_offset, '') if ($dist_offset > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $dist_offset > 0 3722 205 3927
3994 }
3995 else {
3996 6935 $bitstring .= $dict->{$symbol};
3997 }
3998 }
3999  
4000 83 return $bitstring;
4001 }
4002  
4003 83 ▶sub deflate_create_block_type_1 ($literals, $distances, $lengths) {
Subroutine deflate_create_block_type_1: called CC=11 SCAR=24.0
Pod documented
4004  
4005 83 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4); # minimum match length in LZ parsing
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 83 83
4006 83 local $LZ_MAX_LEN = 258; # maximum match length in LZ parsing
4007 83 local $LZ_MAX_DIST = (1 << 15) - 1; # maximum allowed back-reference distance in LZ parsing
4008  
4009 83 state $deflate_tables = [make_deflate_tables()];
4010 83 my ($DISTANCE_SYMBOLS, $LENGTH_SYMBOLS, $LENGTH_INDICES) = @$deflate_tables;
4011  
4012 83 state $dict;
4013 83 state $dist_dict;
4014  
4015 83 ▶ if (!defined($dict)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dict) 4 79 83
4016  
4017 4 my @code_lengths = (0) x 288;
4018 4 foreach my $i (0 .. 143) {
4019 576 $code_lengths[$i] = 8;
4020 }
4021 4 foreach my $i (144 .. 255) {
4022 448 $code_lengths[$i] = 9;
4023 }
4024 4 foreach my $i (256 .. 279) {
4025 96 $code_lengths[$i] = 7;
4026 }
4027 4 foreach my $i (280 .. 287) {
4028 32 $code_lengths[$i] = 8;
4029 }
4030  
4031 4 ($dict) = huffman_from_code_lengths(\@code_lengths);
4032 4 ($dist_dict) = huffman_from_code_lengths([(5) x 32]);
4033 }
4034  
4035 83 my $bitstring = '10';
4036  
4037 83 foreach my $k (0 .. $#$literals) {
4038  
4039 10779 ▶ if ($lengths->[$k] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lengths->[$k] == 0) 6852 3927 10779
4040 6852 $bitstring .= $dict->{$literals->[$k]};
4041 6852 next;
4042 }
4043  
4044 3927 my $len = $lengths->[$k];
4045 3927 my $dist = $distances->[$k];
4046  
4047 {
4048 3927 my $len_idx = $LENGTH_INDICES->[$len];
4049 3927 my ($min, $bits) = @{$LENGTH_SYMBOLS->[$len_idx]};
4050  
4051 3927 $bitstring .= $dict->{$len_idx + 256 - 1};
4052 3927 ▶ $bitstring .= int2bits_lsb($len - $min, $bits) if ($bits > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $bits > 0 1569 2358 3927
4053 }
4054  
4055 {
4056 3927 my $dist_idx = find_deflate_index($dist, $DISTANCE_SYMBOLS);
4057 3927 my ($min, $bits) = @{$DISTANCE_SYMBOLS->[$dist_idx]};
4058  
4059 3927 $bitstring .= $dist_dict->{$dist_idx - 1};
4060 3927 ▶ $bitstring .= int2bits_lsb($dist - $min, $bits) if ($bits > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $bits > 0 3722 205 3927
4061 }
4062 }
4063  
4064 83 $bitstring .= $dict->{256}; # end-of-block symbol
4065  
4066 83 return $bitstring;
4067 }
4068  
4069 2 ▶sub deflate_create_block_type_0_header($chunk) {
Subroutine deflate_create_block_type_0_header: called CC=1 SCAR=0.0
Pod documented
4070  
4071 2 my $chunk_len = length($chunk);
4072 2 my $len = int2bits_lsb($chunk_len, 16);
4073 2 my $nlen = int2bits_lsb((~$chunk_len) & 0xffff, 16);
4074  
4075 2 $len . $nlen;
4076 }
4077  
4078 94 ▶sub gzip_compress ($in_fh, $lzss_encoding_sub = \&lzss_encode) {
Subroutine gzip_compress: called CC=14 SCAR=27.4
Pod documented
4079  
4080 94 ▶ if (ref($in_fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in_fh eq "") 47 47 94
4081 47 ▶ open(my $fh2, '<:raw', \$in_fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$in_fh or confess("error: $!") 47 0 47
4082 47 return __SUB__->($fh2, $lzss_encoding_sub);
4083 }
4084  
4085 47 my $compressed = '';
4086  
4087 47 open my $out_fh, '>:raw', \$compressed;
4088  
4089 47 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4); # minimum match length in LZ parsing
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 47 47
4090 47 local $LZ_MAX_LEN = 258; # maximum match length in LZ parsing
4091 47 local $LZ_MAX_DIST = (1 << 15) - 1; # maximum allowed back-reference distance in LZ parsing
4092  
4093 47 state $MAGIC = pack('C*', 0x1f, 0x8b); # magic MIME type
4094 47 state $CM = chr(0x08); # 0x08 = DEFLATE
4095 47 state $FLAGS = chr(0x00); # flags
4096 47 state $MTIME = pack('C*', (0x00) x 4); # modification time
4097 47 state $XFLAGS = chr(0x00); # extra flags
4098 47 state $OS = chr(0x03); # 0x03 = Unix
4099  
4100 47 print $out_fh $MAGIC, $CM, $FLAGS, $MTIME, $XFLAGS, $OS;
4101  
4102 47 my $total_length = 0;
4103 47 my $crc32 = 0;
4104  
4105 47 my $bitstring = '';
4106  
4107 47 ▶ if (eof($in_fh)) { # empty file
Branch: 1 branch
BranchTrueFalseTotal
if (eof $in_fh) 2 45 47
4108 2 $bitstring = '1' . '10' . '0000000';
4109 }
4110  
4111 47 state $CHUNK_SIZE = (1 << 15) - 1;
4112  
4113 47 ▶ while (read($in_fh, (my $chunk), $CHUNK_SIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $in_fh, my $chunk, $CHUNK_SIZE) 47 47 94
4114  
4115 47 $crc32 = crc32($chunk, $crc32);
4116 47 $total_length += length($chunk);
4117 47 ▶ $bitstring .= eof($in_fh) ? '1' : '0';
Branch: 1 branch
BranchTrueFalseTotal
eof $in_fh ? : 45 2 47
4118  
4119 47 my ($literals, $distances, $lengths) = $lzss_encoding_sub->($chunk);
4120  
4121 47 my $bt1_bitstring = deflate_create_block_type_1($literals, $distances, $lengths);
4122  
4123 # When block type 1 is larger than the input, then we have random uncompressible data: use block type 0
4124 47 ▶ if ((length($bt1_bitstring) >> 3) > length($chunk) + 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($bt1_bitstring) >> 3 > length($chunk) + 5) 0 47 47
4125  
4126 0 ▶ $VERBOSE && say STDERR ":: Using block type: 0";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Using block type: 0" 0 0 0
4127  
4128 0 $bitstring .= '00';
4129  
4130 0 print $out_fh pack('b*', $bitstring); # pads to a byte
4131 0 print $out_fh pack('b*', deflate_create_block_type_0_header($chunk));
4132 0 print $out_fh $chunk;
4133  
4134 0 $bitstring = '';
4135 0 next;
4136 }
4137  
4138 47 my $bt2_bitstring = deflate_create_block_type_2($literals, $distances, $lengths);
4139  
4140 # When block type 2 is larger than block type 1, then we may have very small data
4141 47 ▶ if (length($bt2_bitstring) > length($bt1_bitstring)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bt2_bitstring > length $bt1_bitstring) { } 41 6 47
4142 41 ▶ $VERBOSE && say STDERR ":: Using block type: 1";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Using block type: 1" 0 41 41
4143 41 $bitstring .= $bt1_bitstring;
4144 }
4145 else {
4146 6 ▶ $VERBOSE && say STDERR ":: Using block type: 2";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Using block type: 2" 0 6 6
4147 6 $bitstring .= $bt2_bitstring;
4148 }
4149  
4150 47 print $out_fh pack('b*', substr($bitstring, 0, length($bitstring) - (length($bitstring) % 8), ''));
4151 }
4152  
4153 47 ▶ if ($bitstring ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitstring ne "") 45 2 47
4154 45 print $out_fh pack('b*', $bitstring);
4155 }
4156  
4157 47 print $out_fh int2bytes_lsb($crc32, 4);
4158 47 print $out_fh int2bytes_lsb($total_length, 4);
4159  
4160 47 return $compressed;
4161 }
4162  
4163 ###################
4164 # GZIP DECOMPRESSOR
4165 ###################
4166  
4167 4 ▶sub deflate_extract_block_type_0 ($in_fh, $buffer, $search_window) {
Subroutine deflate_extract_block_type_0: called CC=6 SCAR=18.8
Pod documented
4168  
4169 4 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4); # minimum match length in LZ parsing
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 4 4
4170 4 local $LZ_MAX_LEN = 258; # maximum match length in LZ parsing
4171 4 local $LZ_MAX_DIST = 32768; # maximum allowed back-reference distance in LZ parsing
4172  
4173 4 $$buffer = '';
4174  
4175 4 my $len = bytes2int_lsb($in_fh, 2);
4176 4 my $nlen = bytes2int_lsb($in_fh, 2);
4177 4 my $expected_nlen = (~$len) & 0xffff;
4178  
4179 4 ▶ if ($expected_nlen != $nlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected_nlen != $nlen) { } 0 4 4
4180 0 confess "[!] The ~length value is not correct: $nlen (actual) != $expected_nlen (expected)";
4181 }
4182 else {
4183 4 ▶ $VERBOSE && print STDERR ":: Chunk length: $len\n";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and print STDERR ":: Chunk length: $len\n" 0 4 4
4184 }
4185  
4186 4 ▶ read($in_fh, (my $chunk), $len) // confess "Read error: $!";
Condition50%1/2
read($in_fh, my $chunk, $len) // confess("Read error: $!")
l!l
40
MC/DC0%0/1
read($in_fh, my $chunk, $len) // confess("Read error: $!")
read($in_fh, my $chunk, $len)
Truth table50%1/2
A
A= read($in_fh, my $chunk, $len)
Aresult
11
00
4187 4 $$search_window .= $chunk;
4188  
4189 4 ▶ $$search_window = substr($$search_window, -$LZ_MAX_DIST)
Branch: 1 branch
BranchTrueFalseTotal
if length $$search_window > 2 * $Compression::Util::LZ_MAX_DIST 0 4 4
4190 if (length($$search_window) > 2 * $LZ_MAX_DIST);
4191  
4192 4 return $chunk;
4193 }
4194  
4195 101 ▶sub _deflate_decode_huffman($in_fh, $buffer, $rev_dict, $dist_rev_dict, $search_window) {
Subroutine _deflate_decode_huffman: called CC=20 SCAR=30.5
4196  
4197 101 state $deflate_tables = [make_deflate_tables()];
4198 101 my ($DISTANCE_SYMBOLS, $LENGTH_SYMBOLS, $LENGTH_INDICES) = @$deflate_tables;
4199  
4200 101 my $data = '';
4201 101 my $code = '';
4202  
4203 101 my $max_ll_code_len = max(map { length($_) } keys %$rev_dict);
4204 101 my $max_dist_code_len = max(map { length($_) } keys %$dist_rev_dict);
4205  
4206 101 while (1) {
4207 67663 ▶ if ($$buffer eq '') { $$buffer = unpack('B*', getc($in_fh) // confess "can't read bit"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffer eq "") 8474 59189 67663
Condition50%1/2
getc $in_fh // confess("can't read bit")
l!l
84740
MC/DC0%0/1
getc $in_fh // confess("can't read bit")
getc $in_fh
Truth table50%1/2
A
A= getc $in_fh
Aresult
11
00
4208 67663 $code .= chop($$buffer);
4209  
4210 67663 ▶ if (length($code) > $max_ll_code_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $code > $max_ll_code_len) 0 67663 67663
4211 0 confess "[!] Something went wrong: length of LL code `$code` is > $max_ll_code_len.";
4212 }
4213  
4214 67663 ▶ if (exists($rev_dict->{$code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rev_dict->{$code}) 11050 56613 67663
4215  
4216 11050 my $symbol = $rev_dict->{$code};
4217  
4218 11050 ▶ if ($symbol <= 255) {
Branch: 2 branches
BranchTrueFalseTotal
if ($symbol <= 255) { } 7018 4032 11050
elsif ($symbol == 256) { } 101 3931 4032
4219 7018 $data .= chr($symbol);
4220 7018 $$search_window .= chr($symbol);
4221 }
4222 elsif ($symbol == 256) { # end-of-block marker
4223 101 $code = '';
4224 101 last;
4225 }
4226 else { # LZSS decoding
4227 3931 my ($length, $LL_bits) = @{$LENGTH_SYMBOLS->[$symbol - 256 + 1]};
4228 3931 ▶ $length += bits2int_lsb($in_fh, $LL_bits, $buffer) if ($LL_bits > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $LL_bits > 0 1566 2365 3931
4229  
4230 3931 my $dist_code = '';
4231  
4232 3931 while (1) {
4233 17601 ▶ if ($$buffer eq '') { $$buffer = unpack('B*', getc($in_fh) // confess "can't read bit"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffer eq "") 2221 15380 17601
Condition50%1/2
getc $in_fh // confess("can't read bit")
l!l
22210
MC/DC0%0/1
getc $in_fh // confess("can't read bit")
getc $in_fh
Truth table50%1/2
A
A= getc $in_fh
Aresult
11
00
4234 17601 $dist_code .= chop($$buffer);
4235  
4236 17601 ▶ if (length($dist_code) > $max_dist_code_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dist_code > $max_dist_code_len) 0 17601 17601
4237 0 confess "[!] Something went wrong: length of distance code `$dist_code` is > $max_dist_code_len.";
4238 }
4239  
4240 17601 ▶ if (exists($dist_rev_dict->{$dist_code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dist_rev_dict->{$dist_code}) 3931 13670 17601
4241 3931 last;
4242 }
4243 }
4244  
4245 3931 my ($dist, $dist_bits) = @{$DISTANCE_SYMBOLS->[$dist_rev_dict->{$dist_code} + 1]};
4246 3931 ▶ $dist += bits2int_lsb($in_fh, $dist_bits, $buffer) if ($dist_bits > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $dist_bits > 0 3742 189 3931
4247  
4248 3931 ▶ if ($dist >= $length) { # non-overlapping matches
Branch: 2 branches
BranchTrueFalseTotal
if ($dist >= $length) { } 3720 211 3931
elsif ($dist == 1) { } 43 168 211
4249 3720 ▶ $$search_window .= substr($$search_window, length($$search_window) - $dist, $length) // confess "bad input";
Condition50%1/2
substr($$search_window, length($$search_window) - $dist, $length) // confess("bad input")
l!l
37200
MC/DC0%0/1
substr($$search_window, length($$search_window) - $dist, $length) // confess("bad input")
substr($$search_window, length($$search_window) - $dist, $length)
Truth table50%1/2
A
A= substr($$search_window, length($$search_window) - $dist, $length)
Aresult
11
00
4250 }
4251 elsif ($dist == 1) {
4252 43 $$search_window .= substr($$search_window, -1) x $length;
4253 }
4254 else { # overlapping matches
4255 168 ▶ my $pattern = substr($$search_window, length($$search_window) - $dist, $dist) // confess "bad input";
Condition50%1/2
substr($$search_window, length($$search_window) - $dist, $dist) // confess("bad input")
l!l
1680
MC/DC0%0/1
substr($$search_window, length($$search_window) - $dist, $dist) // confess("bad input")
substr($$search_window, length($$search_window) - $dist, $dist)
Truth table50%1/2
A
A= substr($$search_window, length($$search_window) - $dist, $dist)
Aresult
11
00
4256 168 my $full_reps = int(($length + $dist - 1) / $dist);
4257 168 ▶ $$search_window .= substr($pattern x $full_reps, 0, $length) // confess "bad input";
Condition50%1/2
substr($pattern x $full_reps, 0, $length) // confess("bad input")
l!l
1680
MC/DC0%0/1
substr($pattern x $full_reps, 0, $length) // confess("bad input")
substr($pattern x $full_reps, 0, $length)
Truth table50%1/2
A
A= substr($pattern x $full_reps, 0, $length)
Aresult
11
00
4258 }
4259  
4260 3931 $data .= substr($$search_window, -$length);
4261 }
4262  
4263 10949 $code = '';
4264 }
4265 }
4266  
4267 101 ▶ if ($code ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne "") 0 101 101
4268 0 confess "[!] Something went wrong: code `$code` is not empty!";
4269 }
4270  
4271 101 ▶ $$search_window = substr($$search_window, -$LZ_MAX_DIST)
Branch: 1 branch
BranchTrueFalseTotal
if length $$search_window > 2 * $Compression::Util::LZ_MAX_DIST 0 101 101
4272 if (length($$search_window) > 2 * $LZ_MAX_DIST);
4273  
4274 101 return $data;
4275 }
4276  
4277 89 ▶sub deflate_extract_block_type_1 ($in_fh, $buffer, $search_window) {
Subroutine deflate_extract_block_type_1: called CC=7 SCAR=19.5
Pod documented
4278  
4279 89 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4); # minimum match length in LZ parsing
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 89 89
4280 89 local $LZ_MAX_LEN = 258; # maximum match length in LZ parsing
4281 89 local $LZ_MAX_DIST = 32768; # maximum allowed back-reference distance in LZ parsing
4282  
4283 89 state $rev_dict;
4284 89 state $dist_rev_dict;
4285  
4286 89 ▶ if (!defined($rev_dict)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rev_dict) 1 88 89
4287  
4288 1 my @code_lengths = (0) x 288;
4289 1 foreach my $i (0 .. 143) {
4290 144 $code_lengths[$i] = 8;
4291 }
4292 1 foreach my $i (144 .. 255) {
4293 112 $code_lengths[$i] = 9;
4294 }
4295 1 foreach my $i (256 .. 279) {
4296 24 $code_lengths[$i] = 7;
4297 }
4298 1 foreach my $i (280 .. 287) {
4299 8 $code_lengths[$i] = 8;
4300 }
4301  
4302 1 (undef, $rev_dict) = huffman_from_code_lengths(\@code_lengths);
4303 1 (undef, $dist_rev_dict) = huffman_from_code_lengths([(5) x 32]);
4304 }
4305  
4306 89 _deflate_decode_huffman($in_fh, $buffer, $rev_dict, $dist_rev_dict, $search_window);
4307 }
4308  
4309 24 ▶sub _decode_CL_lengths($in_fh, $buffer, $CL_rev_dict, $size) {
Subroutine _decode_CL_lengths: called CC=10 SCAR=23.9
4310  
4311 24 my @lengths;
4312 24 my $code = '';
4313  
4314 24 while (1) {
4315 4430 $code .= read_bit_lsb($in_fh, $buffer);
4316  
4317 4430 ▶ if (length($code) > 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $code > 7) 0 4430 4430
4318 0 confess "[!] Something went wrong: length of CL code `$code` is > 7.";
4319 }
4320  
4321 4430 ▶ if (exists($CL_rev_dict->{$code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CL_rev_dict->{$code}) 1444 2986 4430
4322 1444 my $CL_symbol = $CL_rev_dict->{$code};
4323  
4324 1444 ▶ if ($CL_symbol <= 15) {
Branch: 4 branches
BranchTrueFalseTotal
if ($CL_symbol <= 15) { } 1364 80 1444
elsif ($CL_symbol == 16) { } 17 63 80
elsif ($CL_symbol == 17) { } 40 23 63
elsif ($CL_symbol == 18) { } 23 0 23
4325 1364 push @lengths, $CL_symbol;
4326 }
4327 elsif ($CL_symbol == 16) {
4328 17 push @lengths, ($lengths[-1]) x (3 + bits2int_lsb($in_fh, 2, $buffer));
4329 }
4330 elsif ($CL_symbol == 17) {
4331 40 push @lengths, (0) x (3 + bits2int_lsb($in_fh, 3, $buffer));
4332 }
4333 elsif ($CL_symbol == 18) {
4334 23 push @lengths, (0) x (11 + bits2int_lsb($in_fh, 7, $buffer));
4335 }
4336 else {
4337 0 confess "Unknown CL symbol: $CL_symbol";
4338 }
4339  
4340 1444 $code = '';
4341 1444 ▶ last if (scalar(@lengths) >= $size);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @lengths >= $size 24 1420 1444
4342 }
4343 }
4344  
4345 24 ▶ if (scalar(@lengths) != $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @lengths != $size) 0 24 24
4346 0 confess "Something went wrong: size $size (expected) != ", scalar(@lengths);
4347 }
4348  
4349 24 ▶ if ($code ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne "") 0 24 24
4350 0 confess "Something went wrong: code `$code` is not empty!";
4351 }
4352  
4353 24 return @lengths;
4354 }
4355  
4356 12 ▶sub deflate_extract_block_type_2 ($in_fh, $buffer, $search_window) {
Subroutine deflate_extract_block_type_2: called CC=7 SCAR=19.8
Pod documented
4357  
4358 12 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4); # minimum match length in LZ parsing
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 12 12
4359 12 local $LZ_MAX_LEN = 258; # maximum match length in LZ parsing
4360 12 local $LZ_MAX_DIST = 32768; # maximum allowed back-reference distance in LZ parsing
4361  
4362 # (5 bits) HLIT = (number of LL code entries present) - 257
4363 12 my $HLIT = bits2int_lsb($in_fh, 5, $buffer) + 257;
4364  
4365 # (5 bits) HDIST = (number of distance code entries present) - 1
4366 12 my $HDIST = bits2int_lsb($in_fh, 5, $buffer) + 1;
4367  
4368 # (4 bits) HCLEN = (number of CL code entries present) - 4
4369 12 my $HCLEN = bits2int_lsb($in_fh, 4, $buffer) + 4;
4370  
4371 12 ▶ $VERBOSE && say STDERR ":: Number of LL codes: $HLIT";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Number of LL codes: $HLIT" 0 12 12
4372 12 ▶ $VERBOSE && say STDERR ":: Number of dist codes: $HDIST";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Number of dist codes: $HDIST" 0 12 12
4373 12 ▶ $VERBOSE && say STDERR ":: Number of CL codes: $HCLEN";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Number of CL codes: $HCLEN" 0 12 12
4374  
4375 12 my @CL_code_lenghts = (0) x 19;
4376 12 my @CL_order = (16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15);
4377  
4378 12 foreach my $i (0 .. $HCLEN - 1) {
4379 190 $CL_code_lenghts[$CL_order[$i]] = bits2int_lsb($in_fh, 3, $buffer);
4380 }
4381  
4382 12 ▶ $VERBOSE && say STDERR ":: CL code lengths: @CL_code_lenghts";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: CL code lengths: @CL_code_lenghts" 0 12 12
4383  
4384 12 my (undef, $CL_rev_dict) = huffman_from_code_lengths(\@CL_code_lenghts);
4385  
4386 12 my @LL_CL_lengths = _decode_CL_lengths($in_fh, $buffer, $CL_rev_dict, $HLIT);
4387 12 my @dist_CL_lengths = _decode_CL_lengths($in_fh, $buffer, $CL_rev_dict, $HDIST);
4388  
4389 12 my (undef, $LL_rev_dict) = huffman_from_code_lengths(\@LL_CL_lengths);
4390 12 my (undef, $dist_rev_dict) = huffman_from_code_lengths(\@dist_CL_lengths);
4391  
4392 12 _deflate_decode_huffman($in_fh, $buffer, $LL_rev_dict, $dist_rev_dict, $search_window);
4393 }
4394  
4395 102 ▶sub deflate_extract_next_block ($in_fh, $buffer, $search_window) {
Subroutine deflate_extract_next_block: called CC=8 SCAR=21.6
Pod documented
4396  
4397 102 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4); # minimum match length in LZ parsing
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 102 102
4398 102 local $LZ_MAX_LEN = 258; # maximum match length in LZ parsing
4399 102 local $LZ_MAX_DIST = 32768; # maximum allowed back-reference distance in LZ parsing
4400  
4401 102 my $block_type = bits2int_lsb($in_fh, 2, $buffer);
4402  
4403 102 my $chunk = '';
4404  
4405 102 ▶ if ($block_type == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($block_type == 0) { } 3 99 102
elsif ($block_type == 1) { } 88 11 99
elsif ($block_type == 2) { } 11 0 11
4406 3 ▶ $VERBOSE && say STDERR "\n:: Extracting block of type 0";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "\n:: Extracting block of type 0" 0 3 3
4407 3 $chunk = deflate_extract_block_type_0($in_fh, $buffer, $search_window);
4408 }
4409 elsif ($block_type == 1) {
4410 88 ▶ $VERBOSE && say STDERR "\n:: Extracting block of type 1";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "\n:: Extracting block of type 1" 0 88 88
4411 88 $chunk = deflate_extract_block_type_1($in_fh, $buffer, $search_window);
4412 }
4413 elsif ($block_type == 2) {
4414 11 ▶ $VERBOSE && say STDERR "\n:: Extracting block of type 2";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "\n:: Extracting block of type 2" 0 11 11
4415 11 $chunk = deflate_extract_block_type_2($in_fh, $buffer, $search_window);
4416 }
4417 else {
4418 0 confess "[!] Unknown block of type: $block_type";
4419 }
4420  
4421 102 return $chunk;
4422 }
4423  
4424 113 ▶sub gzip_decompress ($in_fh) {
Subroutine gzip_decompress: called CC=34 SCAR=41.5
Pod documented
4425  
4426 113 ▶ if (ref($in_fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in_fh eq "") 55 58 113
4427 55 ▶ open(my $fh2, '<:raw', \$in_fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$in_fh or confess("error: $!") 55 0 55
4428 55 return __SUB__->($fh2);
4429 }
4430  
4431 58 my $decompressed = '';
4432  
4433 58 open my $out_fh, '>:raw', \$decompressed;
4434  
4435 58 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4); # minimum match length in LZ parsing
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 58 58
4436 58 local $LZ_MAX_LEN = 258; # maximum match length in LZ parsing
4437 58 local $LZ_MAX_DIST = (1 << 15) - 1; # maximum allowed back-reference distance in LZ parsing
4438  
4439 58 ▶ my $MAGIC = (getc($in_fh) // confess "error") . (getc($in_fh) // confess "error");
Condition50%2/4
getc $in_fh // confess("error")
l!l
580
getc $in_fh // confess("error")
l!l
580
MC/DC0%0/1
getc $in_fh // confess("error")
getc $in_fh
MC/DC0%0/1
getc $in_fh // confess("error")
getc $in_fh
Truth table50%1/2
A
A= getc $in_fh
Aresult
11
00
Truth table50%1/2
A
A= getc $in_fh
Aresult
11
00
4440  
4441 58 ▶ if ($MAGIC ne pack('C*', 0x1f, 0x8b)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MAGIC ne "\37\213") 0 58 58
4442 0 confess "Not a valid GZIP container!";
4443 }
4444  
4445 58 ▶ my $CM = getc($in_fh) // confess "error"; # 0x08 = DEFLATE
Condition50%1/2
getc $in_fh // confess("error")
l!l
580
MC/DC0%0/1
getc $in_fh // confess("error")
getc $in_fh
Truth table50%1/2
A
A= getc $in_fh
Aresult
11
00
4446 58 ▶ my $FLAGS = ord(getc($in_fh) // confess "error"); # flags
Condition50%1/2
getc $in_fh // confess("error")
l!l
580
MC/DC0%0/1
getc $in_fh // confess("error")
getc $in_fh
Truth table50%1/2
A
A= getc $in_fh
Aresult
11
00
4447 58 ▶ my $MTIME = join('', map { getc($in_fh) // confess "error" } 1 .. 4); # modification time
Condition50%1/2
getc $in_fh // confess("error")
l!l
2320
MC/DC0%0/1
getc $in_fh // confess("error")
getc $in_fh
Truth table50%1/2
A
A= getc $in_fh
Aresult
11
00
4448 58 ▶ my $XFLAGS = getc($in_fh) // confess "error"; # extra flags
Condition50%1/2
getc $in_fh // confess("error")
l!l
580
MC/DC0%0/1
getc $in_fh // confess("error")
getc $in_fh
Truth table50%1/2
A
A= getc $in_fh
Aresult
11
00
4449 58 ▶ my $OS = getc($in_fh) // confess "error"; # 0x03 = Unix
Condition50%1/2
getc $in_fh // confess("error")
l!l
580
MC/DC0%0/1
getc $in_fh // confess("error")
getc $in_fh
Truth table50%1/2
A
A= getc $in_fh
Aresult
11
00
4450  
4451 58 ▶ if ($CM ne chr(0x08)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CM ne "\b") 0 58 58
4452 0 confess "Only DEFLATE compression method is supported (0x08)! Got: 0x", sprintf('%02x', ord($CM));
4453 }
4454  
4455 # Reference:
4456 # https://web.archive.org/web/20240221024029/https://forensics.wiki/gzip/
4457  
4458 58 my $has_filename = 0;
4459 58 my $has_comment = 0;
4460 58 my $has_header_checksum = 0;
4461 58 my $has_extra_fields = 0;
4462  
4463 58 ▶ if ($FLAGS & 0x08) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FLAGS & 8) 3 55 58
4464 3 $has_filename = 1;
4465 }
4466  
4467 58 ▶ if ($FLAGS & 0x10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FLAGS & 16) 2 56 58
4468 2 $has_comment = 1;
4469 }
4470  
4471 58 ▶ if ($FLAGS & 0x02) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FLAGS & 2) 0 58 58
4472 0 $has_header_checksum = 1;
4473 }
4474  
4475 58 ▶ if ($FLAGS & 0x04) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FLAGS & 4) 0 58 58
4476 0 $has_extra_fields = 1;
4477 }
4478  
4479 58 ▶ if ($has_extra_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_extra_fields) 0 58 58
4480 0 my $size = bytes2int_lsb($in_fh, 2);
4481 0 ▶ read($in_fh, (my $extra_field_data), $size) // confess "can't read extra field data: $!";
Condition0%0/2
read($in_fh, my $extra_field_data, $size) // confess("can't read extra field data: $!")
l!l
00
MC/DC0%0/1
read($in_fh, my $extra_field_data, $size) // confess("can't read extra field data: $!")
read($in_fh, my $extra_field_data, $size)
Truth table0%0/2
A
A= read($in_fh, my $extra_field_data, $size)
Aresult
11
00
4482 0 ▶ $VERBOSE && say STDERR ":: Extra field data: $extra_field_data";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Extra field data: $extra_field_data" 0 0 0
4483 }
4484  
4485 58 ▶ if ($has_filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_filename) 3 55 58
4486 3 my $filename = read_null_terminated($in_fh); # filename
4487 3 ▶ $VERBOSE && say STDERR ":: Filename: $filename";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Filename: $filename" 0 3 3
4488 }
4489  
4490 58 ▶ if ($has_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_comment) 2 56 58
4491 2 my $comment = read_null_terminated($in_fh); # comment
4492 2 ▶ $VERBOSE && say STDERR ":: Comment: $comment";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Comment: $comment" 0 2 2
4493 }
4494  
4495 # TODO: verify the header checksum
4496 58 ▶ if ($has_header_checksum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_header_checksum) 0 58 58
4497 0 my $header_checksum = bytes2int_lsb($in_fh, 2);
4498 0 ▶ $VERBOSE && say STDERR ":: Header checksum: $header_checksum";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Header checksum: $header_checksum" 0 0 0
4499 }
4500  
4501 58 my $crc32 = 0;
4502 58 my $actual_length = 0;
4503 58 my $buffer = '';
4504 58 my $search_window = '';
4505  
4506 58 while (1) {
4507  
4508 60 my $is_last = read_bit_lsb($in_fh, \$buffer);
4509 60 my $chunk = deflate_extract_next_block($in_fh, \$buffer, \$search_window);
4510  
4511 60 print $out_fh $chunk;
4512 60 $crc32 = crc32($chunk, $crc32);
4513 60 $actual_length += length($chunk);
4514  
4515 60 ▶ last if $is_last;
Branch: 1 branch
BranchTrueFalseTotal
if $is_last 58 2 60
4516 }
4517  
4518 58 $buffer = ''; # discard any padding bits
4519  
4520 58 my $stored_crc32 = bits2int_lsb($in_fh, 32, \$buffer);
4521 58 my $actual_crc32 = $crc32;
4522  
4523 58 ▶ if ($stored_crc32 != $actual_crc32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stored_crc32 != $actual_crc32) { } 0 58 58
4524 0 confess "[!] The CRC32 does not match: $actual_crc32 (actual) != $stored_crc32 (stored)";
4525 }
4526 else {
4527 58 ▶ $VERBOSE && print STDERR ":: CRC32 value: $actual_crc32\n";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and print STDERR ":: CRC32 value: $actual_crc32\n" 0 58 58
4528 }
4529  
4530 58 my $stored_length = bits2int_lsb($in_fh, 32, \$buffer);
4531  
4532 58 ▶ if ($stored_length != $actual_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stored_length != $actual_length) { } 0 58 58
4533 0 confess "[!] The length does not match: $actual_length (actual) != $stored_length (stored)";
4534 }
4535 else {
4536 58 ▶ $VERBOSE && print STDERR ":: Total length: $actual_length\n";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and print STDERR ":: Total length: $actual_length\n" 0 58 58
4537 }
4538  
4539 58 ▶ if (eof($in_fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eof $in_fh) { } 55 3 58
4540 55 ▶ $VERBOSE && print STDERR "\n:: Reached the end of the file.\n";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and print STDERR "\n:: Reached the end of the file.\n" 0 55 55
4541 }
4542 else {
4543 3 ▶ $VERBOSE && print STDERR "\n:: There is something else in the container! Trying to recurse!\n\n";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and print STDERR "\n:: There is something else in the container! Trying to recurse!\n\n" 0 3 3
4544 3 return ($decompressed . __SUB__->($in_fh));
4545 }
4546  
4547 55 return $decompressed;
4548 }
4549  
4550 ###############################
4551 # ZLIB compressor
4552 ###############################
4553  
4554 72 ▶sub zlib_compress ($in_fh, $lzss_encoding_sub = \&lzss_encode) {
Subroutine zlib_compress: called CC=15 SCAR=28.5
Pod documented
4555  
4556 72 ▶ if (ref($in_fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in_fh eq "") 36 36 72
4557 36 ▶ open(my $fh2, '<:raw', \$in_fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$in_fh or confess("error: $!") 36 0 36
4558 36 return __SUB__->($fh2, $lzss_encoding_sub);
4559 }
4560  
4561 36 my $compressed = '';
4562  
4563 36 open my $out_fh, '>:raw', \$compressed;
4564  
4565 36 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4); # minimum match length in LZ parsing
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 36 36
4566 36 local $LZ_MAX_LEN = 258; # maximum match length in LZ parsing
4567 36 local $LZ_MAX_DIST = (1 << 15) - 1; # maximum allowed back-reference distance in LZ parsing
4568  
4569 36 my $CMF = (7 << 4) | 8;
4570 36 my $FLG = 2 << 6;
4571  
4572 36 ▶ while (($CMF * 256 + $FLG) % 31 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($CMF * 256 + $FLG) % 31 != 0) 1008 36 1044
4573 1008 ++$FLG;
4574 }
4575  
4576 36 my $bitstring = '';
4577 36 my $adler32 = 1;
4578  
4579 36 print $out_fh chr($CMF);
4580 36 print $out_fh chr($FLG);
4581  
4582 36 ▶ if (eof($in_fh)) { # empty file
Branch: 1 branch
BranchTrueFalseTotal
if (eof $in_fh) 2 34 36
4583 2 $bitstring = '1' . '10' . '0000000';
4584 }
4585  
4586 36 state $CHUNK_SIZE = (1 << 15) - 1;
4587  
4588 36 ▶ while (read($in_fh, (my $chunk), $CHUNK_SIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $in_fh, my $chunk, $CHUNK_SIZE) 34 36 70
4589  
4590 34 $adler32 = adler32($chunk, $adler32);
4591 34 ▶ $bitstring .= eof($in_fh) ? '1' : '0';
Branch: 1 branch
BranchTrueFalseTotal
eof $in_fh ? : 34 0 34
4592  
4593 34 my ($literals, $distances, $lengths) = $lzss_encoding_sub->($chunk);
4594  
4595 34 my $bt1_bitstring = deflate_create_block_type_1($literals, $distances, $lengths);
4596  
4597 # When block type 1 is larger than the input, then we have random uncompressible data: use block type 0
4598 34 ▶ if ((length($bt1_bitstring) >> 3) > length($chunk) + 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($bt1_bitstring) >> 3 > length($chunk) + 5) 0 34 34
4599  
4600 0 ▶ $VERBOSE && say STDERR ":: Using block type: 0";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Using block type: 0" 0 0 0
4601  
4602 0 $bitstring .= '00';
4603  
4604 0 print $out_fh pack('b*', $bitstring); # pads to a byte
4605 0 print $out_fh pack('b*', deflate_create_block_type_0_header($chunk));
4606 0 print $out_fh $chunk;
4607  
4608 0 $bitstring = '';
4609 0 next;
4610 }
4611  
4612 34 my $bt2_bitstring = deflate_create_block_type_2($literals, $distances, $lengths);
4613  
4614 # When block type 2 is larger than block type 1, then we may have very small data
4615 34 ▶ if (length($bt2_bitstring) > length($bt1_bitstring)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bt2_bitstring > length $bt1_bitstring) { } 33 1 34
4616 33 ▶ $VERBOSE && say STDERR ":: Using block type: 1";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Using block type: 1" 0 33 33
4617 33 $bitstring .= $bt1_bitstring;
4618 }
4619 else {
4620 1 ▶ $VERBOSE && say STDERR ":: Using block type: 2";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR ":: Using block type: 2" 0 1 1
4621 1 $bitstring .= $bt2_bitstring;
4622 }
4623  
4624 34 print $out_fh pack('b*', substr($bitstring, 0, length($bitstring) - (length($bitstring) % 8), ''));
4625 }
4626  
4627 36 ▶ if ($bitstring ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitstring ne "") 34 2 36
4628 34 print $out_fh pack('b*', $bitstring);
4629 }
4630  
4631 36 print $out_fh int2bytes($adler32, 4);
4632  
4633 36 return $compressed;
4634 }
4635  
4636 ###############################
4637 # ZLIB decompressor
4638 ###############################
4639  
4640 77 ▶sub zlib_decompress($in_fh) {
Subroutine zlib_decompress: called CC=12 SCAR=26.2
Pod documented
4641  
4642 77 ▶ if (ref($in_fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in_fh eq "") 38 39 77
4643 38 ▶ open(my $fh2, '<:raw', \$in_fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$in_fh or confess("error: $!") 38 0 38
4644 38 return __SUB__->($fh2);
4645 }
4646  
4647 39 my $decompressed = '';
4648  
4649 39 open my $out_fh, '>:raw', \$decompressed;
4650  
4651 39 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4); # minimum match length in LZ parsing
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 39 39
4652 39 local $LZ_MAX_LEN = 258; # maximum match length in LZ parsing
4653 39 local $LZ_MAX_DIST = (1 << 15) - 1; # maximum allowed back-reference distance in LZ parsin
4654  
4655 39 my $adler32 = 1;
4656  
4657 39 my $CMF = ord(getc($in_fh));
4658 39 my $FLG = ord(getc($in_fh));
4659  
4660 39 ▶ if (($CMF * 256 + $FLG) % 31 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($CMF * 256 + $FLG) % 31 != 0) 0 39 39
4661 0 confess "Invalid header checksum!\n";
4662 }
4663  
4664 39 my $CINFO = $CMF >> 4;
4665  
4666 39 ▶ if ($CINFO > 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CINFO > 7) 0 39 39
4667 0 confess "Values of CINFO above 7 are not supported!\n";
4668 }
4669  
4670 39 my $method = $CMF & 0b1111;
4671  
4672 39 ▶ if ($method != 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method != 8) 0 39 39
4673 0 confess "Only method 8 (DEFLATE) is supported!\n";
4674 }
4675  
4676 39 my $buffer = '';
4677 39 my $search_window = '';
4678  
4679 39 while (1) {
4680  
4681 39 my $is_last = read_bit_lsb($in_fh, \$buffer);
4682 39 my $chunk = deflate_extract_next_block($in_fh, \$buffer, \$search_window);
4683  
4684 39 print $out_fh $chunk;
4685 39 $adler32 = adler32($chunk, $adler32);
4686  
4687 39 ▶ last if $is_last;
Branch: 1 branch
BranchTrueFalseTotal
if $is_last 39 0 39
4688 }
4689  
4690 39 my $stored_adler32 = bytes2int($in_fh, 4);
4691  
4692 39 ▶ if ($adler32 != $stored_adler32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adler32 != $stored_adler32) 0 39 39
4693 0 confess "Adler32 checksum does not match: $adler32 (actual) != $stored_adler32 (stored)\n";
4694 }
4695  
4696 39 ▶ if (eof($in_fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eof $in_fh) { } 38 1 39
4697 38 ▶ $VERBOSE && print STDERR "\n:: Reached the end of the file.\n";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and print STDERR "\n:: Reached the end of the file.\n" 0 38 38
4698 }
4699 else {
4700 1 ▶ $VERBOSE && print STDERR "\n:: There is something else in the container! Trying to recurse!\n\n";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and print STDERR "\n:: There is something else in the container! Trying to recurse!\n\n" 0 1 1
4701 1 return ($decompressed . __SUB__->($in_fh));
4702 }
4703  
4704 38 return $decompressed;
4705 }
4706  
4707 ###############################
4708 # LZ4 compressor
4709 ###############################
4710  
4711 76 ▶sub lz4_compress($fh, $lzss_encoding_sub = \&lzss_encode) {
Subroutine lz4_compress: called CC=18 SCAR=28.9
Pod documented
4712  
4713 76 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 38 38 76
4714 38 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 38 0 38
4715 38 return __SUB__->($fh2, $lzss_encoding_sub);
4716 }
4717  
4718 38 my $compressed = '';
4719  
4720 38 $compressed .= int2bytes_lsb(0x184D2204, 4); # LZ4 magic number
4721  
4722 38 my $fd = ''; # frame description
4723 38 $fd .= chr(0b01_10_00_00); # flags (FLG)
4724 38 $fd .= chr(0b0_111_0000); # block description (BD)
4725  
4726 38 $compressed .= $fd;
4727 38 $compressed .= chr(115); # header checksum
4728  
4729 38 state $CHUNK_SIZE = 1 << 17;
4730  
4731 38 ▶ while (read($fh, (my $chunk), $CHUNK_SIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $chunk, $CHUNK_SIZE) 36 38 74
4732  
4733 36 my ($literals, $distances, $lengths) = do {
4734 36 ▶ local $LZ_MIN_LEN = 4 if ($LZ_MIN_LEN < 4);
Branch: 1 branch
BranchTrueFalseTotal
if $Compression::Util::LZ_MIN_LEN < 4 0 36 36
4735 36 local $LZ_MAX_LEN = ~0;
4736 36 local $LZ_MAX_DIST = (1 << 16) - 1;
4737 36 $lzss_encoding_sub->(substr($chunk, 0, -5));
4738 };
4739  
4740 # The last 5 bytes of each block must be literals
4741 # https://github.com/lz4/lz4/issues/1495
4742 36 push @$literals, unpack('C*', substr($chunk, -5));
4743  
4744 36 my $literals_end = $#{$literals};
4745  
4746 36 my $block = '';
4747  
4748 36 ▶ for (my $i = 0 ; $i <= $literals_end ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $literals_end) 3746 0 3746
4749  
4750 3746 my @uncompressed;
4751 3746 ▶ while ($i <= $literals_end and defined($literals->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $literals_end and defined $literals->[$i]) 5804 3746 9550
Condition100%3/3
$i <= $literals_end and defined $literals->[$i]
!ll&&!rl&&r
3637105804
MC/DC100%2/2
$i <= $literals_end and defined $literals->[$i]
$i <= $literals_end defined $literals->[$i]
Truth table100%3/3
A and B
A= $i <= $literals_endB= defined $literals->[$i]
ABresult
0X0
100
111
4752 5804 push @uncompressed, $literals->[$i];
4753 5804 ++$i;
4754 }
4755  
4756 3746 my $literals_string = pack('C*', @uncompressed);
4757 3746 my $literals_length = scalar(@uncompressed);
4758  
4759 3746 ▶ my $match_len = $lengths->[$i] ? ($lengths->[$i] - 4) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$lengths->[$i] ? : 3710 36 3746
4760  
4761 3746 ▶ $block .= chr((($literals_length >= 15 ? 15 : $literals_length) << 4) | ($match_len >= 15 ? 15 : $match_len));
Branch: 2 branches
BranchTrueFalseTotal
$literals_length >= 15 ? : 69 3677 3746
$match_len >= 15 ? : 1016 2730 3746
4762  
4763 3746 $literals_length -= 15;
4764 3746 $match_len -= 15;
4765  
4766 3746 ▶ while ($literals_length >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length >= 0) 69 3746 3815
4767 69 ▶ $block .= ($literals_length >= 255 ? "\xff" : chr($literals_length));
Branch: 1 branch
BranchTrueFalseTotal
$literals_length >= 255 ? : 0 69 69
4768 69 $literals_length -= 255;
4769 }
4770  
4771 3746 $block .= $literals_string;
4772  
4773 3746 ▶ my $dist = $distances->[$i] // last;
Condition100%2/2
$distances->[$i] // (last)
l!l
371036
MC/DC100%1/1
$distances->[$i] // (last)
$distances->[$i]
Truth table100%2/2
A
A= $distances->[$i]
Aresult
11
00
4774 3710 $block .= pack('b*', scalar reverse sprintf('%016b', $dist));
4775  
4776 3710 ▶ while ($match_len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_len >= 0) 1032 3710 4742
4777 1032 ▶ $block .= ($match_len >= 255 ? "\xff" : chr($match_len));
Branch: 1 branch
BranchTrueFalseTotal
$match_len >= 255 ? : 16 1016 1032
4778 1032 $match_len -= 255;
4779 }
4780 }
4781  
4782 36 ▶ if ($block ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($block ne "") 36 0 36
4783 36 $compressed .= int2bytes_lsb(length($block), 4);
4784 36 $compressed .= $block;
4785 }
4786 }
4787  
4788 38 $compressed .= int2bytes_lsb(0x00000000, 4); # EndMark
4789 38 return $compressed;
4790 }
4791  
4792 ###############################
4793 # LZ4 decompressor
4794 ###############################
4795  
4796 112 ▶sub lz4_decompress($fh) {
Subroutine lz4_decompress: called CC=38 SCAR=39.5
Pod documented
4797  
4798 112 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 56 56 112
4799 56 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 56 0 56
4800 56 return __SUB__->($fh2);
4801 }
4802  
4803 56 my $decompressed = '';
4804  
4805 56 ▶ while (!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 60 56 116
4806  
4807 60 ▶ bytes2int_lsb($fh, 4) == 0x184D2204 or confess "Incorrect LZ4 Frame magic number";
Branch: 1 branch
BranchTrueFalseTotal
bytes2int_lsb($fh, 4) == 407708164 or confess("Incorrect LZ4 Frame magic number") 60 0 60
4808  
4809 60 my $FLG = ord(getc($fh));
4810 60 my $BD = ord(getc($fh));
4811  
4812 60 my $version = $FLG & 0b11_00_00_00;
4813 60 my $B_indep = $FLG & 0b00_10_00_00;
4814 60 my $B_checksum = $FLG & 0b00_01_00_00;
4815 60 my $C_size = $FLG & 0b00_00_10_00;
4816 60 my $C_checksum = $FLG & 0b00_00_01_00;
4817 60 my $DictID = $FLG & 0b00_00_00_01;
4818  
4819 60 my $Block_MaxSize = $BD & 0b0_111_0000;
4820  
4821 60 ▶ $VERBOSE && say STDERR "Maximum block size: $Block_MaxSize";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Maximum block size: $Block_MaxSize" 0 60 60
4822  
4823 60 ▶ if ($version != 0b01_00_00_00) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version != 64) 0 60 60
4824 0 confess "Error: Invalid version number";
4825 }
4826  
4827 60 ▶ if ($C_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($C_size) 0 60 60
4828 0 my $content_size = bytes2int_lsb($fh, 8);
4829 0 ▶ $VERBOSE && say STDERR "Content size: ", $content_size;
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Content size: ", $content_size 0 0 0
4830 }
4831  
4832 60 ▶ if ($DictID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DictID) 0 60 60
4833 0 my $dict_id = bytes2int_lsb($fh, 4);
4834 0 ▶ $VERBOSE && say STDERR "Dictionary ID: ", $dict_id;
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Dictionary ID: ", $dict_id 0 0 0
4835 }
4836  
4837 60 my $header_checksum = ord(getc($fh));
4838  
4839 # TODO: compute and verify the header checksum
4840 60 ▶ $VERBOSE && say STDERR "Header checksum: ", $header_checksum;
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Header checksum: ", $header_checksum 0 60 60
4841  
4842 60 my $decoded = '';
4843  
4844 60 ▶ while (!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 116 0 116
4845  
4846 116 my $block_size = bytes2int_lsb($fh, 4);
4847  
4848 116 ▶ if ($block_size == 0x00000000) { # signifies an EndMark
Branch: 1 branch
BranchTrueFalseTotal
if ($block_size == 0) 60 56 116
4849 60 ▶ $VERBOSE && say STDERR "Block size == 0";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Block size == 0" 0 60 60
4850 60 last;
4851 }
4852  
4853 56 ▶ $VERBOSE && say STDERR "Block size: $block_size";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Block size: $block_size" 0 56 56
4854  
4855 56 ▶ if ($block_size >> 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_size >> 31) { } 3 53 56
4856 3 ▶ $VERBOSE && say STDERR "Highest bit set: ", $block_size;
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Highest bit set: ", $block_size 0 3 3
4857 3 $block_size &= ((1 << 31) - 1);
4858 3 ▶ $VERBOSE && say STDERR "Block size: ", $block_size;
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Block size: ", $block_size 0 3 3
4859 3 my $uncompressed = '';
4860 3 read($fh, $uncompressed, $block_size);
4861 3 $decoded .= $uncompressed;
4862 }
4863 else {
4864  
4865 53 my $compressed = '';
4866 53 read($fh, $compressed, $block_size);
4867  
4868 53 ▶ while ($compressed ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($compressed ne "") 3787 0 3787
4869 3787 my $len_byte = ord(substr($compressed, 0, 1, ''));
4870  
4871 3787 my $literals_length = $len_byte >> 4;
4872 3787 my $match_len = $len_byte & 0b1111;
4873  
4874 ## say STDERR "Literal: ", $literals_length;
4875 ## say STDERR "Match len: ", $match_len;
4876  
4877 3787 ▶ if ($literals_length == 15) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length == 15) 69 3718 3787
4878 69 while (1) {
4879 69 my $byte_len = ord(substr($compressed, 0, 1, ''));
4880 69 $literals_length += $byte_len;
4881 69 ▶ last if $byte_len != 255;
Branch: 1 branch
BranchTrueFalseTotal
if $byte_len != 255 69 0 69
4882 }
4883 }
4884  
4885 ## say STDERR "Total literals length: ", $literals_length;
4886  
4887 3787 my $literals = '';
4888  
4889 3787 ▶ if ($literals_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literals_length > 0) 1192 2595 3787
4890 1192 $literals = substr($compressed, 0, $literals_length, '');
4891 }
4892  
4893 3787 ▶ if ($compressed eq '') { # end of block
Branch: 1 branch
BranchTrueFalseTotal
if ($compressed eq "") 53 3734 3787
4894 53 $decoded .= $literals;
4895 53 last;
4896 }
4897  
4898 3734 my $offset = oct('0b' . reverse unpack('b16', substr($compressed, 0, 2, '')));
4899  
4900 3734 ▶ if ($offset == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset == 0) 0 3734 3734
4901 0 confess "Corrupted block";
4902 }
4903  
4904 ## say STDERR "Offset: $offset";
4905  
4906 3734 ▶ if ($match_len == 15) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_len == 15) 1016 2718 3734
4907 1016 while (1) {
4908 1032 my $byte_len = ord(substr($compressed, 0, 1, ''));
4909 1032 $match_len += $byte_len;
4910 1032 ▶ last if $byte_len != 255;
Branch: 1 branch
BranchTrueFalseTotal
if $byte_len != 255 1016 16 1032
4911 }
4912 }
4913  
4914 3734 $decoded .= $literals;
4915 3734 $match_len += 4;
4916  
4917 ## say STDERR "Total match len: $match_len\n";
4918  
4919 3734 ▶ if ($offset >= $match_len) { # non-overlapping matches
Branch: 2 branches
BranchTrueFalseTotal
if ($offset >= $match_len) { } 3573 161 3734
elsif ($offset == 1) { } 28 133 161
4920 3573 ▶ $decoded .= substr($decoded, length($decoded) - $offset, $match_len) // confess "bad input";
Condition50%1/2
substr($decoded, length($decoded) - $offset, $match_len) // confess("bad input")
l!l
35730
MC/DC0%0/1
substr($decoded, length($decoded) - $offset, $match_len) // confess("bad input")
substr($decoded, length($decoded) - $offset, $match_len)
Truth table50%1/2
A
A= substr($decoded, length($decoded) - $offset, $match_len)
Aresult
11
00
4921 }
4922 elsif ($offset == 1) {
4923 28 $decoded .= substr($decoded, -1) x $match_len;
4924 }
4925 else { # overlapping matches
4926 133 ▶ my $pattern = substr($decoded, length($decoded) - $offset, $offset) // confess "bad input";
Condition50%1/2
substr($decoded, length($decoded) - $offset, $offset) // confess("bad input")
l!l
1330
MC/DC0%0/1
substr($decoded, length($decoded) - $offset, $offset) // confess("bad input")
substr($decoded, length($decoded) - $offset, $offset)
Truth table50%1/2
A
A= substr($decoded, length($decoded) - $offset, $offset)
Aresult
11
00
4927 133 my $full_reps = int(($match_len + $offset - 1) / $offset);
4928 133 ▶ $decoded .= substr($pattern x $full_reps, 0, $match_len) // confess "bad input";
Condition50%1/2
substr($pattern x $full_reps, 0, $match_len) // confess("bad input")
l!l
1330
MC/DC0%0/1
substr($pattern x $full_reps, 0, $match_len) // confess("bad input")
substr($pattern x $full_reps, 0, $match_len)
Truth table50%1/2
A
A= substr($pattern x $full_reps, 0, $match_len)
Aresult
11
00
4929 }
4930 }
4931 }
4932  
4933 56 ▶ if ($B_checksum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($B_checksum) 5 51 56
4934 5 my $content_checksum = bytes2int_lsb($fh, 4);
4935 5 ▶ $VERBOSE && say STDERR "Block checksum: $content_checksum";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Block checksum: $content_checksum" 0 5 5
4936 }
4937  
4938 56 ▶ if ($B_indep) { # blocks are independent of each other
Branch: 2 branches
BranchTrueFalseTotal
if ($B_indep) { } 56 0 56
elsif (length $decoded > 65536.0) { } 0 0 0
4939 56 $decompressed .= $decoded;
4940 56 $decoded = '';
4941 }
4942 elsif (length($decoded) > 2**16) { # blocks are dependent
4943 0 $decompressed .= substr($decoded, 0, -(2**16), '');
4944 }
4945 }
4946  
4947 # TODO: compute and verify checksum
4948 60 ▶ if ($C_checksum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($C_checksum) 12 48 60
4949 12 my $content_checksum = bytes2int_lsb($fh, 4);
4950 12 ▶ $VERBOSE && say STDERR "Content checksum: $content_checksum";
Branch: 1 branch
BranchTrueFalseTotal
$Compression::Util::VERBOSE and say STDERR "Content checksum: $content_checksum" 0 12 12
4951 }
4952  
4953 60 $decompressed .= $decoded;
4954 }
4955  
4956 56 return $decompressed;
4957 }
4958  
4959 ################################################
4960 # Format detection + auto-dispatch decompression
4961 ################################################
4962  
4963 40 ▶sub detect_format ($data) {
Subroutine detect_format: called CC=11 SCAR=31.2
Pod documented
4964  
4965 40 my $peek;
4966  
4967 40 ▶ if (ref($data) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq '') { } 40 0 40
4968 40 $peek = substr($data, 0, 4);
4969 }
4970 else {
4971 0 ▶ my $n = read($data, $peek, 4) // confess "error: $!";
Condition0%0/2
read($data, $peek, 4) // confess("error: $!")
l!l
00
MC/DC0%0/1
read($data, $peek, 4) // confess("error: $!")
read($data, $peek, 4)
Truth table0%0/2
A
A= read($data, $peek, 4)
Aresult
11
00
4972 0 ▶ seek($data, -$n, 1) or confess "error: $!" if $n > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n > 0 0 0 0
Condition0%0/5
$n > 0 && (seek($data, -$n, 1) || confess("error: $!"))
!ll&&!rl&&r
000
seek $data, -$n, 1 or confess("error: $!")
l!l
00
MC/DC0%0/2
$n > 0 && (seek($data, -$n, 1) || confess("error: $!"))
$n > 0 seek $data, -$n, 1
Truth table0%0/3
A && B
A= $n > 0B= seek $data, -$n, 1
ABresult
0X0
100
111
4973 }
4974  
4975 40 ▶ return 'gzip' if substr($peek, 0, 2) eq "\x1f\x8b";
Branch: 1 branch
BranchTrueFalseTotal
if substr($peek, 0, 2) eq "\37\213" 10 30 40
4976 30 ▶ return 'bzip2' if substr($peek, 0, 3) eq 'BZh';
Branch: 1 branch
BranchTrueFalseTotal
if substr($peek, 0, 3) eq "BZh" 10 20 30
4977 20 ▶ return 'lz4' if substr($peek, 0, 4) eq "\x04\x22\x4d\x18";
Branch: 1 branch
BranchTrueFalseTotal
if substr($peek, 0, 4) eq "\4\"M\30" 10 10 20
4978  
4979 10 ▶ if (length($peek) >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $peek >= 2) 10 0 10
4980 10 my ($cmf, $flg) = unpack('CC', $peek);
4981 10 ▶ if (($cmf & 0x0f) == 8 and (($cmf << 8) + $flg) % 31 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($cmf & 15) == 8 and (($cmf << 8) + $flg) % 31 == 0) 10 0 10
Condition33%1/3
($cmf & 15) == 8 and (($cmf << 8) + $flg) % 31 == 0
!ll&&!rl&&r
0010
MC/DC0%0/2
($cmf & 15) == 8 and (($cmf << 8) + $flg) % 31 == 0
($cmf & 15) == 8 (($cmf << 8) + $flg) % 31 == 0
Truth table33%1/3
A and B
A= ($cmf & 15) == 8B= (($cmf << 8) + $flg) % 31 == 0
ABresult
0X0
100
111
4982 10 return 'zlib';
4983 }
4984 }
4985  
4986 0 return undef;
4987 }
4988  
4989 36 ▶sub decompress_auto ($data) {
Subroutine decompress_auto: called CC=6 SCAR=18.3
Pod documented
4990  
4991 36 ▶ my $format = detect_format($data) // confess "error: unrecognized compressed format";
Condition50%1/2
detect_format($data) // confess("error: unrecognized compressed format")
l!l
360
MC/DC0%0/1
detect_format($data) // confess("error: unrecognized compressed format")
detect_format($data)
Truth table50%1/2
A
A= detect_format($data)
Aresult
11
00
4992  
4993 36 ▶ return gzip_decompress($data) if $format eq 'gzip';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "gzip" 9 27 36
4994 27 ▶ return zlib_decompress($data) if $format eq 'zlib';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "zlib" 9 18 27
4995 18 ▶ return bzip2_decompress($data) if $format eq 'bzip2';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "bzip2" 9 9 18
4996 9 ▶ return lz4_decompress($data) if $format eq 'lz4';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "lz4" 9 0 9
4997  
4998 0 confess "error: unhandled format: $format"; # unreachable, kept for safety
4999 }
5000  
5001 ########################################
5002 # Best-of-N meta-compressor
5003 ########################################
5004  
5005 9 ▶sub best_compress ($data, $methods = undef) {
Subroutine best_compress: called CC=7 SCAR=20.1
Pod documented
5006  
5007 9 ▶ $methods //= [['G', \&gzip_compress], ['B', \&bwt_compress], ['M', \&mrl_compress],];
Condition50%1/2
$methods //= [["G", \&Compression::Util::gzip_compress], ["B", \&Compression::Util::bwt_compress], ["M", \&Compression::Util::mrl_compress]]
l!l
09
MC/DC0%0/1
$methods //= [["G", \&Compression::Util::gzip_compress], ["B", \&Compression::Util::bwt_compress], ["M", \&Compression::Util::mrl_compress]]
$methods
Truth table50%1/2
A
A= $methods
Aresult
11
00
5008  
5009 9 my ($best_tag, $best_out);
5010  
5011 9 foreach my $pair (@$methods) {
5012 27 my ($tag, $sub) = @$pair;
5013 27 ▶ length($tag) == 1 or confess "error: tag must be a single character";
Branch: 1 branch
BranchTrueFalseTotal
length $tag == 1 or confess("error: tag must be a single character") 27 0 27
5014  
5015 27 my $out = $sub->($data);
5016  
5017 27 ▶ if (!defined($best_out) or length($out) < length($best_out)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $best_out or length $out < length $best_out) 27 0 27
Condition66%2/3
not defined $best_out or length $out < length $best_out
l!l&&r!l&&!r
9180
MC/DC0%0/2
not defined $best_out or length $out < length $best_out
not defined $best_out length $out < length $best_out
Truth table66%2/3
not(A) or B
A= not defined $best_outB= length $out < length $best_out
ABresult
1X1
011
000
5018 27 ($best_tag, $best_out) = ($tag, $out);
5019 }
5020 }
5021  
5022 9 return $best_tag . $best_out;
5023 }
5024  
5025 18 ▶sub best_decompress ($fh, $methods = undef) {
Subroutine best_decompress: called CC=7 SCAR=20.2
Pod documented
5026  
5027 18 ▶ if (ref($fh) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq "") 9 9 18
5028 9 ▶ open(my $fh2, '<:raw', \$fh) or confess "error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<:raw", \$fh or confess("error: $!") 9 0 9
5029 9 return __SUB__->($fh2, $methods);
5030 }
5031  
5032 $methods //= {
5033 9 ▶ 'G' => \&gzip_decompress,
Condition50%1/2
$methods //= {"G", \&Compression::Util::gzip_decompress, "B", \&Compression::Util::bwt_decompress, "M", \&Compression::Util::mrl_decompress}
l!l
09
MC/DC0%0/1
$methods //= {"G", \&Compression::Util::gzip_decompress, "B", \&Compression::Util::bwt_decompress, "M", \&Compression::Util::mrl_decompress}
$methods
Truth table50%1/2
A
A= $methods
Aresult
11
00
5034 'B' => \&bwt_decompress,
5035 'M' => \&mrl_decompress,
5036 };
5037  
5038 9 ▶ my $tag = getc($fh) // confess "error: can't read compression tag";
Condition50%1/2
getc $fh // confess("error: can't read compression tag")
l!l
90
MC/DC0%0/1
getc $fh // confess("error: can't read compression tag")
getc $fh
Truth table50%1/2
A
A= getc $fh
Aresult
11
00
5039 9 ▶ my $sub = $methods->{$tag} // confess "error: unknown compression tag: '$tag'";
Condition50%1/2
$methods->{$tag} // confess("error: unknown compression tag: '${tag}'")
l!l
90
MC/DC0%0/1
$methods->{$tag} // confess("error: unknown compression tag: '${tag}'")
$methods->{$tag}
Truth table50%1/2
A
A= $methods->{$tag}
Aresult
11
00
5040  
5041 9 return $sub->($fh);
5042 }
5043  
5044 48 1;
↑ Summary