blib/lib/Image/JpegCheck.pm

filter: Statementstmt 96.3% 53 / 55 Branchbran 85.0% 17 / 20 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 90.5% 86 / 95 CC 13 SCAR 16.2
CC
13
Cov
91%
_is_jpeg
7.2
is_jpeg
7.0
BEGIN
1.0
CRAP 5.1
↑ Summary
1 package Image::JpegCheck;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Fcntl ':seek';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 our $VERSION = '0.10';
9 5 our @ISA = qw/Exporter/;
10 5 our @EXPORT = ('is_jpeg');
11  
12 4112 ▶sub is_jpeg {
Subroutine is_jpeg: called CC=7 SCAR=19.5
Pod documented
13 4112 my ($file, ) = @_;
14 4112 ▶ if (ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 4108 4 4112
15 4108 ▶ if (ref $file eq 'GLOB') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $file eq 'GLOB') { } 4 4104 4108
elsif (ref $file eq 'SCALAR') { } 4101 3 4104
elsif (ref $file eq 'Path::Class::File') { } 2 1 3
16 4 return Image::JpegCheck::_is_jpeg($file);
17 } elsif (ref $file eq 'SCALAR') {
18 4101 ▶ open my $fh, '<', $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die $! 4101 0 4101
19 4101 return Image::JpegCheck::_is_jpeg($fh);
20 } elsif (ref $file eq 'Path::Class::File') {
21 2 return Image::JpegCheck::_is_jpeg($file->openr);
22 } else {
23 1 Carp::croak('is_jpeg requires file-glob or filename');
24 }
25 } else {
26 4 ▶ open my $fh, '<', $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die $! 4 0 4
27 4 binmode $fh;
28 4 my $ret = Image::JpegCheck::_is_jpeg($fh);
29 4 close $fh;
30 4 return $ret;
31 }
32 }
33  
34 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 SIZE_FIRST => 0xC0, # Range of segment identifier codes
36 SIZE_LAST => 0xC3, # that hold size info.
37 SECTION_MARKER => "\xFF",
38 SOI => "\xFF\xD8",
39 EOI => "\xFF\xD9",
40 EOI_RE => qr/\xFF\xD9\xFF*$/,
41 READ_SIZE => 512,
42 BYTE_STUFFING => "\xFF"x512,
43 5 };
44  
45 4111 ▶sub _is_jpeg {
Subroutine _is_jpeg: called CC=7 SCAR=19.7
46 4111 my $fh = $_[0];
47 4111 my ($buf, $code, $marker, $len);
48  
49 4111 read($fh, $buf, 2);
50 4111 ▶ return 0 if $buf ne SOI;
Branch: 1 branch
BranchTrueFalseTotal
if $buf ne "\377\330" 4 4107 4111
51  
52 4107 while (1) {
53 20529 read($fh, $buf, 2);
54 20529 ($marker, $code) = unpack("a a", $buf); # read segment header
55  
56 20529 ▶ while ( $code eq SECTION_MARKER && ($marker = $code) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq "\377" and $marker = $code) 108 20529 20637
Condition66%2/3
$code eq "\377" and $marker = $code
!ll&&!rl&&r
205290108
MC/DC50%1/2
$code eq "\377" and $marker = $code
$code eq "\377" $marker = $code
Truth table66%2/3
A and B
A= $code eq "\377"B= $marker = $code
ABresult
0X0
100
111
57 108 read($fh, $buf, 1);
58 108 ($code) = unpack("a", $buf);
59 }
60 20529 read($fh, $buf, 2);
61 20529 $len = unpack( "n", $buf );
62 20529 $code = ord($code);
63  
64 20529 ▶ if ($marker ne SECTION_MARKER) {
Branch: 2 branches
BranchTrueFalseTotal
if ($marker ne "\377") { } 0 20529 20529
elsif ($code >= 192 and $code <= 195) { } 4107 16422 20529
65 0 return 0; # invalid marker
66 ▶ } elsif (($code >= SIZE_FIRST) && ($code <= SIZE_LAST)) {
Condition66%2/3
$code >= 192 and $code <= 195
!ll&&!rl&&r
0164224107
MC/DC50%1/2
$code >= 192 and $code <= 195
$code >= 192 $code <= 195
Truth table66%2/3
A and B
A= $code >= 192B= $code <= 195
ABresult
0X0
100
111
67 4107 return 1; # got a size info
68 } else {
69 16422 seek $fh, $len-2, SEEK_CUR; # skip segment body
70 }
71 }
72 0 die "should not reach here";
73 }
74  
75 5 1;
76 __END__
↑ Summary