blib/lib/IO/Compress/Zlib/Extra.pm

filter: Statementstmt 83.8% 78 / 93 Branchbran 83.3% 50 / 60 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 6 totaltotal 79.2% 145 / 183 CC 35 SCAR 25.3
CC
35
Cov
83%
findID
30.0
parseExtraField
15.0
validateExtraFieldPair
11.2
CRAP 12.6
« blib/lib/IO/Compress/Gzip.pm ↑ Summary blib/lib/IO/Compress/Base.pm »
1 package IO::Compress::Zlib::Extra;
2  
3 125 require 5.006 ;
4  
5 125 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 125 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 125 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 125 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS);
10  
11 125 $VERSION = '2.223';
12  
13 125 ▶use IO::Compress::Gzip::Constants 2.223 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub ExtraFieldError
16 {
17 24 ▶ return $_[0];
Subroutine ExtraFieldError: called CC=1 SCAR=1.2
Pod undocumented
18 0 return "Error with ExtraField Parameter: $_[0]" ;
19 }
20  
21 sub validateExtraFieldPair
22 {
23 910 ▶ my $pair = shift ;
Subroutine validateExtraFieldPair: called CC=11 SCAR=24.2
Pod undocumented
24 910 my $strict = shift;
25 910 my $gzipMode = shift ;
26  
27 910 ▶ return ExtraFieldError("Not an array ref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pair and ref $pair eq "ARRAY" 0 910 910
Condition33%1/3
ref $pair and ref $pair eq "ARRAY"
!ll&&!rl&&r
00910
MC/DC0%0/2
ref $pair and ref $pair eq "ARRAY"
ref $pair ref $pair eq "ARRAY"
Truth table33%1/3
A and B
A= ref $pairB= ref $pair eq "ARRAY"
ABresult
0X0
100
111
28 unless ref $pair && ref $pair eq 'ARRAY';
29  
30 910 ▶ return ExtraFieldError("SubField must have two parts")
Branch: 1 branch
BranchTrueFalseTotal
unless @$pair == 2 2 908 910
31 unless @$pair == 2 ;
32  
33 908 ▶ return ExtraFieldError("SubField ID is a reference")
Branch: 1 branch
BranchTrueFalseTotal
if ref $pair->[0] 1 907 908
34 if ref $pair->[0] ;
35  
36 907 ▶ return ExtraFieldError("SubField Data is a reference")
Branch: 1 branch
BranchTrueFalseTotal
if ref $pair->[1] 2 905 907
37 if ref $pair->[1] ;
38  
39 # ID is exactly two chars
40 905 ▶ return ExtraFieldError("SubField ID not two chars long")
Branch: 1 branch
BranchTrueFalseTotal
unless length $pair->[0] == 2 4 901 905
41 unless length $pair->[0] == GZIP_FEXTRA_SUBFIELD_ID_SIZE ;
42  
43 # Check that the 2nd byte of the ID isn't 0
44 901 ▶ return ExtraFieldError("SubField ID 2nd byte is 0x00")
Branch: 1 branch
BranchTrueFalseTotal
if $strict and $gzipMode and substr($pair->[0], 1, 1) eq "\0" 3 898 901
Condition83%5/6
$strict and $gzipMode and substr($pair->[0], 1, 1) eq "\0"
!ll&&!rl&&r
4004983
$strict and $gzipMode
!ll&&!rl&&r
0400501
MC/DC66%2/3
$strict and $gzipMode and substr($pair->[0], 1, 1) eq "\0"
$strict $gzipMode substr($pair->[0], 1, 1) eq "\0"
Truth table75%3/4
A and B and C
A= $strictB= $gzipModeC= substr($pair->[0], 1, 1) eq "\0"
ABCresult
0XX0
10X0
1100
1111
45 if $strict && $gzipMode && substr($pair->[0], 1, 1) eq "\x00" ;
46  
47 898 ▶ return ExtraFieldError("SubField Data too long")
Branch: 1 branch
BranchTrueFalseTotal
if length $pair->[1] > 65531 1 897 898
48 if length $pair->[1] > GZIP_FEXTRA_SUBFIELD_MAX_SIZE ;
49  
50  
51 897 return undef ;
52 }
53  
54 sub parseRawExtra
55 {
56 673 ▶ my $data = shift ;
Subroutine parseRawExtra: called CC=7 SCAR=19.5
Pod undocumented
57 673 my $extraRef = shift;
58 673 my $strict = shift;
59 673 my $gzipMode = shift ;
60  
61 #my $lax = shift ;
62  
63 #return undef
64 # if $lax ;
65  
66 673 my $XLEN = length $data ;
67  
68 673 ▶ return ExtraFieldError("Too Large")
Branch: 1 branch
BranchTrueFalseTotal
if $XLEN > 65535 1 672 673
69 if $XLEN > GZIP_FEXTRA_MAX_SIZE;
70  
71 672 my $offset = 0 ;
72 672 ▶ while ($offset < $XLEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $XLEN) 877 664 1541
73  
74 877 ▶ return ExtraFieldError("Truncated in FEXTRA Body Section")
Branch: 1 branch
BranchTrueFalseTotal
if $offset + 4 > $XLEN 2 875 877
75 if $offset + GZIP_FEXTRA_SUBFIELD_HEADER_SIZE > $XLEN ;
76  
77 875 my $id = substr($data, $offset, GZIP_FEXTRA_SUBFIELD_ID_SIZE);
78 875 $offset += GZIP_FEXTRA_SUBFIELD_ID_SIZE;
79  
80 875 my $subLen = unpack("v", substr($data, $offset,
81 GZIP_FEXTRA_SUBFIELD_LEN_SIZE));
82 875 $offset += GZIP_FEXTRA_SUBFIELD_LEN_SIZE ;
83  
84 875 ▶ return ExtraFieldError("Truncated in FEXTRA Body Section")
Branch: 1 branch
BranchTrueFalseTotal
if $offset + $subLen > $XLEN 4 871 875
85 if $offset + $subLen > $XLEN ;
86  
87 871 my $bad = validateExtraFieldPair( [$id,
88 substr($data, $offset, $subLen)],
89 $strict, $gzipMode );
90 871 ▶ return $bad if $bad ;
Branch: 1 branch
BranchTrueFalseTotal
if $bad 2 869 871
91 869 ▶ push @$extraRef, [$id => substr($data, $offset, $subLen)]
Branch: 1 branch
BranchTrueFalseTotal
if defined $extraRef 850 19 869
92 if defined $extraRef;
93  
94 869 $offset += $subLen ;
95 }
96  
97  
98 664 return undef ;
99 }
100  
101 sub findID
102 {
103 0 ▶ my $id_want = shift ;
Subroutine findID: not called CC=5 SCAR=34.0
Pod undocumented
104 0 my $data = shift;
105  
106 0 my $XLEN = length $data ;
107  
108 0 my $offset = 0 ;
109 0 ▶ while ($offset < $XLEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $XLEN) 0 0 0
110  
111 return undef
112 0 ▶ if $offset + GZIP_FEXTRA_SUBFIELD_HEADER_SIZE > $XLEN ;
Branch: 1 branch
BranchTrueFalseTotal
if $offset + 4 > $XLEN 0 0 0
113  
114 0 my $id = substr($data, $offset, GZIP_FEXTRA_SUBFIELD_ID_SIZE);
115 0 $offset += GZIP_FEXTRA_SUBFIELD_ID_SIZE;
116  
117 0 my $subLen = unpack("v", substr($data, $offset,
118 GZIP_FEXTRA_SUBFIELD_LEN_SIZE));
119 0 $offset += GZIP_FEXTRA_SUBFIELD_LEN_SIZE ;
120  
121 return undef
122 0 ▶ if $offset + $subLen > $XLEN ;
Branch: 1 branch
BranchTrueFalseTotal
if $offset + $subLen > $XLEN 0 0 0
123  
124 0 ▶ return substr($data, $offset, $subLen)
Branch: 1 branch
BranchTrueFalseTotal
if $id eq $id_want 0 0 0
125 if $id eq $id_want ;
126  
127 0 $offset += $subLen ;
128 }
129  
130 0 return undef ;
131 }
132  
133  
134 sub mkSubField
135 {
136 393 ▶ my $id = shift ;
Subroutine mkSubField: called CC=1 SCAR=0.0
Pod undocumented
137 393 my $data = shift ;
138  
139 393 return $id . pack("v", length $data) . $data ;
140 }
141  
142 sub parseExtraField
143 {
144 82 ▶ my $dataRef = $_[0];
Subroutine parseExtraField: called CC=15 SCAR=27.1
Pod undocumented
145 82 my $strict = $_[1];
146 82 my $gzipMode = $_[2];
147 #my $lax = @_ == 2 ? $_[1] : 1;
148  
149  
150 # ExtraField can be any of
151 #
152 # -ExtraField => $data
153 #
154 # -ExtraField => [$id1, $data1,
155 # $id2, $data2]
156 # ...
157 # ]
158 #
159 # -ExtraField => [ [$id1 => $data1],
160 # [$id2 => $data2],
161 # ...
162 # ]
163 #
164 # -ExtraField => { $id1 => $data1,
165 # $id2 => $data2,
166 # ...
167 # }
168  
169 82 ▶ if ( ! ref $dataRef ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $dataRef) 48 34 82
170  
171 return undef
172 48 ▶ if ! $strict;
Branch: 1 branch
BranchTrueFalseTotal
unless $strict 18 30 48
173  
174 30 return parseRawExtra($dataRef, undef, 1, $gzipMode);
175 }
176  
177 34 my $data = $dataRef;
178 34 my $out = '' ;
179  
180 34 ▶ if (ref $data eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 27 7 34
elsif (ref $data eq 'HASH') { } 6 1 7
181 27 ▶ if (ref $data->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->[0]) { } 8 19 27
182  
183 8 foreach my $pair (@$data) {
184 10 ▶ return ExtraFieldError("Not list of lists")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pair eq "ARRAY" 2 8 10
185 unless ref $pair eq 'ARRAY' ;
186  
187 8 my $bad = validateExtraFieldPair($pair, $strict, $gzipMode) ;
188 8 ▶ return $bad if $bad ;
Branch: 1 branch
BranchTrueFalseTotal
if $bad 5 3 8
189  
190 3 $out .= mkSubField(@$pair);
191 }
192 }
193 else {
194 19 ▶ return ExtraFieldError("Not even number of elements")
Branch: 1 branch
BranchTrueFalseTotal
unless @$data % 2 == 0 1 18 19
195 unless @$data % 2 == 0;
196  
197 18 ▶ for (my $ix = 0; $ix <= @$data -1 ; $ix += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix <= @$data - 1) 23 15 38
198 23 my $bad = validateExtraFieldPair([$data->[$ix],
199 $data->[$ix+1]],
200 $strict, $gzipMode) ;
201 23 ▶ return $bad if $bad ;
Branch: 1 branch
BranchTrueFalseTotal
if $bad 3 20 23
202  
203 20 $out .= mkSubField($data->[$ix], $data->[$ix+1]);
204 }
205 }
206 }
207 elsif (ref $data eq 'HASH') {
208 6 ▶ while (my ($id, $info) = each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 8 3 11
209 8 my $bad = validateExtraFieldPair([$id, $info], $strict, $gzipMode);
210 8 ▶ return $bad if $bad ;
Branch: 1 branch
BranchTrueFalseTotal
if $bad 3 5 8
211  
212 5 $out .= mkSubField($id, $info);
213 }
214 }
215 else {
216 1 return ExtraFieldError("Not a scalar, array ref or hash ref") ;
217 }
218  
219 19 ▶ return ExtraFieldError("Too Large")
Branch: 1 branch
BranchTrueFalseTotal
if length $out > 65535 0 19 19
220 if length $out > GZIP_FEXTRA_MAX_SIZE;
221  
222 19 $_[0] = $out ;
223  
224 19 return undef;
225 }
226  
227 125 1;
228  
229 __END__
« blib/lib/IO/Compress/Gzip.pm ↑ Summary blib/lib/IO/Compress/Base.pm »