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

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