blib/lib/IO/Compress/Gzip.pm

filter: Statementstmt 99.1% 118 / 119 Branchbran 93.7% 60 / 64 Conditioncond 88.8% 24 / 27 MC/DCmcdc 72.2% 13 / 18 Subroutinesub 95.0% 19 / 20 Podpod 22.2% 2 / 9 totaltotal 91.8% 236 / 257 CC 41 SCAR 25.8
CC
41
Cov
96%
ckParams
23.0
mkHeader
15.1
getFileInfo
4.0
CRAP 13.2
« blib/lib/IO/Uncompress/Base.pm ↑ Summary blib/lib/IO/Compress/Zlib/Extra.pm »
1 package IO::Compress::Gzip ;
2  
3 33 require 5.006 ;
4  
5 33 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 33 require Exporter ;
10  
11 33 ▶use IO::Compress::RawDeflate 2.223 () ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 33 ▶use IO::Compress::Adapter::Deflate 2.223 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 33 ▶use IO::Compress::Base::Common 2.223 qw(:Status );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 33 ▶use IO::Compress::Gzip::Constants 2.223 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 33 ▶use IO::Compress::Zlib::Extra 2.223 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 BEGIN
19 {
20 33 ▶ if (defined &utf8::downgrade )
Subroutine BEGIN: called CC=2 SCAR=8.1
Branch: 1 branch
BranchTrueFalseTotal
if (defined &utf8::downgrade) { } 33 0 33
21 33 { *noUTF8 = \&utf8::downgrade }
22 else
23 0 ▶ { *noUTF8 = sub {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 }
25  
26 33 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, %DEFLATE_CONSTANTS, $GzipError);
27  
28 33 $VERSION = '2.223';
29 33 $GzipError = '' ;
30  
31 33 @ISA = qw(IO::Compress::RawDeflate Exporter);
32 33 @EXPORT_OK = qw( $GzipError gzip ) ;
33 33 %EXPORT_TAGS = %IO::Compress::RawDeflate::DEFLATE_CONSTANTS ;
34  
35 33 ▶$EXPORT_TAGS{all} = [ defined $EXPORT_TAGS{all} ? @{ $EXPORT_TAGS{all} } : (), @EXPORT_OK ] ;
Branch: 1 branch
BranchTrueFalseTotal
defined $IO::Compress::Gzip::EXPORT_TAGS{'all'} ? : 33 0 33
36 33 Exporter::export_ok_tags('all');
37  
38 sub new
39 {
40 317 ▶ my $class = shift ;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
41  
42 317 my $obj = IO::Compress::Base::Common::createSelfTiedObject($class, \$GzipError);
43  
44 317 $obj->_create(undef, @_);
45 }
46  
47  
48 sub gzip
49 {
50 162 ▶ my $obj = IO::Compress::Base::Common::createSelfTiedObject(undef, \$GzipError);
Subroutine gzip: called CC=1 SCAR=0.0
Pod documented
51 162 return $obj->_def(@_);
52 }
53  
54 #sub newHeader
55 #{
56 # my $self = shift ;
57 # #return GZIP_MINIMUM_HEADER ;
58 # return $self->mkHeader(*$self->{Got});
59 #}
60  
61 sub getExtraParams
62 {
63 478 ▶ my $self = shift ;
Subroutine getExtraParams: called CC=1 SCAR=0.0
Pod undocumented
64  
65 return (
66 # zlib behaviour
67 478 $self->getZlibParams(),
68  
69 # Gzip header fields
70 'minimal' => [IO::Compress::Base::Common::Parse_boolean, 0],
71 'comment' => [IO::Compress::Base::Common::Parse_any, undef],
72 'name' => [IO::Compress::Base::Common::Parse_any, undef],
73 'time' => [IO::Compress::Base::Common::Parse_any, undef],
74 'textflag' => [IO::Compress::Base::Common::Parse_boolean, 0],
75 'headercrc' => [IO::Compress::Base::Common::Parse_boolean, 0],
76 'os_code' => [IO::Compress::Base::Common::Parse_unsigned, $Compress::Raw::Zlib::gzip_os_code],
77 'extrafield'=> [IO::Compress::Base::Common::Parse_any, undef],
78 'extraflags'=> [IO::Compress::Base::Common::Parse_any, undef],
79  
80 );
81 }
82  
83  
84 sub ckParams
85 {
86 474 ▶ my $self = shift ;
Subroutine ckParams: called CC=23 SCAR=31.4
Pod undocumented
87 474 my $got = shift ;
88  
89 # gzip always needs crc32
90 474 $got->setValue('crc32' => 1);
91  
92 474 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $got->getValue("merge") 29 445 474
93 if $got->getValue('merge') ;
94  
95 445 my $strict = $got->getValue('strict') ;
96  
97  
98 {
99 445 ▶ if (! $got->parsed('time') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($got->parsed("time")) 379 66 445
100 # Modification time defaults to now.
101 379 $got->setValue(time => time) ;
102 }
103  
104 # Check that the Name & Comment don't have embedded NULLs
105 # Also check that they only contain ISO 8859-1 chars.
106 445 ▶ if ($got->parsed('name') && defined $got->getValue('name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got->parsed("name") and defined $got->getValue("name")) 80 365 445
Condition100%3/3
$got->parsed("name") and defined $got->getValue("name")
!ll&&!rl&&r
364180
MC/DC100%2/2
$got->parsed("name") and defined $got->getValue("name")
$got->parsed("name") defined $got->getValue("name")
Truth table100%3/3
A and B
A= $got->parsed("name")B= defined $got->getValue("name")
ABresult
0X0
100
111
107 80 my $name = $got->getValue('name');
108  
109 80 ▶ return $self->saveErrorString(undef, "Null Character found in Name",
Branch: 1 branch
BranchTrueFalseTotal
if $strict and $name =~ /\x00/ 2 78 80
Condition100%3/3
$strict and $name =~ /\x00/
!ll&&!rl&&r
5732
MC/DC100%2/2
$strict and $name =~ /\x00/
$strict $name =~ /\x00/
Truth table100%3/3
A and B
A= $strictB= $name =~ /\x00/
ABresult
0X0
100
111
110 Z_DATA_ERROR)
111 if $strict && $name =~ /\x00/ ;
112  
113 78 ▶ return $self->saveErrorString(undef, "Non ISO 8859-1 Character found in Name",
Branch: 1 branch
BranchTrueFalseTotal
if $strict and $name =~ /$IO::Compress::Gzip::GZIP_FNAME_INVALID_CHAR_RE/o 1 77 78
Condition100%3/3
$strict and $name =~ /$IO::Compress::Gzip::GZIP_FNAME_INVALID_CHAR_RE/o
!ll&&!rl&&r
5721
MC/DC100%2/2
$strict and $name =~ /$IO::Compress::Gzip::GZIP_FNAME_INVALID_CHAR_RE/o
$strict $name =~ /$IO::Compress::Gzip::GZIP_FNAME_INVALID_CHAR_RE/o
Truth table100%3/3
A and B
A= $strictB= $name =~ /$IO::Compress::Gzip::GZIP_FNAME_INVALID_CHAR_RE/o
ABresult
0X0
100
111
114 Z_DATA_ERROR)
115 if $strict && $name =~ /$GZIP_FNAME_INVALID_CHAR_RE/o ;
116 }
117  
118 442 ▶ if ($got->parsed('comment') && defined $got->getValue('comment')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got->parsed("comment") and defined $got->getValue("comment")) 38 404 442
Condition100%3/3
$got->parsed("comment") and defined $got->getValue("comment")
!ll&&!rl&&r
403138
MC/DC100%2/2
$got->parsed("comment") and defined $got->getValue("comment")
$got->parsed("comment") defined $got->getValue("comment")
Truth table100%3/3
A and B
A= $got->parsed("comment")B= defined $got->getValue("comment")
ABresult
0X0
100
111
119 38 my $comment = $got->getValue('comment');
120  
121 38 ▶ return $self->saveErrorString(undef, "Null Character found in Comment",
Branch: 1 branch
BranchTrueFalseTotal
if $strict and $comment =~ /\x00/ 2 36 38
Condition100%3/3
$strict and $comment =~ /\x00/
!ll&&!rl&&r
5312
MC/DC100%2/2
$strict and $comment =~ /\x00/
$strict $comment =~ /\x00/
Truth table100%3/3
A and B
A= $strictB= $comment =~ /\x00/
ABresult
0X0
100
111
122 Z_DATA_ERROR)
123 if $strict && $comment =~ /\x00/ ;
124  
125 36 ▶ return $self->saveErrorString(undef, "Non ISO 8859-1 Character found in Comment",
Branch: 1 branch
BranchTrueFalseTotal
if $strict and $comment =~ /$IO::Compress::Gzip::GZIP_FCOMMENT_INVALID_CHAR_RE/o 1 35 36
Condition100%3/3
$strict and $comment =~ /$IO::Compress::Gzip::GZIP_FCOMMENT_INVALID_CHAR_RE/o
!ll&&!rl&&r
5301
MC/DC100%2/2
$strict and $comment =~ /$IO::Compress::Gzip::GZIP_FCOMMENT_INVALID_CHAR_RE/o
$strict $comment =~ /$IO::Compress::Gzip::GZIP_FCOMMENT_INVALID_CHAR_RE/o
Truth table100%3/3
A and B
A= $strictB= $comment =~ /$IO::Compress::Gzip::GZIP_FCOMMENT_INVALID_CHAR_RE/o
ABresult
0X0
100
111
126 Z_DATA_ERROR)
127 if $strict && $comment =~ /$GZIP_FCOMMENT_INVALID_CHAR_RE/o;
128 }
129  
130 439 ▶ if ($got->parsed('os_code') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got->parsed("os_code")) 6 433 439
131 6 my $value = $got->getValue('os_code');
132  
133 6 ▶ return $self->saveErrorString(undef, "OS_Code must be between 0 and 255, got '$value'")
Branch: 1 branch
BranchTrueFalseTotal
if $value < 0 or $value > 255 1 5 6
Condition66%2/3
$value < 0 or $value > 255
l!l&&r!l&&!r
015
MC/DC50%1/2
$value < 0 or $value > 255
$value < 0 $value > 255
Truth table66%2/3
A or B
A= $value < 0B= $value > 255
ABresult
1X1
011
000
134 if $value < 0 || $value > 255 ;
135  
136 }
137  
138 # gzip only supports Deflate at present
139 438 $got->setValue('method' => Z_DEFLATED) ;
140  
141 438 ▶ if ( ! $got->parsed('extraflags')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($got->parsed("extraflags")) 437 1 438
142 437 ▶ $got->setValue('extraflags' => 2)
Branch: 1 branch
BranchTrueFalseTotal
if $got->getValue("level") == Z_BEST_COMPRESSION() 1 436 437
143 if $got->getValue('level') == Z_BEST_COMPRESSION ;
144 437 ▶ $got->setValue('extraflags' => 4)
Branch: 1 branch
BranchTrueFalseTotal
if $got->getValue("level") == Z_BEST_SPEED() 1 436 437
145 if $got->getValue('level') == Z_BEST_SPEED ;
146 }
147  
148 438 my $data = $got->getValue('extrafield') ;
149 438 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 82 356 438
150 82 my $bad = IO::Compress::Zlib::Extra::parseExtraField($data, $strict, 1) ;
151 82 ▶ return $self->saveErrorString(undef, "Error with ExtraField Parameter: $bad", Z_DATA_ERROR)
Branch: 1 branch
BranchTrueFalseTotal
if $bad 20 62 82
152 if $bad ;
153  
154 62 $got->setValue('extrafield' => $data) ;
155 }
156 }
157  
158 418 return 1;
159 }
160  
161 sub mkTrailer
162 {
163 440 ▶ my $self = shift ;
Subroutine mkTrailer: called CC=1 SCAR=0.0
Pod undocumented
164 return pack("V V", *$self->{Compress}->crc32(),
165 440 *$self->{UnCompSize}->get32bit());
166 }
167  
168 sub getInverseClass
169 {
170 33 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
171 25 ▶ return ('IO::Uncompress::Gunzip',
Subroutine getInverseClass: called CC=1 SCAR=0.0
Pod undocumented
172 \$IO::Uncompress::Gunzip::GunzipError);
173 }
174  
175 sub getFileInfo
176 {
177 110 ▶ my $self = shift ;
Subroutine getFileInfo: called CC=4 SCAR=13.9
Pod undocumented
178 110 my $params = shift;
179 110 my $filename = shift ;
180  
181 110 ▶ return if IO::Compress::Base::Common::isaScalar($filename);
Branch: 1 branch
BranchTrueFalseTotal
if IO::Compress::Base::Common::isaScalar($filename) 44 66 110
182  
183 66 my $defaultTime = (stat($filename))[9] ;
184  
185 66 ▶ $params->setValue('name' => $filename)
Branch: 1 branch
BranchTrueFalseTotal
unless $params->parsed("name") 59 7 66
186 if ! $params->parsed('name') ;
187  
188 66 ▶ $params->setValue('time' => $defaultTime)
Branch: 1 branch
BranchTrueFalseTotal
if not $params->parsed("time") 59 7 66
189 if ! $params->parsed('time') ;
190 }
191  
192  
193 sub mkHeader
194 {
195 420 ▶ my $self = shift ;
Subroutine mkHeader: called CC=15 SCAR=27.1
Pod undocumented
196 420 my $param = shift ;
197  
198 # short-circuit if a minimal header is requested.
199 420 ▶ return GZIP_MINIMUM_HEADER if $param->getValue('minimal') ;
Branch: 1 branch
BranchTrueFalseTotal
if $param->getValue("minimal") 37 383 420
200  
201 # METHOD
202 383 my $method = $param->valueOrDefault('method', GZIP_CM_DEFLATED) ;
203  
204 # FLAGS
205 383 my $flags = GZIP_FLG_DEFAULT ;
206 383 ▶ $flags |= GZIP_FLG_FTEXT if $param->getValue('textflag') ;
Branch: 1 branch
BranchTrueFalseTotal
if $param->getValue("textflag") 7 376 383
207 383 ▶ $flags |= GZIP_FLG_FHCRC if $param->getValue('headercrc') ;
Branch: 1 branch
BranchTrueFalseTotal
if $param->getValue("headercrc") 51 332 383
208 383 ▶ $flags |= GZIP_FLG_FEXTRA if $param->wantValue('extrafield') ;
Branch: 1 branch
BranchTrueFalseTotal
if $param->wantValue("extrafield") 62 321 383
209 383 ▶ $flags |= GZIP_FLG_FNAME if $param->wantValue('name') ;
Branch: 1 branch
BranchTrueFalseTotal
if $param->wantValue("name") 86 297 383
210 383 ▶ $flags |= GZIP_FLG_FCOMMENT if $param->wantValue('comment') ;
Branch: 1 branch
BranchTrueFalseTotal
if $param->wantValue("comment") 53 330 383
211  
212 # MTIME
213 383 my $time = $param->valueOrDefault('time', GZIP_MTIME_DEFAULT) ;
214  
215 # EXTRA FLAGS
216 383 my $extra_flags = $param->valueOrDefault('extraflags', GZIP_XFL_DEFAULT);
217  
218 # OS CODE
219 383 my $os_code = $param->valueOrDefault('os_code', GZIP_OS_DEFAULT) ;
220  
221  
222 383 my $out = pack("C4 V C C",
223 GZIP_ID1, # ID1
224 GZIP_ID2, # ID2
225 $method, # Compression Method
226 $flags, # Flags
227 $time, # Modification Time
228 $extra_flags, # Extra Flags
229 $os_code, # Operating System Code
230 ) ;
231  
232 # EXTRA
233 383 ▶ if ($flags & GZIP_FLG_FEXTRA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 4) 62 321 383
234 62 my $extra = $param->getValue('extrafield') ;
235 62 $out .= pack("v", length $extra) . $extra ;
236 }
237  
238 # NAME
239 383 ▶ if ($flags & GZIP_FLG_FNAME) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 8) 86 297 383
240 86 my $name .= $param->getValue('name') ;
241 86 $name =~ s/\x00.*$//;
242 86 $out .= $name ;
243 # Terminate the filename with NULL unless it already is
244 86 ▶ $out .= GZIP_NULL_BYTE
Branch: 1 branch
BranchTrueFalseTotal
if not length $name or substr($name, 1, -1) ne "\0" 86 0 86
Condition66%2/3
not length $name or substr($name, 1, -1) ne "\0"
l!l&&r!l&&!r
7790
MC/DC0%0/2
not length $name or substr($name, 1, -1) ne "\0"
not length $name substr($name, 1, -1) ne "\0"
Truth table66%2/3
not(A) or B
A= not length $nameB= substr($name, 1, -1) ne "\0"
ABresult
1X1
011
000
245 if !length $name or
246 substr($name, 1, -1) ne GZIP_NULL_BYTE ;
247 }
248  
249 # COMMENT
250 383 ▶ if ($flags & GZIP_FLG_FCOMMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 16) 53 330 383
251 53 my $comment .= $param->getValue('comment') ;
252 53 $comment =~ s/\x00.*$//;
253 53 $out .= $comment ;
254 # Terminate the comment with NULL unless it already is
255 53 ▶ $out .= GZIP_NULL_BYTE
Branch: 1 branch
BranchTrueFalseTotal
if not length $comment or substr($comment, 1, -1) ne "\0" 53 0 53
Condition66%2/3
not length $comment or substr($comment, 1, -1) ne "\0"
l!l&&r!l&&!r
1520
MC/DC0%0/2
not length $comment or substr($comment, 1, -1) ne "\0"
not length $comment substr($comment, 1, -1) ne "\0"
Truth table66%2/3
not(A) or B
A= not length $commentB= substr($comment, 1, -1) ne "\0"
ABresult
1X1
011
000
256 if ! length $comment or
257 substr($comment, 1, -1) ne GZIP_NULL_BYTE;
258 }
259  
260 # HEADER CRC
261 383 ▶ $out .= pack("v", Compress::Raw::Zlib::crc32($out) & 0x00FF )
Branch: 1 branch
BranchTrueFalseTotal
if $param->getValue("headercrc") 51 332 383
262 if $param->getValue('headercrc') ;
263  
264 383 noUTF8($out);
265  
266 383 return $out ;
267 }
268  
269 sub mkFinalTrailer
270 {
271 413 ▶ return '';
Subroutine mkFinalTrailer: called CC=1 SCAR=0.0
Pod undocumented
272 }
273  
274 33 1;
275  
276 __END__
« blib/lib/IO/Uncompress/Base.pm ↑ Summary blib/lib/IO/Compress/Zlib/Extra.pm »