Image/ExifTool/InDesign.pm

filter: Statementstmt 62.1% 92 / 148 Branchbran 38.2% 49 / 128 Conditioncond 40.9% 9 / 22 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 48.7% 155 / 318 CC 72 SCAR 66.6
CC
72
Cov
50%
ProcessIND
813.5
BEGIN
1.0
BEGIN
1.0
CRAP 781.0
« Image/ExifTool/Matroska.pm ↑ Summary Image/ExifTool/PICT.pm »
1 #------------------------------------------------------------------------------
2 # File: InDesign.pm
3 #
4 # Description: Read/write meta information in Adobe InDesign files
5 #
6 # Revisions: 2009-06-17 - P. Harvey Created
7 #
8 # References: 1) http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart3.pdf
9 #------------------------------------------------------------------------------
10  
11 package Image::ExifTool::InDesign;
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 $VERSION = '1.09';
18  
19 # map for writing metadata to InDesign files (currently only write XMP)
20 1 my %indMap = (
21 XMP => 'IND',
22 );
23  
24 # GUID's used in InDesign files
25 1 my $masterPageGUID = "\x06\x06\xed\xf5\xd8\x1d\x46\xe5\xbd\x31\xef\xe7\xfe\x74\xb7\x1d";
26 1 my $objectHeaderGUID = "\xde\x39\x39\x79\x51\x88\x4b\x6c\x8E\x63\xee\xf8\xae\xe0\xdd\x38";
27 1 my $objectTrailerGUID = "\xfd\xce\xdb\x70\xf7\x86\x4b\x4f\xa4\xd3\xc7\x28\xb3\x41\x71\x06";
28  
29 #------------------------------------------------------------------------------
30 # Read or write meta information in an InDesign file
31 # Inputs: 0) ExifTool object reference, 1) dirInfo reference
32 # Returns: 1 on success, 0 if this wasn't a valid InDesign file, or -1 on write error
33 sub ProcessIND($$)
34 5 ▶{
Subroutine ProcessIND: called CC=72 SCAR=67.0
Pod undocumented
35 5 my ($et, $dirInfo) = @_;
36 5 my $raf = $$dirInfo{RAF};
37 5 my $outfile = $$dirInfo{OutFile};
38 5 my ($hdr, $buff, $buf2, $err, $writeLen, $foundXMP);
39  
40 # validate the InDesign file
41 5 ▶ return 0 unless $raf->Read($hdr, 16) == 16;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($hdr, 16) == 16 0 5 5
42 5 ▶ return 0 unless $hdr eq $masterPageGUID;
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr eq $masterPageGUID 0 5 5
43 5 ▶ return 0 unless $raf->Read($buff, 8) == 8;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($buff, 8) == 8 0 5 5
44 5 ▶ $et->SetFileType($buff eq 'DOCUMENT' ? 'INDD' : 'IND'); # set the FileType tag
Branch: 1 branch
BranchTrueFalseTotal
$buff eq 'DOCUMENT' ? : 5 0 5
45  
46 # read the master pages
47 5 ▶ $raf->Seek(0, 0) or $err = 'Seek error', goto DONE;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek(0, 0) or $err = "Seek error", goto DONE 5 0 5
48 5 ▶ unless ($raf->Read($buff, 4096) == 4096 and
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf->Read($buff, 4096) == 4096 and $raf->Read($buf2, 4096) == 4096) 0 5 5
Condition33%1/3
$raf->Read($buff, 4096) == 4096 and $raf->Read($buf2, 4096) == 4096
!ll&&!rl&&r
005
MC/DC0%0/2
$raf->Read($buff, 4096) == 4096 and $raf->Read($buf2, 4096) == 4096
$raf->Read($buff, 4096) == 4096 $raf->Read($buf2, 4096) == 4096
Truth table33%1/3
A and B
A= $raf->Read($buff, 4096) == 4096B= $raf->Read($buf2, 4096) == 4096
ABresult
0X0
100
111
49 $raf->Read($buf2, 4096) == 4096)
50 {
51 0 $err = 'Unexpected end of file';
52 0 goto DONE; # (goto's can be our friend)
53 }
54 5 SetByteOrder('II');
55 5 ▶ unless ($buf2 =~ /^\Q$masterPageGUID/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buf2 =~ /^\Q$masterPageGUID\E/) 0 5 5
56 0 $err = 'Second master page is invalid';
57 0 goto DONE;
58 }
59 5 my $seq1 = Get64u(\$buff, 264);
60 5 my $seq2 = Get64u(\$buf2, 264);
61 # take the most current master page
62 5 ▶ my $curPage = $seq2 > $seq1 ? \$buf2 : \$buff;
Branch: 1 branch
BranchTrueFalseTotal
$seq2 > $seq1 ? : 0 5 5
63 # byte order of stream data may be different than headers
64 5 my $streamInt32u = Get8u($curPage, 24);
65 5 ▶ if ($streamInt32u == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($streamInt32u == 1) { } 0 5 5
elsif ($streamInt32u == 2) { } 5 0 5
66 0 $streamInt32u = 'V'; # little-endian int32u
67 } elsif ($streamInt32u == 2) {
68 5 $streamInt32u = 'N'; # big-endian int32u
69 } else {
70 0 $err = 'Invalid stream byte order';
71 0 goto DONE;
72 }
73 5 my $pages = Get32u($curPage, 280);
74 5 ▶ $pages < 2 and $err = 'Invalid page count', goto DONE;
Branch: 1 branch
BranchTrueFalseTotal
$pages < 2 and $err = "Invalid page count", goto DONE 0 5 5
75 5 my $pos = $pages * 4096;
76 5 ▶ if ($pos > 0x7fffffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > 2147483647) 0 5 5
77 0 ▶ if (not $et->Options('LargeFileSupport')) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $et->Options('LargeFileSupport')) { } 0 0 0
elsif ($et->Options('LargeFileSupport') eq '2') { } 0 0 0
78 0 $err = 'InDesign files larger than 2 GB not supported (LargeFileSupport not set)';
79 0 goto DONE;
80 } elsif ($et->Options('LargeFileSupport') eq '2') {
81 0 $et->Warn('Processing large file (LargeFileSupport is 2)');
82 }
83 }
84 5 ▶ if ($outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) { } 2 3 5
85 # make XMP the preferred group for writing
86 2 $et->InitWriteDirs(\%indMap, 'XMP');
87  
88 2 ▶ Write($outfile, $buff, $buf2) or $err = 1, goto DONE;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $buff, $buf2) or $err = 1, goto DONE 2 0 2
89 2 my $result = Image::ExifTool::CopyBlock($raf, $outfile, $pos - 8192);
90 2 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 2 2
91 0 ▶ $err = defined $result ? 'Error reading InDesign database' : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $result ? : 0 0 0
92 0 goto DONE;
93 }
94 2 $writeLen = 0;
95 } else {
96 3 ▶ $raf->Seek($pos, 0) or $err = 'Seek error', goto DONE;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($pos, 0) or $err = "Seek error", goto DONE 3 0 3
97 }
98 # scan through the contiguous objects for XMP
99 5 my $verbose = $et->Options('Verbose');
100 5 my $out = $et->Options('TextOut');
101 5 for (;;) {
102 10 ▶ $raf->Read($hdr, 32) or last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($hdr, 32) or last 10 0 10
103 10 ▶ unless (length($hdr) == 32 and $hdr =~ /^\Q$objectHeaderGUID/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $hdr == 32 and $hdr =~ /^\Q$objectHeaderGUID\E/) 5 5 10
Condition66%2/3
length $hdr == 32 and $hdr =~ /^\Q$objectHeaderGUID\E/
!ll&&!rl&&r
055
MC/DC50%1/2
length $hdr == 32 and $hdr =~ /^\Q$objectHeaderGUID\E/
length $hdr == 32 $hdr =~ /^\Q$objectHeaderGUID\E/
Truth table66%2/3
A and B
A= length $hdr == 32B= $hdr =~ /^\Q$objectHeaderGUID\E/
ABresult
0X0
100
111
104 # this must be null padding or we have a possible error
105 5 ▶ last if $hdr =~ /^\0+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $hdr =~ /^\0+$/ 5 0 5
106 # (could be up to 4095 bytes of non-null garbage plus 4095 null bytes from ExifTool)
107 0 ▶ $raf->Read($buff, 8192) and $hdr .= $buff;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 8192) and $hdr .= $buff 0 0 0
108 0 my $n = length $hdr;
109 0 $hdr =~ s/\0+$//; # remove trailing nulls
110 0 ▶ if ($n > 8190 or length($hdr) > 4095) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 8190 or length $hdr > 4095) 0 0 0
Condition0%0/3
$n > 8190 or length $hdr > 4095
l!l&&r!l&&!r
000
MC/DC0%0/2
$n > 8190 or length $hdr > 4095
$n > 8190 length $hdr > 4095
Truth table0%0/3
A or B
A= $n > 8190B= length $hdr > 4095
ABresult
1X1
011
000
111 0 $err = 'Corrupt file or unsupported InDesign version';
112 0 last;
113 }
114 0 my $non = 'Non-null padding at end of file';
115 0 ▶ if (not $outfile) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $outfile) { } 0 0 0
elsif (not $et->Error($non, 1)) { } 0 0 0
116 0 $et->Warn($non, 1);
117 } elsif (not $et->Error($non, 1)) {
118 0 ▶ Write($outfile, $hdr) or $err = 1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $hdr) or $err = 1 0 0 0
119 0 $writeLen += length $hdr;
120 }
121 0 last;
122 }
123 5 my $len = Get32u(\$hdr, 24);
124 5 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 5 5
125 0 printf $out "Contiguous object at offset 0x%x (%d bytes):\n", $raf->Tell(), $len;
126 0 ▶ if ($verbose > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 2) 0 0 0
127 0 ▶ my $len2 = $len < 1024000 ? $len : 1024000;
Branch: 1 branch
BranchTrueFalseTotal
$len < 1024000 ? : 0 0 0
128 0 ▶ $raf->Seek(-$raf->Read($buff, $len2), 1) or $err = 1;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek(-$raf->Read($buff, $len2), 1) or $err = 1 0 0 0
129 0 $et->VerboseDump(\$buff, Addr => $raf->Tell());
130 }
131 }
132 # check for XMP if stream data is long enough
133 # (56 bytes is just enough for XMP header)
134 5 ▶ if ($len > 56) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 56) { } 5 0 5
135 5 ▶ $raf->Read($buff, 56) == 56 or $err = 'Unexpected end of file', last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 56) == 56 or $err = "Unexpected end of file", last 5 0 5
136 5 ▶ if ($buff =~ /^(....)<\?xpacket begin=(['"])\xef\xbb\xbf\2 id=(['"])W5M0MpCehiHzreSzNTczkc9d\3/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buff =~ /^(....)<\?xpacket begin=(['"])\xef\xbb\xbf\2 id=(['"])W5M0MpCehiHzreSzNTczkc9d\3/s) { } 5 0 5
137 5 my $lenWord = $1; # save length word for writing later
138 5 $len -= 4; # get length of XMP only
139 5 $foundXMP = 1;
140 # I have a sample where the XMP is 107 MB, and ActivePerl may run into
141 # memory troubles (with its apparent 1 GB limit) if the XMP is larger
142 # than about 400 MB, so guard against this
143 5 ▶ if ($len > 300 * 1024 * 1024) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 314572800) 0 5 5
144 0 my $msg = sprintf('Insanely large XMP (%.0f MiB)', $len / (1024 * 1024));
145 0 ▶ if ($outfile) {
Branch: 2 branches
BranchTrueFalseTotal
if ($outfile) { } 0 0 0
elsif ($et->Options('IgnoreMinorErrors')) { } 0 0 0
146 0 ▶ $et->Error($msg, 2) and $err = 1, last;
Branch: 1 branch
BranchTrueFalseTotal
$et->Error($msg, 2) and $err = 1, last 0 0 0
147 } elsif ($et->Options('IgnoreMinorErrors')) {
148 0 $et->Warn($msg);
149 } else {
150 0 $et->Warn("$msg. Ignored.", 1);
151 0 $err = 1;
152 0 last;
153 }
154 }
155 # load and parse the XMP data
156 5 ▶ unless ($raf->Seek(-52, 1) and $raf->Read($buff, $len) == $len) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf->Seek(-52, 1) and $raf->Read($buff, $len) == $len) 0 5 5
Condition33%1/3
$raf->Seek(-52, 1) and $raf->Read($buff, $len) == $len
!ll&&!rl&&r
005
MC/DC0%0/2
$raf->Seek(-52, 1) and $raf->Read($buff, $len) == $len
$raf->Seek(-52, 1) $raf->Read($buff, $len) == $len
Truth table33%1/3
A and B
A= $raf->Seek(-52, 1)B= $raf->Read($buff, $len) == $len
ABresult
0X0
100
111
157 0 $err = 'Error reading XMP stream';
158 0 last;
159 }
160 5 my %dirInfo = (
161 DataPt => \$buff,
162 Parent => 'IND',
163 NoDelete => 1, # do not allow this to be deleted when writing
164 );
165 # validate xmp data length (should be same as length in header - 4)
166 5 my $xmpLen = unpack($streamInt32u, $lenWord);
167 5 ▶ unless ($xmpLen == $len) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xmpLen == $len) 0 5 5
168 0 ▶ if ($xmpLen < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmpLen < $len) { } 0 0 0
169 0 $dirInfo{DirLen} = $xmpLen;
170 } else {
171 0 $err = 'Truncated XMP stream (missing ' . ($xmpLen - $len) . ' bytes)';
172 }
173 }
174 5 my $tagTablePtr = GetTagTable('Image::ExifTool::XMP::Main');
175 5 ▶ if ($outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) { } 2 3 5
176 2 ▶ last if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 2 2
177 # make sure that XMP is writable
178 2 my $classID = Get32u(\$hdr, 20);
179 2 ▶ $classID & 0x40000000 or $err = 'XMP stream is not writable', last;
Branch: 1 branch
BranchTrueFalseTotal
$classID & 1073741824 or $err = "XMP stream is not writable", last 2 0 2
180 2 my $xmp = $et->WriteDirectory(\%dirInfo, $tagTablePtr);
181 2 ▶ if ($xmp and length $xmp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmp and length $xmp) { } 2 0 2
Condition33%1/3
$xmp and length $xmp
!ll&&!rl&&r
002
MC/DC0%0/2
$xmp and length $xmp
$xmp length $xmp
Truth table33%1/3
A and B
A= $xmpB= length $xmp
ABresult
0X0
100
111
182 # write new xmp with leading length word
183 2 $buff = pack($streamInt32u, length $xmp) . $xmp;
184 # update header with new length and invalid checksum
185 2 Set32u(length($buff), \$hdr, 24);
186 2 Set32u(0xffffffff, \$hdr, 28);
187 } else {
188 0 $$et{CHANGED} = 0; # didn't change anything
189 0 ▶ $et->Warn("Can't delete XMP as a block from InDesign file") if defined $xmp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $xmp 0 0 0
190 # put length word back at start of stream
191 0 $buff = $lenWord . $buff;
192 }
193 } else {
194 3 $et->ProcessDirectory(\%dirInfo, $tagTablePtr);
195 }
196 5 $len = 0; # we got the full stream (nothing left to read)
197 } else {
198 0 $len -= 56; # we got 56 bytes of the stream
199 }
200 } else {
201 0 $buff = ''; # must reset this for writing later
202 }
203 5 ▶ if ($outfile) {
Branch: 2 branches
BranchTrueFalseTotal
if ($outfile) { } 2 3 5
elsif ($len) { } 0 3 3
204 # write object header and data
205 2 ▶ Write($outfile, $hdr, $buff) or $err = 1, last;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $hdr, $buff) or $err = 1, last 2 0 2
206 2 my $result = Image::ExifTool::CopyBlock($raf, $outfile, $len);
207 2 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 2 2
208 0 ▶ $err = defined $result ? 'Truncated stream data' : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $result ? : 0 0 0
209 0 last;
210 }
211 2 $writeLen += 32 + length($buff) + $len;
212 } elsif ($len) {
213 # skip over remaining stream data
214 0 ▶ $raf->Seek($len, 1) or $err = 'Seek error', last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($len, 1) or $err = "Seek error", last 0 0 0
215 }
216 5 ▶ $raf->Read($buff, 32) == 32 or $err = 'Unexpected end of file', last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 32) == 32 or $err = "Unexpected end of file", last 5 0 5
217 5 ▶ unless ($buff =~ /^\Q$objectTrailerGUID/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buff =~ /^\Q$objectTrailerGUID\E/) 0 5 5
218 0 $err = 'Invalid object trailer';
219 0 last;
220 }
221 5 ▶ if ($outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) 2 3 5
222 # make sure object UID and ClassID are the same in the trailer
223 2 ▶ substr($hdr,16,8) eq substr($buff,16,8) or $err = 'Non-matching object trailer', last;
Branch: 1 branch
BranchTrueFalseTotal
substr($hdr, 16, 8) eq substr($buff, 16, 8) or $err = "Non-matching object trailer", last 2 0 2
224 # write object trailer
225 2 ▶ Write($outfile, $objectTrailerGUID, substr($hdr,16)) or $err = 1, last;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $objectTrailerGUID, substr($hdr, 16)) or $err = 1, last 2 0 2
226 2 $writeLen += 32;
227 }
228 }
229 5 ▶ if ($outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) 2 3 5
230 # write null padding if necessary
231 # (InDesign files must be an even number of 4096-byte blocks)
232 2 my $part = $writeLen % 4096;
233 2 ▶ Write($outfile, "\0" x (4096 - $part)) or $err = 1 if $part;
Branch: 1 branch
BranchTrueFalseTotal
if $part 2 0 2
Condition50%2/4
$part && (&Write($outfile, scalar "\000" x (4096 - $part)) || ($err = 1))
!ll
02
&Write($outfile, scalar "\0" x (4096 - $part)) or $err = 1
l!l
20
MC/DC0%0/3
$part && (&Write($outfile, scalar "\000" x (4096 - $part)) || ($err = 1))
$part &Write($outfile, scalar "\0" x (4096 - $part)) $err = 1
Truth table0%0/4
A && B or C
A= $partB= &Write($outfile, scalar "\0" x (4096 - $part))C= $err = 1
ABCresult
0XX0
1000
11X1
1011
234 }
235 DONE:
236 5 ▶ if (not $err) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $err) { } 5 0 5
elsif (not $outfile) { } 0 0 0
elsif ($err ne '1') { } 0 0 0
237 5 ▶ $et->Warn('No XMP stream to edit') if $outfile and not $foundXMP;
Branch: 1 branch
BranchTrueFalseTotal
if $outfile and not $foundXMP 0 5 5
Condition66%2/3
$outfile and not $foundXMP
!ll&&!rl&&r
320
MC/DC0%0/2
$outfile and not $foundXMP
$outfile not $foundXMP
Truth table66%2/3
A and not(B)
A= $outfileB= not $foundXMP
ABresult
0X0
100
111
238 5 return 1; # success!
239 } elsif (not $outfile) {
240 # issue warning on read error
241 0 ▶ $et->Warn($err) unless $err eq '1';
Branch: 1 branch
BranchTrueFalseTotal
unless $err eq 1 0 0 0
242 } elsif ($err ne '1') {
243 # set error and return success code
244 0 $et->Error($err);
245 } else {
246 0 return -1; # write error
247 }
248 0 return 1;
249 }
250  
251 1 1; # end
252  
253 __END__
« Image/ExifTool/Matroska.pm ↑ Summary Image/ExifTool/PICT.pm »