Image/ExifTool/RSRC.pm

filter: Statementstmt 70.5% 72 / 102 Branchbran 39.0% 32 / 82 Conditioncond 23.5% 12 / 51 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 44.6% 120 / 269 CC 59 SCAR 63.3
CC
59
Cov
49%
ProcessRSRC
588.0
BEGIN
1.0
BEGIN
1.0
CRAP 559.6
« Image/ExifTool/XMP.pm ↑ Summary Image/ExifTool/MPEG.pm »
1 #------------------------------------------------------------------------------
2 # File: RSRC.pm
3 #
4 # Description: Read Mac OS Resource information
5 #
6 # Revisions: 2010/03/17 - P. Harvey Created
7 #
8 # References: 1) http://developer.apple.com/legacy/mac/library/documentation/mac/MoreToolbox/MoreToolbox-99.html
9 #------------------------------------------------------------------------------
10  
11 package Image::ExifTool::RSRC;
12  
13 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 $VERSION = '1.10';
18  
19 sub ProcessRSRC($$);
20  
21 # Information decoded from Mac OS resources
22 2 %Image::ExifTool::RSRC::Main = (
23 GROUPS => { 2 => 'Document' },
24 PROCESS_PROC => \&ProcessRSRC,
25 NOTES => q{
26 Tags extracted from Mac OS resource files, DFONT files and "._" sidecar
27 files. These tags may also be extracted from the resource fork of any file
28 in OS X, either by adding "/..namedfork/rsrc" to the filename to process the
29 resource fork alone, or by using the L<ExtractEmbedded|../ExifTool.html#ExtractEmbedded> (-ee) option to process
30 the resource fork as a sub-document of the main file. When writing,
31 ExifTool preserves the Mac OS resource fork by default, but it may deleted
32 with C<-rsrc:all=> on the command line.
33 },
34 '8BIM' => {
35 Name => 'PhotoshopInfo',
36 SubDirectory => { TagTable => 'Image::ExifTool::Photoshop::Main' },
37 },
38 'sfnt' => {
39 Name => 'Font',
40 SubDirectory => { TagTable => 'Image::ExifTool::Font::Name' },
41 },
42 # my samples of postscript-type DFONT files have a POST resource
43 # with ID 0x1f5 and the same format as a PostScript file
44 'POST_0x01f5' => {
45 Name => 'PostscriptFont',
46 SubDirectory => { TagTable => 'Image::ExifTool::PostScript::Main' },
47 },
48 'usro_0x0000' => 'OpenWithApplication',
49 'vers_0x0001' => 'ApplicationVersion',
50 'STR _0xbff3' => 'ApplicationMissingMsg',
51 'STR _0xbff4' => 'CreatorApplication',
52 # the following written by Photoshop
53 # (ref http://www.adobe.ca/devnet/photoshop/psir/ps_image_resources.pdf)
54 'STR#_0x0080' => 'Keywords',
55 'TEXT_0x0080' => 'Description',
56 # don't extract PICT's because the clip region isn't set properly
57 # in the PICT resource for some reason. Also, a dummy 512-byte
58 # header would have to be added to create a valid PICT file.
59 # 'PICT' => { Name => 'PreviewPICT', Binary => 1 },
60 );
61  
62 #------------------------------------------------------------------------------
63 # Read information from a Mac resource file (ref 1)
64 # Inputs: 0) ExifTool ref, 1) dirInfo ref
65 # Returns: 1 on success, 0 if this wasn't a valid resource file
66 sub ProcessRSRC($$)
67 2 ▶{
Subroutine ProcessRSRC: called CC=59 SCAR=63.8
Pod undocumented
68 2 my ($et, $dirInfo) = @_;
69 2 my $raf = $$dirInfo{RAF};
70 2 my ($hdr, $map, $buff, $i, $j);
71  
72 # allow access with data reference
73 2 ▶ $raf or $raf = File::RandomAccess->new($$dirInfo{DataPt});
Branch: 1 branch
BranchTrueFalseTotal
$raf or $raf = "File::RandomAccess"->new($dirInfo->{"DataPt"}) 1 1 2
74  
75 # attempt to validate the format as thoroughly as practical
76 2 ▶ return 0 unless $raf->Read($hdr, 30) == 30;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($hdr, 30) == 30 0 2 2
77 2 my ($datOff, $mapOff, $datLen, $mapLen) = unpack('N*', $hdr);
78 2 ▶ return 0 unless $raf->Seek(0, 2);
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Seek(0, 2) 0 2 2
79 2 my $fLen = $raf->Tell();
80 2 ▶ return 0 if $datOff < 0x10 or $datOff + $datLen > $fLen;
Branch: 1 branch
BranchTrueFalseTotal
if $datOff < 16 or $datOff + $datLen > $fLen 0 2 2
Condition33%1/3
$datOff < 16 or $datOff + $datLen > $fLen
l!l&&r!l&&!r
002
MC/DC0%0/2
$datOff < 16 or $datOff + $datLen > $fLen
$datOff < 16 $datOff + $datLen > $fLen
Truth table33%1/3
A or B
A= $datOff < 16B= $datOff + $datLen > $fLen
ABresult
1X1
011
000
81 2 ▶ return 0 if $mapOff < 0x10 or $mapOff + $mapLen > $fLen or $mapLen < 30;
Branch: 1 branch
BranchTrueFalseTotal
if $mapOff < 16 or $mapOff + $mapLen > $fLen or $mapLen < 30 0 2 2
Condition33%2/6
$mapOff < 16 or $mapOff + $mapLen > $fLen or $mapLen < 30
l!l&&r!l&&!r
002
$mapOff < 16 or $mapOff + $mapLen > $fLen
l!l&&r!l&&!r
002
MC/DC0%0/3
$mapOff < 16 or $mapOff + $mapLen > $fLen or $mapLen < 30
$mapOff < 16 $mapOff + $mapLen > $fLen $mapLen < 30
Truth table25%1/4
A or B or C
A= $mapOff < 16B= $mapOff + $mapLen > $fLenC= $mapLen < 30
ABCresult
1XX1
01X1
0011
0000
82 2 ▶ return 0 if $datOff < $mapOff and $datOff + $datLen > $mapOff;
Branch: 1 branch
BranchTrueFalseTotal
if $datOff < $mapOff and $datOff + $datLen > $mapOff 0 2 2
Condition66%2/3
$datOff < $mapOff and $datOff + $datLen > $mapOff
!ll&&!rl&&r
110
MC/DC0%0/2
$datOff < $mapOff and $datOff + $datLen > $mapOff
$datOff < $mapOff $datOff + $datLen > $mapOff
Truth table66%2/3
A and B
A= $datOff < $mapOffB= $datOff + $datLen > $mapOff
ABresult
0X0
100
111
83 2 ▶ return 0 if $mapOff < $datOff and $mapOff + $mapLen > $datOff;
Branch: 1 branch
BranchTrueFalseTotal
if $mapOff < $datOff and $mapOff + $mapLen > $datOff 0 2 2
Condition33%1/3
$mapOff < $datOff and $mapOff + $mapLen > $datOff
!ll&&!rl&&r
200
MC/DC0%0/2
$mapOff < $datOff and $mapOff + $mapLen > $datOff
$mapOff < $datOff $mapOff + $mapLen > $datOff
Truth table33%1/3
A and B
A= $mapOff < $datOffB= $mapOff + $mapLen > $datOff
ABresult
0X0
100
111
84  
85 # read the resource map
86 2 ▶ $raf->Seek($mapOff, 0) and $raf->Read($map, $mapLen) == $mapLen or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($mapOff, 0) and $raf->Read($map, $mapLen) == $mapLen or return 0 2 0 2
Condition33%1/3
$raf->Seek($mapOff, 0) and $raf->Read($map, $mapLen) == $mapLen
!ll&&!rl&&r
002
MC/DC0%0/2
$raf->Seek($mapOff, 0) and $raf->Read($map, $mapLen) == $mapLen
$raf->Seek($mapOff, 0) $raf->Read($map, $mapLen) == $mapLen
Truth table33%1/3
A and B
A= $raf->Seek($mapOff, 0)B= $raf->Read($map, $mapLen) == $mapLen
ABresult
0X0
100
111
87 2 SetByteOrder('MM');
88 2 my $typeOff = Get16u(\$map, 24);
89 2 my $nameOff = Get16u(\$map, 26);
90 2 my $numTypes = (Get16u(\$map, 28) + 1) & 0xffff;
91  
92 # validate offsets in the resource map
93 2 ▶ return 0 if $typeOff < 28 or $nameOff < 30;
Branch: 1 branch
BranchTrueFalseTotal
if $typeOff < 28 or $nameOff < 30 0 2 2
Condition33%1/3
$typeOff < 28 or $nameOff < 30
l!l&&r!l&&!r
002
MC/DC0%0/2
$typeOff < 28 or $nameOff < 30
$typeOff < 28 $nameOff < 30
Truth table33%1/3
A or B
A= $typeOff < 28B= $nameOff < 30
ABresult
1X1
011
000
94  
95 2 ▶ $et->SetFileType('RSRC') unless $$et{IN_RESOURCE};
Branch: 1 branch
BranchTrueFalseTotal
unless $et->{"IN_RESOURCE"} 2 0 2
96 2 my $verbose = $et->Options('Verbose');
97 2 my $tagTablePtr = GetTagTable('Image::ExifTool::RSRC::Main');
98 2 $et->VerboseDir('RSRC', $numTypes);
99  
100 # parse resource type list
101 2 ▶ for ($i=0; $i<$numTypes; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numTypes) 2 2 4
102 2 my $off = $typeOff + 2 + 8 * $i; # offset of entry in type list
103 2 ▶ last if $off + 8 > $mapLen;
Branch: 1 branch
BranchTrueFalseTotal
if $off + 8 > $mapLen 0 2 2
104 2 my $resType = substr($map,$off,4); # resource type
105 2 my $resNum = Get16u(\$map,$off+4); # number of resources - 1
106 2 my $refOff = Get16u(\$map,$off+6) + $typeOff; # offset to first resource reference
107 # loop through all resources
108 2 ▶ for ($j=0; $j<=$resNum; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $resNum) 2 2 4
109 2 my $roff = $refOff + 12 * $j;
110 2 ▶ last if $roff + 12 > $mapLen;
Branch: 1 branch
BranchTrueFalseTotal
if $roff + 12 > $mapLen 0 2 2
111 # read only the 24-bit resource data offset
112 2 my $id = Get16u(\$map,$roff);
113 2 my $resOff = (Get32u(\$map,$roff+4) & 0x00ffffff) + $datOff;
114 2 my $resNameOff = Get16u(\$map,$roff+2) + $nameOff + $mapOff;
115 2 my ($tag, $val, $valLen);
116 2 my $tagInfo = $$tagTablePtr{$resType};
117 2 ▶ if ($tagInfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagInfo) { } 1 1 2
118 1 $tag = $resType;
119 } else {
120 1 $tag = sprintf('%s_0x%.4x', $resType, $id);
121 1 $tagInfo = $$tagTablePtr{$tag};
122 }
123 # read the resource data if necessary
124 2 ▶ if ($tagInfo or $verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagInfo or $verbose) 2 0 2
Condition33%1/3
$tagInfo or $verbose
l!l&&r!l&&!r
200
MC/DC0%0/2
$tagInfo or $verbose
$tagInfo $verbose
Truth table33%1/3
A or B
A= $tagInfoB= $verbose
ABresult
1X1
011
000
125 2 ▶ unless ($raf->Seek($resOff, 0) and $raf->Read($buff, 4) == 4 and
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf->Seek($resOff, 0) and $raf->Read($buff, 4) == 4 and ($valLen = unpack("N", $buff)) < 100000000 and $raf->Read($val, $valLen) == $valLen) 0 2 2
Condition33%3/9
$raf->Seek($resOff, 0) and $raf->Read($buff, 4) == 4 and ($valLen = unpack("N", $buff)) < 100000000 and $raf->Read($val, $valLen) == $valLen
!ll&&!rl&&r
002
$raf->Seek($resOff, 0) and $raf->Read($buff, 4) == 4 and ($valLen = unpack("N", $buff)) < 100000000
!ll&&!rl&&r
002
$raf->Seek($resOff, 0) and $raf->Read($buff, 4) == 4
!ll&&!rl&&r
002
MC/DC0%0/4
$raf->Seek($resOff, 0) and $raf->Read($buff, 4) == 4 and ($valLen = unpack("N", $buff)) < 100000000 and $raf->Read($val, $valLen) == $valLen
$raf->Seek($resOff, 0) $raf->Read($buff, 4) == 4 ($valLen = unpack("N", $buff)) < 100000000 $raf->Read($val, $valLen) == $valLen
Truth table20%1/5
A and B and C and D
A= $raf->Seek($resOff, 0)B= $raf->Read($buff, 4) == 4C= ($valLen = unpack("N", $buff)) < 100000000D= $raf->Read($val, $valLen) == $valLen
ABCDresult
0XXX0
10XX0
110X0
11100
11111
126 ($valLen = unpack('N', $buff)) < 100000000 and # arbitrary size limit (100MB)
127 $raf->Read($val, $valLen) == $valLen)
128 {
129 0 $et->Warn("Error reading $resType resource");
130 0 next;
131 }
132 }
133 2 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 2 2
134 0 my ($resName, $nameLen);
135 0 ▶ $resName = '' unless $raf->Seek($resNameOff, 0) and $raf->Read($buff, 1) and
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Seek($resNameOff, 0) and $raf->Read($buff, 1) and ($nameLen = ord $buff) != 0 and $raf->Read($resName, $nameLen) == $nameLen 0 0 0
Condition0%0/9
$raf->Seek($resNameOff, 0) and $raf->Read($buff, 1) and ($nameLen = ord $buff) != 0 and $raf->Read($resName, $nameLen) == $nameLen
!ll&&!rl&&r
000
$raf->Seek($resNameOff, 0) and $raf->Read($buff, 1) and ($nameLen = ord $buff) != 0
!ll&&!rl&&r
000
$raf->Seek($resNameOff, 0) and $raf->Read($buff, 1)
!ll&&!rl&&r
000
MC/DC0%0/4
$raf->Seek($resNameOff, 0) and $raf->Read($buff, 1) and ($nameLen = ord $buff) != 0 and $raf->Read($resName, $nameLen) == $nameLen
$raf->Seek($resNameOff, 0) $raf->Read($buff, 1) ($nameLen = ord $buff) != 0 $raf->Read($resName, $nameLen) == $nameLen
Truth table0%0/5
A and B and C and D
A= $raf->Seek($resNameOff, 0)B= $raf->Read($buff, 1)C= ($nameLen = ord $buff) != 0D= $raf->Read($resName, $nameLen) == $nameLen
ABCDresult
0XXX0
10XX0
110X0
11100
11111
136 ($nameLen = ord $buff) != 0 and $raf->Read($resName, $nameLen) == $nameLen;
137 0 $et->VPrint(0,sprintf("%s resource ID 0x%.4x (offset 0x%.4x, $valLen bytes, name='%s'):\n",
138 $resType, $id, $resOff, $resName));
139 0 $et->VerboseDump(\$val);
140 }
141 2 ▶ next unless $tagInfo;
Branch: 1 branch
BranchTrueFalseTotal
unless $tagInfo 0 2 2
142 2 ▶ if ($resType eq 'vers') {
Branch: 8 branches
BranchTrueFalseTotal
if ($resType eq 'vers') { } 1 1 2
elsif ($resType eq 'sfnt') { } 1 0 1
elsif ($resType eq '8BIM') { } 0 0 0
elsif ($resType eq 'STR ' and $valLen > 1) { } 0 0 0
elsif ($resType eq 'usro' and $valLen > 4) { } 0 0 0
elsif ($resType eq 'STR#' and $valLen > 2) { } 0 0 0
elsif ($resType eq 'POST') { } 0 0 0
elsif ($resType ne 'TEXT') { } 0 0 0
143 # parse the 'vers' resource to get the long version string
144 1 ▶ next unless $valLen > 8;
Branch: 1 branch
BranchTrueFalseTotal
unless $valLen > 8 0 1 1
145 # long version string is after short version
146 1 my $p = 7 + Get8u(\$val, 6);
147 1 ▶ next if $p >= $valLen;
Branch: 1 branch
BranchTrueFalseTotal
if $p >= $valLen 0 1 1
148 1 my $vlen = Get8u(\$val, $p++);
149 1 ▶ next if $p + $vlen > $valLen;
Branch: 1 branch
BranchTrueFalseTotal
if $p + $vlen > $valLen 0 1 1
150 1 my $tagTablePtr = GetTagTable('Image::ExifTool::RSRC::Main');
151 1 $val = $et->Decode(substr($val, $p, $vlen), 'MacRoman');
152 } elsif ($resType eq 'sfnt') {
153 # parse the OTF font block
154 1 ▶ $raf->Seek($resOff + 4, 0) or next;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($resOff + 4, 0) or next 1 0 1
155 1 $$dirInfo{Base} = $resOff + 4;
156 1 require Image::ExifTool::Font;
157 1 ▶ unless (Image::ExifTool::Font::ProcessOTF($et, $dirInfo)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Image::ExifTool::Font::ProcessOTF($et, $dirInfo)) 0 1 1
158 0 $et->Warn('Unrecognized sfnt resource format');
159 }
160 # assume this is a DFONT file unless processing the rsrc fork
161 1 ▶ $et->OverrideFileType('DFONT') unless $$et{DOC_NUM};
Branch: 1 branch
BranchTrueFalseTotal
unless $et->{"DOC_NUM"} 1 0 1
162 1 next;
163 } elsif ($resType eq '8BIM') {
164 0 my $ttPtr = GetTagTable('Image::ExifTool::Photoshop::Main');
165 0 $et->HandleTag($ttPtr, $id, $val,
166 DataPt => \$val,
167 DataPos => $resOff + 4,
168 Size => $valLen,
169 Start => 0,
170 Parent => 'RSRC',
171 );
172 0 next;
173 ▶ } elsif ($resType eq 'STR ' and $valLen > 1) {
Condition0%0/3
$resType eq "STR " and $valLen > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$resType eq "STR " and $valLen > 1
$resType eq "STR " $valLen > 1
Truth table0%0/3
A and B
A= $resType eq "STR "B= $valLen > 1
ABresult
0X0
100
111
174 # extract Pascal string
175 0 my $len = ord $val;
176 0 ▶ next unless $valLen >= $len + 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $valLen >= $len + 1 0 0 0
177 0 $val = substr($val, 1, $len);
178 ▶ } elsif ($resType eq 'usro' and $valLen > 4) {
Condition0%0/3
$resType eq "usro" and $valLen > 4
!ll&&!rl&&r
000
MC/DC0%0/2
$resType eq "usro" and $valLen > 4
$resType eq "usro" $valLen > 4
Truth table0%0/3
A and B
A= $resType eq "usro"B= $valLen > 4
ABresult
0X0
100
111
179 0 my $len = unpack('N', $val);
180 0 ▶ next unless $valLen >= $len + 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $valLen >= $len + 4 0 0 0
181 0 ($val = substr($val, 4, $len)) =~ s/\0.*//g; # truncate at null
182 ▶ } elsif ($resType eq 'STR#' and $valLen > 2) {
Condition0%0/3
$resType eq "STR#" and $valLen > 2
!ll&&!rl&&r
000
MC/DC0%0/2
$resType eq "STR#" and $valLen > 2
$resType eq "STR#" $valLen > 2
Truth table0%0/3
A and B
A= $resType eq "STR#"B= $valLen > 2
ABresult
0X0
100
111
183 # extract list of strings (ref http://simtech.sourceforge.net/tech/strings.html)
184 0 my $num = unpack('n', $val);
185 0 ▶ next if $num & 0xf000; # (ignore special-format STR# resources)
Branch: 1 branch
BranchTrueFalseTotal
if $num & 61440 0 0 0
186 0 my ($i, @vals);
187 0 my $pos = 2;
188 0 ▶ for ($i=0; $i<$num; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num) 0 0 0
189 0 ▶ last if $pos >= $valLen;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $valLen 0 0 0
190 0 my $len = ord substr($val, $pos++, 1);
191 0 ▶ last if $pos + $len > $valLen;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $len > $valLen 0 0 0
192 0 push @vals, substr($val, $pos, $len);
193 0 $pos += $len;
194 }
195 0 $val = \@vals;
196 } elsif ($resType eq 'POST') {
197 # assume this is a DFONT file unless processing the rsrc fork
198 0 ▶ $et->OverrideFileType('DFONT') unless $$et{DOC_NUM};
Branch: 1 branch
BranchTrueFalseTotal
unless $et->{"DOC_NUM"} 0 0 0
199 0 $val = substr $val, 2;
200 } elsif ($resType ne 'TEXT') {
201 0 next;
202 }
203 1 $et->HandleTag($tagTablePtr, $tag, $val);
204 }
205 }
206 2 return 1;
207 }
208  
209 2 1; # end
210  
211 __END__
« Image/ExifTool/XMP.pm ↑ Summary Image/ExifTool/MPEG.pm »