blib/lib/IO/Uncompress/Gunzip.pm

filter: Statementstmt 92.1% 117 / 127 Branchbran 84.2% 64 / 76 Conditioncond 93.3% 14 / 15 MC/DCmcdc 90.0% 9 / 10 Subroutinesub 94.4% 17 / 18 Podpod 25.0% 2 / 8 totaltotal 87.8% 223 / 254 CC 43 SCAR 30.1
CC
43
Cov
89%
_readGzipHeader
33.1
_readFullGzipHeader
20.0
chkTrailer
4.0
CRAP 20.3
« blib/lib/IO/Compress/Zip.pm ↑ Summary blib/lib/IO/Uncompress/Base.pm »
1  
2 package IO::Uncompress::Gunzip ;
3 # for RFC1952
4  
5 96 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 96 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 96 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 96 ▶use IO::Uncompress::RawInflate 2.225 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 96 ▶use Compress::Raw::Zlib 2.225 () ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 96 ▶use IO::Compress::Base::Common 2.225 qw(:Status );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 96 ▶use IO::Compress::Gzip::Constants 2.225 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 96 ▶use IO::Compress::Zlib::Extra 2.225 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 96 require Exporter ;
17  
18 96 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $GunzipError);
19  
20 96 @ISA = qw(IO::Uncompress::RawInflate Exporter);
21 96 @EXPORT_OK = qw( $GunzipError gunzip );
22 96 %EXPORT_TAGS = %IO::Uncompress::RawInflate::DEFLATE_CONSTANTS ;
23 0 ▶$EXPORT_TAGS{all} = [ defined $EXPORT_TAGS{all} ? @{ $EXPORT_TAGS{all} } : (), @EXPORT_OK ] ;
Branch: 1 branch
BranchTrueFalseTotal
defined $IO::Uncompress::Gunzip::EXPORT_TAGS{'all'} ? : 0 96 96
24 96 Exporter::export_ok_tags('all');
25  
26 96 $GunzipError = '';
27  
28 96 $VERSION = '2.225';
29  
30 sub new
31 424 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 424 my $class = shift ;
33 424 $GunzipError = '';
34 424 my $obj = IO::Compress::Base::Common::createSelfTiedObject($class, \$GunzipError);
35  
36 424 $obj->_create(undef, 0, @_);
37 }
38  
39 sub gunzip
40 74 ▶{
Subroutine gunzip: called CC=1 SCAR=0.0
Pod documented
41 74 my $obj = IO::Compress::Base::Common::createSelfTiedObject(undef, \$GunzipError);
42 74 return $obj->_inf(@_) ;
43 }
44  
45 sub getExtraParams
46 481 ▶{
Subroutine getExtraParams: called CC=1 SCAR=0.0
Pod undocumented
47 return (
48 481 'parseextra' => [IO::Compress::Base::Common::Parse_boolean, 0],
49 'newstreamhook' => [IO::Compress::Base::Common::Parse_code, undef],
50 ) ;
51 }
52  
53 sub ckParams
54 489 ▶{
Subroutine ckParams: called CC=1 SCAR=0.0
Pod undocumented
55 489 my $self = shift ;
56 489 my $got = shift ;
57  
58 # gunzip always needs crc32
59 489 $got->setValue('crc32' => 1);
60  
61 489 *$self->{'newstreamhook'} = $got->getValue('newstreamhook');
62  
63 489 return 1;
64 }
65  
66 sub ckMagic
67 1678 ▶{
Subroutine ckMagic: called CC=3 SCAR=11.0
Pod undocumented
68 1678 my $self = shift;
69  
70 1678 my $magic ;
71 1678 $self->smartReadExact(\$magic, GZIP_ID_SIZE);
72  
73 1678 *$self->{HeaderPending} = $magic ;
74  
75 1678 ▶ return $self->HeaderError("Minimum header size is " .
Branch: 1 branch
BranchTrueFalseTotal
if length $magic != 2 37 1641 1678
76 GZIP_MIN_HEADER_SIZE . " bytes")
77 if length $magic != GZIP_ID_SIZE ;
78  
79 1641 ▶ return $self->HeaderError("Bad Magic")
Branch: 1 branch
BranchTrueFalseTotal
unless isGzipMagic($magic) 588 1053 1641
80 if ! isGzipMagic($magic) ;
81  
82 1053 *$self->{Type} = 'rfc1952';
83  
84 1053 return $magic ;
85 }
86  
87 sub readHeader
88 1053 ▶{
Subroutine readHeader: called CC=1 SCAR=0.0
Pod undocumented
89 1053 my $self = shift;
90 1053 my $magic = shift;
91  
92 1053 return $self->_readGzipHeader($magic);
93 }
94  
95 sub chkTrailer
96 1005 ▶{
Subroutine chkTrailer: called CC=4 SCAR=13.9
Pod undocumented
97 1005 my $self = shift;
98 1005 my $trailer = shift;
99  
100 # Check CRC & ISIZE
101 1005 my ($CRC32, $ISIZE) = unpack("V V", $trailer) ;
102 1005 *$self->{Info}{CRC32} = $CRC32;
103 1005 *$self->{Info}{ISIZE} = $ISIZE;
104  
105 1005 ▶ if (*$self->{Strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if (*$self->{"Strict"}) 493 512 1005
106 return $self->TrailerError("CRC mismatch")
107 493 ▶ if $CRC32 != *$self->{Uncomp}->crc32() ;
Branch: 1 branch
BranchTrueFalseTotal
if $CRC32 != *$self->{"Uncomp"}->crc32 2 491 493
108  
109 491 my $exp_isize = *$self->{UnCompSize}->get32bit();
110 491 ▶ return $self->TrailerError("ISIZE mismatch. Got $ISIZE"
Branch: 1 branch
BranchTrueFalseTotal
if $ISIZE != $exp_isize 1 490 491
111 . ", expected $exp_isize")
112 if $ISIZE != $exp_isize ;
113 }
114  
115 1002 return STATUS_OK;
116 }
117  
118 sub isGzipMagic
119 1641 ▶{
Subroutine isGzipMagic: called CC=3 SCAR=11.0
Pod undocumented
120 1641 my $buffer = shift ;
121 1641 ▶ return 0 if length $buffer < GZIP_ID_SIZE ;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer < 2 0 1641 1641
122 1641 my ($id1, $id2) = unpack("C C", $buffer) ;
123 1641 ▶ return $id1 == GZIP_ID1 && $id2 == GZIP_ID2 ;
Condition100%3/3
$id1 == 31 && $id2 == 139
!ll&&!rl&&r
58711053
MC/DC100%2/2
$id1 == 31 && $id2 == 139
$id1 == 31 $id2 == 139
Truth table100%3/3
A && B
A= $id1 == 31B= $id2 == 139
ABresult
0X0
100
111
124 }
125  
126 sub _readFullGzipHeader($)
127 0 ▶{
Subroutine _readFullGzipHeader: not called CC=4 SCAR=30.0
128 0 my ($self) = @_ ;
129 0 my $magic = '' ;
130  
131 0 $self->smartReadExact(\$magic, GZIP_ID_SIZE);
132  
133 0 *$self->{HeaderPending} = $magic ;
134  
135 0 ▶ return $self->HeaderError("Minimum header size is " .
Branch: 1 branch
BranchTrueFalseTotal
if length $magic != 2 0 0 0
136 GZIP_MIN_HEADER_SIZE . " bytes")
137 if length $magic != GZIP_ID_SIZE ;
138  
139  
140 0 ▶ return $self->HeaderError("Bad Magic")
Branch: 1 branch
BranchTrueFalseTotal
unless isGzipMagic($magic) 0 0 0
141 if ! isGzipMagic($magic) ;
142  
143 0 my $status = $self->_readGzipHeader($magic);
144 0 ▶ delete *$self->{Transparent} if ! defined $status ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 0 0 0
145 0 return $status ;
146 }
147  
148 sub _readGzipHeader($)
149 1053 ▶{
Subroutine _readGzipHeader: called CC=33 SCAR=35.0
150 1053 my ($self, $magic) = @_ ;
151 1053 my ($HeaderCRC) ;
152 1053 my ($buffer) = '' ;
153  
154 1053 ▶ $self->smartReadExact(\$buffer, GZIP_MIN_HEADER_SIZE - GZIP_ID_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
$self->smartReadExact(\$buffer, 8) or return $self->HeaderError("Minimum header size is 10 bytes") 1053 0 1053
155 or return $self->HeaderError("Minimum header size is " .
156 GZIP_MIN_HEADER_SIZE . " bytes") ;
157  
158 1053 my $keep = $magic . $buffer ;
159 1053 *$self->{HeaderPending} = $keep ;
160  
161 # now split out the various parts
162 1053 my ($cm, $flag, $mtime, $xfl, $os) = unpack("C C V C C", $buffer) ;
163  
164 1053 ▶ $cm == GZIP_CM_DEFLATED
Branch: 1 branch
BranchTrueFalseTotal
$cm == 8 or return $self->HeaderError("Not Deflate (CM is $cm)") 1052 1 1053
165 or return $self->HeaderError("Not Deflate (CM is $cm)") ;
166  
167 # check for use of reserved bits
168 1052 ▶ return $self->HeaderError("Use of Reserved Bits in FLG field.")
Branch: 1 branch
BranchTrueFalseTotal
if $flag & 224 1 1051 1052
169 if $flag & GZIP_FLG_RESERVED ;
170  
171 1051 my $EXTRA ;
172 1051 my @EXTRA = () ;
173 1051 ▶ if ($flag & GZIP_FLG_FEXTRA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag & 4) 471 580 1051
174 471 $EXTRA = "" ;
175 471 ▶ $self->smartReadExact(\$buffer, GZIP_FEXTRA_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
$self->smartReadExact(\$buffer, 2) or return $self->TruncatedHeader("FEXTRA Length") 470 1 471
176 or return $self->TruncatedHeader("FEXTRA Length") ;
177  
178 470 my ($XLEN) = unpack("v", $buffer) ;
179 470 ▶ $self->smartReadExact(\$EXTRA, $XLEN)
Branch: 1 branch
BranchTrueFalseTotal
$self->smartReadExact(\$EXTRA, $XLEN) or return $self->TruncatedHeader("FEXTRA Body") 468 2 470
180 or return $self->TruncatedHeader("FEXTRA Body");
181 468 $keep .= $buffer . $EXTRA ;
182  
183 468 ▶ if ($XLEN && *$self->{'ParseExtra'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XLEN and *$self->{"ParseExtra"}) 446 22 468
Condition100%3/3
$XLEN and *$self->{"ParseExtra"}
!ll&&!rl&&r
715446
MC/DC100%2/2
$XLEN and *$self->{"ParseExtra"}
$XLEN *$self->{"ParseExtra"}
Truth table100%3/3
A and B
A= $XLENB= *$self->{"ParseExtra"}
ABresult
0X0
100
111
184 446 my $bad = IO::Compress::Zlib::Extra::parseRawExtra($EXTRA,
185 \@EXTRA, 1, 1);
186 446 ▶ return $self->HeaderError($bad)
Branch: 1 branch
BranchTrueFalseTotal
if defined $bad 4 442 446
187 if defined $bad;
188 }
189 }
190  
191 1044 my $origname ;
192 1044 ▶ if ($flag & GZIP_FLG_FNAME) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag & 8) 121 923 1044
193 121 ▶ $self->smartReadToDelim(\$origname, GZIP_NULL_BYTE) != STATUS_ERROR
Branch: 1 branch
BranchTrueFalseTotal
$self->smartReadToDelim(\$origname, "\0") != -1 or return $self->TruncatedHeader("FNAME") 121 0 121
194 or return $self->TruncatedHeader("FNAME");
195  
196 121 $keep .= $origname ;
197  
198 121 ▶ return $self->HeaderError("No NULL termination found in FNAME")
Branch: 1 branch
BranchTrueFalseTotal
if substr($origname, -1, 1) ne "\0" 5 116 121
199 if substr($origname, -1, 1) ne GZIP_NULL_BYTE ;
200  
201 # remove the terminating GZIP_NULL_BYTE
202 116 $origname = substr($origname, 0, -1);
203  
204 return $self->HeaderError("Non ISO 8859-1 Character found in Name")
205 116 ▶ if *$self->{Strict} && $origname =~ /$GZIP_FNAME_INVALID_CHAR_RE/o ;
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Strict"} and $origname =~ /$IO::Uncompress::Gunzip::GZIP_FNAME_INVALID_CHAR_RE/o 1 115 116
Condition66%2/3
*$self->{"Strict"} and $origname =~ /$IO::Uncompress::Gunzip::GZIP_FNAME_INVALID_CHAR_RE/o
!ll&&!rl&&r
11501
MC/DC50%1/2
*$self->{"Strict"} and $origname =~ /$IO::Uncompress::Gunzip::GZIP_FNAME_INVALID_CHAR_RE/o
*$self->{"Strict"} $origname =~ /$IO::Uncompress::Gunzip::GZIP_FNAME_INVALID_CHAR_RE/o
Truth table66%2/3
A and B
A= *$self->{"Strict"}B= $origname =~ /$IO::Uncompress::Gunzip::GZIP_FNAME_INVALID_CHAR_RE/o
ABresult
0X0
100
111
206 }
207  
208 1038 my $comment ;
209 1038 ▶ if ($flag & GZIP_FLG_FCOMMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag & 16) 452 586 1038
210 452 ▶ $self->smartReadToDelim(\$comment, GZIP_NULL_BYTE) != STATUS_ERROR
Branch: 1 branch
BranchTrueFalseTotal
$self->smartReadToDelim(\$comment, "\0") != -1 or return $self->TruncatedHeader("FCOMMENT") 452 0 452
211 or return $self->TruncatedHeader("FCOMMENT");
212  
213 452 $keep .= $comment ;
214  
215 452 ▶ return $self->HeaderError("No NULL termination found in FCOMMENT")
Branch: 1 branch
BranchTrueFalseTotal
if substr($comment, -1, 1) ne "\0" 8 444 452
216 if substr($comment, -1, 1) ne GZIP_NULL_BYTE ;
217  
218 # remove the terminating GZIP_NULL_BYTE
219 444 $comment = substr($comment, 0, -1);
220  
221 return $self->HeaderError("Non ISO 8859-1 Character found in Comment")
222 444 ▶ if *$self->{Strict} && $comment =~ /$GZIP_FCOMMENT_INVALID_CHAR_RE/o ;
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Strict"} and $comment =~ /$IO::Uncompress::Gunzip::GZIP_FCOMMENT_INVALID_CHAR_RE/o 1 443 444
Condition100%3/3
*$self->{"Strict"} and $comment =~ /$IO::Uncompress::Gunzip::GZIP_FCOMMENT_INVALID_CHAR_RE/o
!ll&&!rl&&r
114321
MC/DC100%2/2
*$self->{"Strict"} and $comment =~ /$IO::Uncompress::Gunzip::GZIP_FCOMMENT_INVALID_CHAR_RE/o
*$self->{"Strict"} $comment =~ /$IO::Uncompress::Gunzip::GZIP_FCOMMENT_INVALID_CHAR_RE/o
Truth table100%3/3
A and B
A= *$self->{"Strict"}B= $comment =~ /$IO::Uncompress::Gunzip::GZIP_FCOMMENT_INVALID_CHAR_RE/o
ABresult
0X0
100
111
223 }
224  
225 1029 ▶ if ($flag & GZIP_FLG_FHCRC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag & 2) 446 583 1029
226 446 ▶ $self->smartReadExact(\$buffer, GZIP_FHCRC_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
$self->smartReadExact(\$buffer, 2) or return $self->TruncatedHeader("FHCRC") 444 2 446
227 or return $self->TruncatedHeader("FHCRC");
228  
229 444 $HeaderCRC = unpack("v", $buffer) ;
230 444 my $crc16 = Compress::Raw::Zlib::crc32($keep) & 0xFF ;
231  
232 return $self->HeaderError("CRC16 mismatch.")
233 444 ▶ if *$self->{Strict} && $crc16 != $HeaderCRC;
Branch: 1 branch
BranchTrueFalseTotal
if *$self->{"Strict"} and $crc16 != $HeaderCRC 1 443 444
Condition100%3/3
*$self->{"Strict"} and $crc16 != $HeaderCRC
!ll&&!rl&&r
114321
MC/DC100%2/2
*$self->{"Strict"} and $crc16 != $HeaderCRC
*$self->{"Strict"} $crc16 != $HeaderCRC
Truth table100%3/3
A and B
A= *$self->{"Strict"}B= $crc16 != $HeaderCRC
ABresult
0X0
100
111
234  
235 443 $keep .= $buffer ;
236 }
237  
238 # Assume compression method is deflated for xfl tests
239 #if ($xfl) {
240 #}
241  
242 1026 *$self->{Type} = 'rfc1952';
243  
244 my $info = {
245 'Type' => 'rfc1952',
246 'FingerprintLength' => 2,
247 'HeaderLength' => length $keep,
248 'TrailerLength' => GZIP_TRAILER_SIZE,
249 'Header' => $keep,
250 'isMinimalHeader' => $keep eq GZIP_MINIMUM_HEADER ? 1 : 0,
251  
252 'MethodID' => $cm,
253 'MethodName' => $cm == GZIP_CM_DEFLATED ? "Deflated" : "Unknown" ,
254 'TextFlag' => $flag & GZIP_FLG_FTEXT ? 1 : 0,
255 'HeaderCRCFlag' => $flag & GZIP_FLG_FHCRC ? 1 : 0,
256 'NameFlag' => $flag & GZIP_FLG_FNAME ? 1 : 0,
257 'CommentFlag' => $flag & GZIP_FLG_FCOMMENT ? 1 : 0,
258 'ExtraFlag' => $flag & GZIP_FLG_FEXTRA ? 1 : 0,
259 'Name' => $origname,
260 'Comment' => $comment,
261 'Time' => $mtime,
262 'OsID' => $os,
263 'OsName' => defined $GZIP_OS_Names{$os}
264 1026 ▶ ? $GZIP_OS_Names{$os} : "Unknown",
Branch: 8 branches
BranchTrueFalseTotal
$keep eq "\037\213\cH\000\000\000\000\000\000\377" ? : 48 978 1026
$cm == 8 ? : 1026 0 1026
$flag & 1 ? : 7 1019 1026
$flag & 2 ? : 443 583 1026
$flag & 8 ? : 114 912 1026
$flag & 16 ? : 443 583 1026
$flag & 4 ? : 464 562 1026
defined $IO::Uncompress::Gunzip::GZIP_OS_Names{$os} ? : 1023 3 1026
265 'HeaderCRC' => $HeaderCRC,
266 'Flags' => $flag,
267 'ExtraFlags' => $xfl,
268 'ExtraFieldRaw' => $EXTRA,
269 'ExtraField' => [ @EXTRA ],
270  
271  
272 #'CompSize'=> $compsize,
273 #'CRC32'=> $CRC32,
274 #'OrigSize'=> $ISIZE,
275 };
276  
277 1026 ▶ if (*$self->{'newstreamhook'})
Branch: 1 branch
BranchTrueFalseTotal
if (*$self->{"newstreamhook"}) 5 1021 1026
278 {
279 5 my $cb = *$self->{'newstreamhook'} ;
280 5 $cb->($info);
281 }
282  
283 1026 return $info;
284 }
285  
286  
287 96 1;
288  
289 __END__
« blib/lib/IO/Compress/Zip.pm ↑ Summary blib/lib/IO/Uncompress/Base.pm »