Image/ExifTool/WritePDF.pl

filter: Statementstmt 67.7% 284 / 419 Branchbran 52.3% 177 / 338 Conditioncond 56.4% 57 / 101 MC/DCmcdc 35.0% 21 / 60 Subroutinesub 77.7% 7 / 9 Podpod 0.0% 0 / 7 totaltotal 58.4% 546 / 934 CC 216 SCAR 62.2
CC
216
Cov
60%
WritePDF
732.4
CryptObject
132.0
CheckPDF
94.6
CRAP 504.9
« Image/ExifTool/Import.pm ↑ Summary Image/ExifTool/Validate.pm »
1 #------------------------------------------------------------------------------
2 # File: WritePDF.pl
3 #
4 # Description: Write PDF meta information
5 #
6 # Revisions: 12/08/2007 - P. Harvey Created
7 #
8 # References: 1) http://partners.adobe.com/public/developer/pdf/index_reference.html
9 #
10 # Notes: The special "PDF-update" group can be deleted to revert exiftool updates
11 #------------------------------------------------------------------------------
12 package Image::ExifTool::PDF;
13  
14 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 19 ▶use vars qw($lastFetched);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub WriteObject($$);
18 sub EncodeString($);
19 sub CryptObject($);
20  
21 # comments to mark beginning and end of ExifTool incremental update
22 19 my $beginComment = '%BeginExifToolUpdate';
23 19 my $endComment = '%EndExifToolUpdate ';
24  
25 19 my $keyExt; # crypt key extension
26 my $pdfVer; # version of PDF file we are writing (highest Version in Root dictionaries)
27  
28 # internal tags used in dictionary objects
29 19 my %myDictTags = (
30 _tags => 1, _stream => 1, _decrypted => 1, _needCrypt => 1,
31 _filtered => 1, _entry_size => 1, _table => 1,
32 );
33  
34 # map for directories that we can add
35 19 my %pdfMap = (
36 XMP => 'PDF',
37 );
38  
39 #------------------------------------------------------------------------------
40 # Validate raw PDF values for writing (string date integer real boolean name)
41 # Inputs: 0) ExifTool object ref, 1) tagInfo hash ref, 2) raw value ref
42 # Returns: error string or undef (and possibly changes value) on success
43 sub CheckPDF($$$)
44 101 ▶{
Subroutine CheckPDF: called CC=15 SCAR=45.5
Pod undocumented
45 101 my ($et, $tagInfo, $valPtr) = @_;
46 101 ▶ my $format = $$tagInfo{Writable} || $tagInfo->{Table}->{WRITABLE};
Condition66%2/3
$tagInfo->{'Writable'} || $tagInfo->{'Table'}{'WRITABLE'}
l!l&&r!l&&!r
31700
MC/DC0%0/2
$tagInfo->{'Writable'} || $tagInfo->{'Table'}{'WRITABLE'}
$tagInfo->{'Writable'} $tagInfo->{'Table'}{'WRITABLE'}
Truth table66%2/3
A || B
A= $tagInfo->{'Writable'}B= $tagInfo->{'Table'}{'WRITABLE'}
ABresult
1X1
011
000
47 101 ▶ if (not $format) {
Branch: 7 branches
BranchTrueFalseTotal
if (not $format) { } 0 101 101
elsif ($format eq 'string') { } 70 31 101
elsif ($format eq 'date') { } 31 0 31
elsif ($format eq 'integer') { } 0 0 0
elsif ($format eq 'real') { } 0 0 0
elsif ($format eq 'boolean') { } 0 0 0
elsif ($format eq 'name') { } 0 0 0
48 0 return 'No writable format';
49 } elsif ($format eq 'string') {
50 # (encode later because list-type string tags need to be encoded as a unit)
51 } elsif ($format eq 'date') {
52 # be flexible about this for now
53 31 ▶ return 'Bad date format' unless $$valPtr =~ /^\d{4}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $$valPtr =~ /^\d{4}/ 0 31 31
54 } elsif ($format eq 'integer') {
55 0 ▶ return 'Not an integer' unless Image::ExifTool::IsInt($$valPtr);
Branch: 1 branch
BranchTrueFalseTotal
unless &Image::ExifTool::IsInt($$valPtr) 0 0 0
56 } elsif ($format eq 'real') {
57 0 ▶ return 'Not a real number' unless $$valPtr =~ /^[+-]?(?=\d|\.\d)\d*(\.\d*)?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $$valPtr =~ /^[+-]?(?=\d|\.\d)\d*(\.\d*)?$/ 0 0 0
58 } elsif ($format eq 'boolean') {
59 0 ▶ $$valPtr = ($$valPtr and $$valPtr !~ /^f/i) ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$$valPtr && !($$valPtr =~ /^f/i) ? : 0 0 0
Condition0%0/3
$$valPtr and not $$valPtr =~ /^f/i
!ll&&!rl&&r
000
MC/DC0%0/2
$$valPtr and not $$valPtr =~ /^f/i
$$valPtr not $$valPtr =~ /^f/i
Truth table0%0/3
A and not(B)
A= $$valPtrB= not $$valPtr =~ /^f/i
ABresult
0X0
100
111
60 } elsif ($format eq 'name') {
61 0 ▶ return 'Invalid PDF name' if $$valPtr =~ /\0/;
Branch: 1 branch
BranchTrueFalseTotal
if $$valPtr =~ /\0/ 0 0 0
62 } else {
63 0 return "Invalid PDF format '${format}'";
64 }
65 101 return undef; # value is OK
66 }
67  
68 #------------------------------------------------------------------------------
69 # Format value for writing to PDF file
70 # Inputs: 0) ExifTool ref, 1) value, 2) format string (string,date,integer,real,boolean,name)
71 # Returns: formatted value or undef on error
72 # Notes: Called at write time, so $pdfVer may be checked
73 sub WritePDFValue($$$)
74 13 ▶{
Subroutine WritePDFValue: called CC=10 SCAR=34.0
Pod undocumented
75 13 my ($et, $val, $format) = @_;
76 13 ▶ if (not $format) {
Branch: 5 branches
BranchTrueFalseTotal
if (not $format) { } 0 13 13
elsif ($format eq 'string') { } 11 2 13
elsif ($format eq 'date') { } 2 0 2
elsif ($format =~ /^(integer|real|boolean)$/) { } 0 0 0
elsif ($format eq 'name') { } 0 0 0
77 0 return undef;
78 } elsif ($format eq 'string') {
79 # encode as UCS2 if it contains any special characters
80 11 ▶ $val = "\xfe\xff" . $et->Encode($val,'UCS2','MM') if $val =~ /[\x80-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /[\x80-\xff]/ 0 11 11
81 11 EncodeString(\$val);
82 } elsif ($format eq 'date') {
83 # convert date to "D:YYYYmmddHHMMSS+-HH'MM'" format
84 2 $val =~ s/(:\d{2})\.\d*/$1/; # remove sub-seconds
85 2 $val =~ s/([-+]\d{2}):(\d{2})/${1}'${2}'/; # change timezone delimiters if necessary
86 2 $val =~ tr/ ://d; # remove spaces and colons
87 2 $val = "D:$val"; # add leading "D:"
88 2 EncodeString(\$val);
89 } elsif ($format =~ /^(integer|real|boolean)$/) {
90 # no reformatting necessary
91 } elsif ($format eq 'name') {
92 0 ▶ return undef if $val =~ /\0/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /\0/ 0 0 0
93 0 ▶ if ($pdfVer >= 1.2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdfVer >= 1.2) { } 0 0 0
94 0 $val =~ s/([\t\n\f\r ()<>[\]{}\/%#])/sprintf('#%.2x',ord $1)/sge;
95 } else {
96 0 ▶ return undef if $val =~ /[\t\n\f\r ()<>[\]{}\/%]/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ m([\t\n\f\r ()<>[\]{}/%]) 0 0 0
97 }
98 0 $val = "/$val"; # add leading '/'
99 } else {
100 0 return undef;
101 }
102 13 return $val;
103 }
104  
105 #------------------------------------------------------------------------------
106 # Encode PDF string
107 # Inputs: 0) reference to PDF string
108 # Returns: (updates string with encoded data)
109 sub EncodeString($)
110 13 ▶{
Subroutine EncodeString: called CC=7 SCAR=33.0
Pod undocumented
111 13 my $strPt = shift;
112 13 ▶ if (ref $$strPt eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$strPt eq "ARRAY") 0 13 13
113 0 my $str;
114 0 foreach $str (@{$$strPt}) {
115 0 EncodeString(\$str);
116 }
117 0 return;
118 }
119 13 Crypt($strPt, $keyExt, 1); # encrypt if necessary
120 # encode as hex if we have any control characters (except tab)
121 13 ▶ if ($$strPt=~/[\x00-\x08\x0a-\x1f\x7f\xff]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$strPt =~ /[\x00-\x08\x0a-\x1f\x7f\xff]/) { } 0 13 13
122 # encode as hex
123 0 my $str='';
124 0 my $len = length $$strPt;
125 0 my $i = 0;
126 0 for (;;) {
127 0 ▶ my $n = $len - $i or last;
Branch: 1 branch
BranchTrueFalseTotal
my $n = $len - $i or last 0 0 0
128 0 ▶ $n = 40 if $n > 40; # break into reasonable-length lines
Branch: 1 branch
BranchTrueFalseTotal
if $n > 40 0 0 0
129 0 ▶ $str .= $/ if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 0 0 0
130 0 $str .= unpack('H*', substr($$strPt, $i, $n));
131 0 $i += $n;
132 }
133 0 $$strPt = "<$str>";
134 } else {
135 13 $$strPt =~ s/([()\\])/\\$1/g; # must escape round brackets and backslashes
136 13 $$strPt = "($$strPt)";
137 }
138 }
139  
140 #------------------------------------------------------------------------------
141 # Encrypt an object
142 # Inputs: 0) PDF object (encrypts in place)
143 # Notes: Encrypts according to "_needCrypt" dictionary entry,
144 # then deletes "_needCrypt" when done
145 sub CryptObject($)
146 0 ▶{
Subroutine CryptObject: not called CC=11 SCAR=48.8
Pod undocumented
147 0 my $obj = $_[0];
148 0 ▶ if (not ref $obj) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $obj) { } 0 0 0
elsif (ref $obj eq 'HASH') { } 0 0 0
elsif (ref $obj eq 'ARRAY') { } 0 0 0
149 # only literal strings and hex strings are encrypted
150 0 ▶ if ($obj =~ /^[(<]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj =~ /^[(<]/) 0 0 0
151 0 undef $lastFetched; # (reset this just in case)
152 0 my $val = ReadPDFValue($obj);
153 0 EncodeString(\$val);
154 0 $_[0] = $val;
155 }
156 } elsif (ref $obj eq 'HASH') {
157 0 my $tag;
158 0 my $needCrypt = $$obj{_needCrypt};
159 0 foreach $tag (keys %$obj) {
160 0 ▶ next if $myDictTags{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $myDictTags{$tag} 0 0 0
161 # re-encrypt necessary objects only (others are still encrypted)
162 # (this is really annoying, but is necessary because objects stored
163 # in encrypted streams are decrypted when extracting, but strings stored
164 # as direct objects are decrypted later since they must be decoded
165 # before being decrypted)
166 0 ▶ if ($needCrypt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needCrypt) 0 0 0
167 0 ▶ next unless defined $$needCrypt{$tag} ? $$needCrypt{$tag} : $$needCrypt{'*'};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $needCrypt->{$tag} ? $needCrypt->{$tag} : $needCrypt->{"*"} 0 0 0
defined $needCrypt->{$tag} ? : 0 0 0
168 }
169 0 CryptObject($$obj{$tag});
170 }
171 0 delete $$obj{_needCrypt}; # avoid re-re-crypting
172 } elsif (ref $obj eq 'ARRAY') {
173 0 my $val;
174 0 foreach $val (@$obj) {
175 0 CryptObject($val);
176 }
177 }
178 }
179  
180 #------------------------------------------------------------------------------
181 # Get free entries from xref stream dictionary that we wrote previously
182 # Inputs: 0) xref dictionary reference
183 # Returns: free entry hash (keys are object numbers, values are xref entry list refs)
184 sub GetFreeEntries($)
185 0 ▶{
Subroutine GetFreeEntries: not called CC=9 SCAR=45.0
Pod undocumented
186 0 my $dict = shift;
187 0 my %xrefFree;
188 # we write xref stream entries in 'CNn' or 'CNNn' format (with 8-byte 'NN' offset),
189 0 my $w = $$dict{W};
190 0 ▶ if (ref $w eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $w eq "ARRAY") 0 0 0
191 0 my $bytes = "@$w";
192 0 my $fmt;
193 0 ▶ if ($bytes eq '1 4 2') {
Branch: 2 branches
BranchTrueFalseTotal
if ($bytes eq '1 4 2') { } 0 0 0
elsif ($bytes eq '1 8 2') { } 0 0 0
194 0 $fmt = 'CNn';
195 } elsif ($bytes eq '1 8 2') {
196 0 $fmt = 'CNNn';
197 } else {
198 0 return \%xrefFree;
199 }
200 0 my $size = $$dict{_entry_size}; # this will be 7 for 'CNn' or 11 for 'CNNn'
201 0 my $index = $$dict{Index};
202 0 my $len = length $$dict{_stream};
203 # scan the table for free objects
204 0 my $num = scalar(@$index) / 2;
205 0 my $pos = 0;
206 0 my ($i, $j);
207 0 ▶ for ($i=0; $i<$num; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num) 0 0 0
208 0 my $start = $$index[$i*2];
209 0 my $count = $$index[$i*2+1];
210 0 ▶ for ($j=0; $j<$count; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $count) 0 0 0
211 0 ▶ last if $pos + $size > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $size > $len 0 0 0
212 0 my @t = unpack("x$pos $fmt", $$dict{_stream});
213 0 ▶ if (@t == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@t == 4) 0 0 0
214 0 $t[1] = $t[1] * 4294967296 + $t[2];
215 0 $t[2] = $t[3];
216 0 @t = 3;
217 }
218 # add entry if object was free
219 0 ▶ $xrefFree{$start+$j} = [ $t[1], $t[2], 'f' ] if $t[0] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t[0] == 0 0 0 0
220 0 $pos += $size; # step to next entry
221 }
222 }
223 }
224 0 return \%xrefFree;
225 }
226  
227 #------------------------------------------------------------------------------
228 # Write PDF object
229 # Inputs: 0) output file or scalar ref, 1) PDF object
230 # Returns: true on success
231 # Notes: inserts white space before object, but none afterward
232 sub WriteObject($$)
233 208 ▶{
Subroutine WriteObject: called CC=22 SCAR=32.0
Pod undocumented
234 208 my ($outfile, $obj) = @_;
235 208 ▶ if (ref $obj eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $obj eq 'SCALAR') { } 43 165 208
elsif (ref $obj eq 'ARRAY') { } 12 153 165
elsif (ref $obj eq 'HASH') { } 47 106 153
236 43 ▶ Write($outfile, ' ', $$obj) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, " ", $$obj) or return 0 43 0 43
237 } elsif (ref $obj eq 'ARRAY') {
238 # write array
239 12 ▶ Write($outfile, @$obj > 10 ? $/ : ' ', '[') or return 0;
Branch: 2 branches
BranchTrueFalseTotal
&Write($outfile, @$obj > 10 ? $/ : " ", "[") or return 0 12 0 12
@$obj > 10 ? : 0 12 12
240 12 my $item;
241 12 foreach $item (@$obj) {
242 22 ▶ WriteObject($outfile, $item) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&WriteObject($outfile, $item) or return 0 22 0 22
243 }
244 12 ▶ Write($outfile, ' ]') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, " ]") or return 0 12 0 12
245 } elsif (ref $obj eq 'HASH') {
246 # write dictionary
247 47 my $tag;
248 47 ▶ Write($outfile, $/, '<<') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $/, "<<") or return 0 47 0 47
249 # prepare object as required if it has a stream
250 47 ▶ if ($$obj{_stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"_stream"}) 9 38 47
251 # encrypt stream if necessary (must be done before determining Length)
252 9 ▶ CryptStream($obj, $keyExt) if $$obj{_decrypted};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"_decrypted"} 9 0 9
253 # write "Length" entry in dictionary
254 9 $$obj{Length} = length $$obj{_stream};
255 9 push @{$$obj{_tags}}, 'Length';
256 # delete Filter-related entries since we don't yet write filtered streams
257 9 delete $$obj{Filter};
258 9 delete $$obj{DecodeParms};
259 9 delete $$obj{DL};
260 }
261 # don't write my internal entries
262 47 my %wrote = %myDictTags;
263 # write tags in original order, adding new ones later alphabetically
264 47 foreach $tag (@{$$obj{_tags}}, sort keys %$obj) {
265 # ignore already-written or missing entries
266 339 ▶ next if $wrote{$tag} or not defined $$obj{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $wrote{$tag} or not defined $obj->{$tag} 200 139 339
Condition100%3/3
$wrote{$tag} or not defined $obj->{$tag}
l!l&&r!l&&!r
1919139
MC/DC100%2/2
$wrote{$tag} or not defined $obj->{$tag}
$wrote{$tag} not defined $obj->{$tag}
Truth table100%3/3
A or not(B)
A= $wrote{$tag}B= not defined $obj->{$tag}
ABresult
1X1
011
000
267 139 ▶ Write($outfile, $/, "/$tag") or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $/, "/$tag") or return 0 139 0 139
268 139 ▶ WriteObject($outfile, $$obj{$tag}) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&WriteObject($outfile, $obj->{$tag}) or return 0 139 0 139
269 139 $wrote{$tag} = 1;
270 }
271 47 ▶ Write($outfile, $/, '>>') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $/, ">>") or return 0 47 0 47
272 47 ▶ if ($$obj{_stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"_stream"}) 9 38 47
273 # write object stream
274 # (a single 0x0d may not follow 'stream', so use 0x0d+0x0a here to be sure)
275 9 ▶ Write($outfile, $/, "stream\x0d\x0a") or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $/, "stream\r\n") or return 0 9 0 9
276 9 ▶ Write($outfile, $$obj{_stream}, $/, 'endstream') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $obj->{"_stream"}, $/, "endstream") or return 0 9 0 9
277 }
278 } else {
279 # write string, number, name or object reference
280 106 Write($outfile, ' ', $obj);
281 }
282 208 return 1;
283 }
284  
285 #------------------------------------------------------------------------------
286 # Write PDF File
287 # Inputs: 0) ExifTool object reference, 1) dirInfo reference
288 # Returns: 1 on success, 0 if not valid PDF file, -1 on write error
289 # Notes: dictionary structure: Main --+--> Info
290 # +--> Root --> Metadata
291 sub WritePDF($$)
292 19 ▶{
Subroutine WritePDF: called CC=148 SCAR=66.0
Pod undocumented
293 19 my ($et, $dirInfo) = @_;
294 19 my $raf = $$dirInfo{RAF};
295 19 my $outfile = $$dirInfo{OutFile};
296 19 my ($buff, %capture, %newXRef, %newObj, $objRef);
297 19 my ($out, $id, $gen, $obj);
298  
299 # make sure this is a PDF file
300 19 my $pos = $raf->Tell();
301 19 ▶ $raf->Read($buff, 1024) >= 8 or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 1024) >= 8 or return 0 19 0 19
302 19 ▶ $buff =~ /^(\s*)%PDF-(\d+\.\d+)/ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$buff =~ /^(\s*)%PDF-(\d+\.\d+)/ or return 0 19 0 19
303 19 $$et{PDFBase} = length $1;
304 19 $raf->Seek($pos, 0);
305  
306 # create a new ExifTool object and use it to read PDF and XMP information
307 19 my $newTool = Image::ExifTool->new;
308 19 $newTool->Options(List => 1);
309 19 $newTool->Options(Password => $et->Options('Password'));
310 19 $newTool->Options(NoPDFList => $et->Options('NoPDFList'));
311 19 $$newTool{PDF_CAPTURE} = \%capture;
312 19 my $info = $newTool->ImageInfo($raf, 'XMP', 'PDF:*', 'Error', 'Warning');
313 # not a valid PDF file unless we got a version number
314 19 $pdfVer = $$newTool{PDFVersion};
315 19 ▶ $pdfVer or $et->Error('Missing PDF:PDFVersion'), return 0;
Branch: 1 branch
BranchTrueFalseTotal
$pdfVer or $et->Error("Missing PDF:PDFVersion"), (return 0) 19 0 19
316 # check version number
317 19 ▶ if ($pdfVer > 2.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdfVer > 2) 0 19 19
318 0 ▶ $et->Error("Writing PDF $pdfVer is untested", 1) and return 0;
Branch: 1 branch
BranchTrueFalseTotal
$et->Error("Writing PDF $pdfVer is untested", 1) and return 0 0 0 0
319 }
320 # fail if we had any serious errors while extracting information
321 19 ▶ if ($capture{Error} or $$info{Error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($capture{"Error"} or $info->{"Error"}) 0 19 19
Condition33%1/3
$capture{"Error"} or $info->{"Error"}
l!l&&r!l&&!r
0019
MC/DC0%0/2
$capture{"Error"} or $info->{"Error"}
$capture{"Error"} $info->{"Error"}
Truth table33%1/3
A or B
A= $capture{"Error"}B= $info->{"Error"}
ABresult
1X1
011
000
322 0 ▶ $et->Error($capture{Error} || $$info{Error});
Condition0%0/3
$capture{'Error'} || $info->{'Error'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$capture{'Error'} || $info->{'Error'}
$capture{'Error'} $info->{'Error'}
Truth table0%0/3
A || B
A= $capture{'Error'}B= $info->{'Error'}
ABresult
1X1
011
000
323 0 return 1;
324 }
325 # make sure we have everything we need to rewrite this file
326 19 foreach $obj (qw(Main Root xref)) {
327 57 ▶ next if $capture{$obj};
Branch: 1 branch
BranchTrueFalseTotal
if $capture{$obj} 57 0 57
328 # any warning we received may give a clue about why this object is missing
329 0 ▶ $et->Error($$info{Warning}) if $$info{Warning};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"Warning"} 0 0 0
330 0 $et->Error("Can't find $obj object");
331 0 return 1;
332 }
333 19 $et->InitWriteDirs(\%pdfMap, 'XMP');
334  
335 # copy file up to start of previous exiftool update or end of file
336 # (comment, startxref & EOF with 11-digit offsets and 2-byte newlines is 63 bytes)
337 19 ▶ $raf->Seek(-64,2) and $raf->Read($buff,64) and $raf->Seek(0,0) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek(-64, 2) and $raf->Read($buff, 64) and $raf->Seek(0, 0) or return -1 19 0 19
Condition33%2/6
$raf->Seek(-64, 2) and $raf->Read($buff, 64) and $raf->Seek(0, 0)
!ll&&!rl&&r
0019
$raf->Seek(-64, 2) and $raf->Read($buff, 64)
!ll&&!rl&&r
0019
MC/DC0%0/3
$raf->Seek(-64, 2) and $raf->Read($buff, 64) and $raf->Seek(0, 0)
$raf->Seek(-64, 2) $raf->Read($buff, 64) $raf->Seek(0, 0)
Truth table25%1/4
A and B and C
A= $raf->Seek(-64, 2)B= $raf->Read($buff, 64)C= $raf->Seek(0, 0)
ABCresult
0XX0
10X0
1100
1111
338 19 my $rtn = 1;
339 19 my $prevUpdate;
340 # (now $endComment is before "startxref", but pre-7.41 we wrote it after the EOF)
341 19 ▶ if ($buff =~ /$endComment(\d+)\s+(startxref\s+\d+\s+%%EOF\s+)?$/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($buff =~ /$endComment(\d+)\s+(startxref\s+\d+\s+%%EOF\s+)?$/s) { } 16 3 19
elsif ($et->{'DEL_GROUP'}{'PDF-update'}) { } 0 3 3
342 16 $prevUpdate = $1;
343 # rewrite the file up to the original EOF
344 16 ▶ Image::ExifTool::CopyBlock($raf, $outfile, $prevUpdate + $$et{PDFBase}) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Image::ExifTool::CopyBlock($raf, $outfile, $prevUpdate + $et->{"PDFBase"}) or $rtn = -1 16 0 16
345 # verify that we are now at the start of an ExifTool update
346 16 ▶ unless ($raf->Read($buff, length $beginComment) and $buff eq $beginComment) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf->Read($buff, length $beginComment) and $buff eq $beginComment) 0 16 16
Condition33%1/3
$raf->Read($buff, length $beginComment) and $buff eq $beginComment
!ll&&!rl&&r
0016
MC/DC0%0/2
$raf->Read($buff, length $beginComment) and $buff eq $beginComment
$raf->Read($buff, length $beginComment) $buff eq $beginComment
Truth table33%1/3
A and B
A= $raf->Read($buff, length $beginComment)B= $buff eq $beginComment
ABresult
0X0
100
111
347 0 $et->Error('Previous ExifTool update is corrupted');
348 0 return $rtn;
349 }
350 16 ▶ $raf->Seek($prevUpdate+$$et{PDFBase}, 0) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($prevUpdate + $et->{"PDFBase"}, 0) or $rtn = -1 16 0 16
351 16 ▶ if ($$et{DEL_GROUP}{'PDF-update'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->{"DEL_GROUP"}{"PDF-update"}) 2 14 16
352 2 $et->VPrint(0, " Reverted previous ExifTool updates\n");
353 2 ++$$et{CHANGED};
354 2 return $rtn;
355 }
356 } elsif ($$et{DEL_GROUP}{'PDF-update'}) {
357 0 $et->Error('File contains no previous ExifTool update');
358 0 return $rtn;
359 } else {
360 # rewrite the whole file
361 3 ▶ while ($raf->Read($buff, 65536)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raf->Read($buff, 65536)) 3 3 6
362 3 ▶ Write($outfile, $buff) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $buff) or $rtn = -1 3 0 3
363 }
364 }
365 17 ▶ $out = $et->Options('TextOut') if $et->Options('Verbose');
Branch: 1 branch
BranchTrueFalseTotal
if $et->Options("Verbose") 0 17 17
366 #
367 # create our new PDF objects to write
368 #
369 17 my $xref = $capture{xref};
370 17 my $mainDict = $capture{Main};
371 17 my $metaRef = $capture{Root}->{Metadata};
372 17 my $nextObject;
373  
374 # start by finding reference for info object in case it was deleted
375 # in a previous edit so we can re-use it here if adding PDF Info
376 my $prevInfoRef;
377 17 ▶ if ($prevUpdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prevUpdate) { } 14 3 17
378 14 ▶ unless ($capture{Prev}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($capture{"Prev"}) 0 14 14
379 0 $et->Error("Can't locate trailer dictionary prior to last edit");
380 0 return $rtn;
381 }
382 14 $prevInfoRef = $capture{Prev}->{Info};
383 # start from previous size so the xref table doesn't continue
384 # to grow if we repeatedly add and delete the Metadata object
385 14 $nextObject = $capture{Prev}->{Size};
386 # don't re-use Meta reference if object was added in a previous update
387 14 ▶ undef $metaRef if $metaRef and $$metaRef=~/^(\d+)/ and $1 >= $nextObject;
Branch: 1 branch
BranchTrueFalseTotal
if $metaRef and $$metaRef =~ /^(\d+)/ and $1 >= $nextObject 5 9 14
Condition83%5/6
$metaRef and $$metaRef =~ /^(\d+)/ and $1 >= $nextObject
!ll&&!rl&&r
545
$metaRef and $$metaRef =~ /^(\d+)/
!ll&&!rl&&r
509
MC/DC66%2/3
$metaRef and $$metaRef =~ /^(\d+)/ and $1 >= $nextObject
$metaRef $$metaRef =~ /^(\d+)/ $1 >= $nextObject
Truth table75%3/4
A and B and C
A= $metaRefB= $$metaRef =~ /^(\d+)/C= $1 >= $nextObject
ABCresult
0XX0
10X0
1100
1111
388 } else {
389 3 $prevInfoRef = $$mainDict{Info};
390 3 $nextObject = $$mainDict{Size};
391 }
392  
393 # delete entire PDF group if specified
394 17 my $infoChanged = 0;
395 17 ▶ if ($$et{DEL_GROUP}{PDF} and $capture{Info}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->{"DEL_GROUP"}{"PDF"} and $capture{"Info"}) 4 13 17
Condition100%3/3
$et->{"DEL_GROUP"}{"PDF"} and $capture{"Info"}
!ll&&!rl&&r
1214
MC/DC100%2/2
$et->{"DEL_GROUP"}{"PDF"} and $capture{"Info"}
$et->{"DEL_GROUP"}{"PDF"} $capture{"Info"}
Truth table100%3/3
A and B
A= $et->{"DEL_GROUP"}{"PDF"}B= $capture{"Info"}
ABresult
0X0
100
111
396 4 delete $capture{Info};
397 4 $info = { XMP => $$info{XMP} }; # remove extracted PDF tags
398 4 ▶ print $out " Deleting PDF Info dictionary\n" if $out;
Branch: 1 branch
BranchTrueFalseTotal
if $out 0 4 4
399 4 ++$infoChanged;
400 }
401  
402 # create new Info dictionary if necessary
403 17 ▶ $capture{Info} = { _tags => [ ] } unless $capture{Info};
Branch: 1 branch
BranchTrueFalseTotal
unless $capture{"Info"} 10 7 17
404 17 my $infoDict = $capture{Info};
405  
406 # must pre-determine Info reference to be used in encryption
407 17 ▶ my $infoRef = $prevInfoRef || \ "$nextObject 0 R";
Condition100%2/2
$prevInfoRef || \("$nextObject 0 R")
l!l
107
MC/DC100%1/1
$prevInfoRef || \("$nextObject 0 R")
$prevInfoRef
Truth table100%2/2
A
A= $prevInfoRef
Aresult
11
00
408 17 ▶ unless (ref $infoRef eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $infoRef eq "SCALAR") 0 17 17
409 0 $et->Error("Info dictionary is not an indirect object");
410 0 return $rtn;
411 }
412 17 $keyExt = $$infoRef;
413  
414 # must encrypt all values in dictionary if they came from an encrypted stream
415 17 ▶ CryptObject($infoDict) if $$infoDict{_needCrypt};
Branch: 1 branch
BranchTrueFalseTotal
if $infoDict->{"_needCrypt"} 0 17 17
416  
417 # must set line separator before calling WritePDFValue()
418 17 local $/ = $capture{newline};
419  
420 # rewrite PDF Info tags
421 17 my $newTags = $et->GetNewTagInfoHash(\%Image::ExifTool::PDF::Info);
422 17 my $tagID;
423 17 foreach $tagID (sort keys %$newTags) {
424 11 my $tagInfo = $$newTags{$tagID};
425 11 ▶ if ($pdfVer >= 2.0 and not $$tagInfo{PDF2}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdfVer >= 2 and not $tagInfo->{"PDF2"}) 0 11 11
Condition33%1/3
$pdfVer >= 2 and not $tagInfo->{"PDF2"}
!ll&&!rl&&r
1100
MC/DC0%0/2
$pdfVer >= 2 and not $tagInfo->{"PDF2"}
$pdfVer >= 2 not $tagInfo->{"PDF2"}
Truth table33%1/3
A and not(B)
A= $pdfVer >= 2B= not $tagInfo->{"PDF2"}
ABresult
0X0
100
111
426 0 ▶ next if $et->Warn("Writing PDF:$$tagInfo{Name} is deprecated for PDF 2.0 documents",2);
Branch: 1 branch
BranchTrueFalseTotal
if $et->Warn(qq[Writing PDF:$tagInfo->{"Name"} is deprecated for PDF 2.0 documents], 2) 0 0 0
427 }
428 11 my $nvHash = $et->GetNewValueHash($tagInfo);
429 11 my (@vals, $deleted);
430 11 my $tag = $$tagInfo{Name};
431 11 my $val = $$info{$tag};
432 11 my $tagKey = $tag;
433 11 ▶ unless (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 9 2 11
434 # must check for tag key with copy number
435 9 ($tagKey) = grep /^$tag/, keys %$info;
436 9 ▶ $val = $$info{$tagKey} if $tagKey;
Branch: 1 branch
BranchTrueFalseTotal
if $tagKey 5 4 9
437 }
438 11 ▶ if (defined $val) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val) { } 7 4 11
elsif ($nvHash->{'EditOnly'}) { } 0 4 4
439 7 my @oldVals;
440 7 ▶ if (ref $val eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 3 4 7
441 3 @oldVals = @$val;
442 3 $val = shift @oldVals;
443 }
444 7 for (;;) {
445 11 ▶ if ($et->IsOverwriting($nvHash, $val) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->IsOverwriting($nvHash, $val) > 0) { } 5 6 11
446 5 $deleted = 1;
447 5 $et->VerboseValue("- PDF:$tag", $val);
448 5 ++$infoChanged;
449 } else {
450 6 push @vals, $val;
451 }
452 11 ▶ last unless @oldVals;
Branch: 1 branch
BranchTrueFalseTotal
unless @oldVals 7 4 11
453 4 $val = shift @oldVals;
454 }
455 # don't write this out if we deleted all values
456 7 ▶ delete $$infoDict{$tagID} unless @vals;
Branch: 1 branch
BranchTrueFalseTotal
unless @vals 3 4 7
457 } elsif ($$nvHash{EditOnly}) {
458 0 next;
459 }
460 # decide whether we want to write this tag
461 # (native PDF information is always preferred, so don't check IsCreating)
462 11 ▶ next unless $deleted or $$tagInfo{List} or not exists $$infoDict{$tagID};
Branch: 1 branch
BranchTrueFalseTotal
unless $deleted or $tagInfo->{"List"} or not exists $infoDict->{$tagID} 0 11 11
Condition83%5/6
$deleted or $tagInfo->{"List"} or not exists $infoDict->{$tagID}
l!l&&r!l&&!r
920
$deleted or $tagInfo->{"List"}
l!l&&r!l&&!r
542
MC/DC0%0/3
$deleted or $tagInfo->{"List"} or not exists $infoDict->{$tagID}
$deleted $tagInfo->{"List"} not exists $infoDict->{$tagID}
Truth table75%3/4
A or B or not(C)
A= $deletedB= $tagInfo->{"List"}C= not exists $infoDict->{$tagID}
ABCresult
1XX1
01X1
0011
0000
463  
464 # add new values to existing ones
465 11 my @newVals = $et->GetNewValue($nvHash);
466 11 ▶ if (@newVals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@newVals) 9 2 11
467 9 push @vals, @newVals;
468 9 ++$infoChanged;
469 9 ▶ if ($out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out) 0 9 9
470 0 foreach $val (@newVals) {
471 0 $et->VerboseValue("+ PDF:$tag", $val);
472 }
473 }
474 }
475 11 ▶ unless (@vals) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@vals) 0 11 11
476 # remove this entry from the Info dictionary if no values remain
477 0 delete $$infoDict{$tagID};
478 0 next;
479 }
480 # format value(s) for writing to PDF file
481 11 ▶ my $writable = $$tagInfo{Writable} || $Image::ExifTool::PDF::Info{WRITABLE};
Condition66%2/3
$tagInfo->{'Writable'} || $Image::ExifTool::PDF::Info{'WRITABLE'}
l!l&&r!l&&!r
290
MC/DC0%0/2
$tagInfo->{'Writable'} || $Image::ExifTool::PDF::Info{'WRITABLE'}
$tagInfo->{'Writable'} $Image::ExifTool::PDF::Info{'WRITABLE'}
Truth table66%2/3
A || B
A= $tagInfo->{'Writable'}B= $Image::ExifTool::PDF::Info{'WRITABLE'}
ABresult
1X1
011
000
482 11 ▶ if (not $$tagInfo{List}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $tagInfo->{'List'}) { } 5 6 11
elsif ($tagInfo->{'List'} eq 'array') { } 3 3 6
483 5 $val = WritePDFValue($et, shift(@vals), $writable);
484 } elsif ($$tagInfo{List} eq 'array') {
485 3 foreach $val (@vals) {
486 5 $val = WritePDFValue($et, $val, $writable);
487 5 ▶ defined $val or undef(@vals), last;
Branch: 1 branch
BranchTrueFalseTotal
defined $val or undef @vals, last 5 0 5
488 }
489 3 ▶ $val = @vals ? \@vals : undef;
Branch: 1 branch
BranchTrueFalseTotal
@vals ? : 3 0 3
490 } else {
491 3 $val = WritePDFValue($et, join($et->Options('ListSep'), @vals), $writable);
492 }
493 11 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 11 0 11
494 11 $$infoDict{$tagID} = $val;
495 11 ++$infoChanged;
496 } else {
497 0 $et->Warn("Error converting $$tagInfo{Name} value");
498 }
499 }
500 17 ▶ if ($infoChanged) {
Branch: 2 branches
BranchTrueFalseTotal
if ($infoChanged) { } 10 7 17
elsif ($prevUpdate) { } 6 1 7
501 10 $$et{CHANGED} += $infoChanged;
502 } elsif ($prevUpdate) {
503 # must still write Info dictionary if it was previously updated
504 6 my $oldPos = LocateObject($xref, $$infoRef);
505 6 ▶ $infoChanged = 1 if $oldPos and $oldPos > $prevUpdate;
Branch: 1 branch
BranchTrueFalseTotal
if $oldPos and $oldPos > $prevUpdate 4 2 6
Condition66%2/3
$oldPos and $oldPos > $prevUpdate
!ll&&!rl&&r
204
MC/DC50%1/2
$oldPos and $oldPos > $prevUpdate
$oldPos $oldPos > $prevUpdate
Truth table66%2/3
A and B
A= $oldPosB= $oldPos > $prevUpdate
ABresult
0X0
100
111
506 }
507  
508 # create new Info dictionary if necessary
509 17 ▶ if ($infoChanged) {
Branch: 1 branch
BranchTrueFalseTotal
if ($infoChanged) 14 3 17
510 # increment object count if we used a new object here
511 14 ▶ if (scalar(keys %{$capture{Info}}) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$capture{'Info'};} > 1) { } 10 4 14
512 10 $newObj{$$infoRef} = $capture{Info};# save to write later
513 10 $$mainDict{Info} = $infoRef; # add reference to trailer dictionary
514 10 ▶ ++$nextObject unless $prevInfoRef;
Branch: 1 branch
BranchTrueFalseTotal
unless $prevInfoRef 3 7 10
515 } else {
516 # remove Info from Main (trailer) dictionary
517 4 delete $$mainDict{Info};
518 # write free entry in xref table if Info existed prior to all edits
519 4 ▶ $newObj{$$infoRef} = '' if $prevInfoRef;
Branch: 1 branch
BranchTrueFalseTotal
if $prevInfoRef 2 2 4
520 }
521 }
522  
523 # rewrite XMP
524 my %xmpInfo = (
525 DataPt => $$info{XMP},
526 17 Parent => 'PDF',
527 );
528 17 my $xmpTable = Image::ExifTool::GetTagTable('Image::ExifTool::XMP::Main');
529 17 my $oldChanged = $$et{CHANGED};
530 17 my $newXMP = $et->WriteDirectory(\%xmpInfo, $xmpTable);
531 17 ▶ $newXMP = $$info{XMP} ? ${$$info{XMP}} : '' unless defined $newXMP;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $newXMP 6 11 17
$info->{'XMP'} ? : 4 2 6
532  
533 # WriteDirectory() will increment CHANGED erroneously if non-existent
534 # XMP is deleted as a block -- so check for this
535 17 ▶ unless ($newXMP or $$info{XMP}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newXMP or $info->{"XMP"}) 3 14 17
Condition100%3/3
$newXMP or $info->{"XMP"}
l!l&&r!l&&!r
953
MC/DC100%2/2
$newXMP or $info->{"XMP"}
$newXMP $info->{"XMP"}
Truth table100%3/3
A or B
A= $newXMPB= $info->{"XMP"}
ABresult
1X1
011
000
536 3 $$et{CHANGED} = $oldChanged;
537 3 $et->VPrint(0, " (XMP not changed -- still empty)\n");
538 }
539 17 my ($metaChanged, $rootChanged);
540  
541 17 ▶ if ($$et{CHANGED} != $oldChanged and defined $newXMP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($et->{'CHANGED'} != $oldChanged and defined $newXMP) { } 10 7 17
elsif ($prevUpdate and $capture{'Root'}{'Metadata'}) { } 4 3 7
Condition66%2/3
$et->{"CHANGED"} != $oldChanged and defined $newXMP
!ll&&!rl&&r
7010
MC/DC50%1/2
$et->{"CHANGED"} != $oldChanged and defined $newXMP
$et->{"CHANGED"} != $oldChanged defined $newXMP
Truth table66%2/3
A and B
A= $et->{"CHANGED"} != $oldChangedB= defined $newXMP
ABresult
0X0
100
111
542 10 $metaChanged = 1;
543 ▶ } elsif ($prevUpdate and $capture{Root}->{Metadata}) {
Condition66%2/3
$prevUpdate and $capture{"Root"}{"Metadata"}
!ll&&!rl&&r
034
MC/DC50%1/2
$prevUpdate and $capture{"Root"}{"Metadata"}
$prevUpdate $capture{"Root"}{"Metadata"}
Truth table66%2/3
A and B
A= $prevUpdateB= $capture{"Root"}{"Metadata"}
ABresult
0X0
100
111
544 # must still write Metadata dictionary if it was previously updated
545 4 my $oldPos = LocateObject($xref, ${$capture{Root}->{Metadata}});
546 4 ▶ $metaChanged = 1 if $oldPos and $oldPos > $prevUpdate;
Branch: 1 branch
BranchTrueFalseTotal
if $oldPos and $oldPos > $prevUpdate 4 0 4
Condition33%1/3
$oldPos and $oldPos > $prevUpdate
!ll&&!rl&&r
004
MC/DC0%0/2
$oldPos and $oldPos > $prevUpdate
$oldPos $oldPos > $prevUpdate
Truth table33%1/3
A and B
A= $oldPosB= $oldPos > $prevUpdate
ABresult
0X0
100
111
547 }
548 17 ▶ if ($metaChanged) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metaChanged) 14 3 17
549 14 ▶ if ($newXMP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($newXMP) { } 9 5 14
elsif ($capture{'Root'}{'Metadata'}) { } 5 0 5
550 9 ▶ unless (ref $metaRef) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $metaRef) 5 4 9
551 # allocate new PDF object
552 5 $metaRef = \ "$nextObject 0 R";
553 5 ++$nextObject;
554 5 $capture{Root}->{Metadata} = $metaRef;
555 5 $rootChanged = 1; # set flag to replace Root dictionary
556 }
557 # create the new metadata dictionary to write later
558 9 $newObj{$$metaRef} = {
559 Type => '/Metadata',
560 Subtype => '/XML',
561 # Length => length $newXMP, (set by WriteObject)
562 _tags => [ qw(Type Subtype) ],
563 _stream => $newXMP,
564 _decrypted => 1, # (this will be ignored if EncryptMetadata is false)
565 };
566 } elsif ($capture{Root}->{Metadata}) {
567 # free existing metadata object
568 5 $newObj{${$capture{Root}->{Metadata}}} = '';
569 5 delete $capture{Root}->{Metadata};
570 5 $rootChanged = 1; # set flag to replace Root dictionary
571 }
572 }
573 # add new Root dictionary if necessary
574 17 my $rootRef = $$mainDict{Root};
575 17 ▶ unless ($rootRef) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rootRef) 0 17 17
576 0 $et->Error("Can't find Root dictionary");
577 0 return $rtn;
578 }
579 17 ▶ if (not $rootChanged and $prevUpdate) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rootChanged and $prevUpdate) 6 11 17
Condition100%3/3
not $rootChanged and $prevUpdate
!ll&&!rl&&r
1016
MC/DC100%2/2
not $rootChanged and $prevUpdate
not $rootChanged $prevUpdate
Truth table100%3/3
not(A) and B
A= not $rootChangedB= $prevUpdate
ABresult
0X0
100
111
580 # must still write Root dictionary if it was previously updated
581 6 my $oldPos = LocateObject($xref, $$rootRef);
582 6 ▶ $rootChanged = 1 if $oldPos and $oldPos > $prevUpdate;
Branch: 1 branch
BranchTrueFalseTotal
if $oldPos and $oldPos > $prevUpdate 3 3 6
Condition66%2/3
$oldPos and $oldPos > $prevUpdate
!ll&&!rl&&r
033
MC/DC50%1/2
$oldPos and $oldPos > $prevUpdate
$oldPos $oldPos > $prevUpdate
Truth table66%2/3
A and B
A= $oldPosB= $oldPos > $prevUpdate
ABresult
0X0
100
111
583 }
584 17 ▶ $newObj{$$rootRef} = $capture{Root} if $rootChanged;
Branch: 1 branch
BranchTrueFalseTotal
if $rootChanged 13 4 17
585 #
586 # write incremental update if anything was changed
587 #
588 17 ▶ if ($$et{CHANGED}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($et->{'CHANGED'}) { } 16 1 17
elsif ($prevUpdate) { } 1 0 1
589 # remember position of original EOF
590 16 my $oldEOF = Tell($outfile) - $$et{PDFBase};
591 16 ▶ Write($outfile, $beginComment) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $beginComment) or $rtn = -1 16 0 16
592  
593 # write new objects
594 16 foreach $objRef (sort keys %newObj) {
595 38 ▶ $objRef =~ /^(\d+) (\d+)/ or $rtn = -1, last;
Branch: 1 branch
BranchTrueFalseTotal
$objRef =~ /^(\d+) (\d+)/ or $rtn = -1, last 38 0 38
596 38 ($id, $gen) = ($1, $2);
597 38 ▶ if (not $newObj{$objRef}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newObj{$objRef}) 7 31 38
598 7 ▶ ++$gen if $gen < 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $gen < 65535 7 0 7
599 # write free entry in xref table
600 7 $newXRef{$id} = [ 0, $gen, 'f' ];
601 7 next;
602 }
603 # create new entry for xref table
604 31 $newXRef{$id} = [ Tell($outfile) - $$et{PDFBase} + length($/), $gen, 'n' ];
605 31 $keyExt = "$id $gen obj"; # (must set for stream encryption)
606 31 ▶ Write($outfile, $/, $keyExt) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $/, $keyExt) or $rtn = -1 31 0 31
607 31 ▶ WriteObject($outfile, $newObj{$objRef}) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&WriteObject($outfile, $newObj{$objRef}) or $rtn = -1 31 0 31
608 31 ▶ Write($outfile, $/, 'endobj') or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $/, "endobj") or $rtn = -1 31 0 31
609 }
610  
611 # Prev points to old xref table
612 16 ▶ $$mainDict{Prev} = $capture{startxref} unless $prevUpdate;
Branch: 1 branch
BranchTrueFalseTotal
unless $prevUpdate 3 13 16
613  
614 # add xref entry for head of free-object list
615 16 $newXRef{0} = [ 0, 65535, 'f' ];
616  
617 # must insert free xref entries from previous exiftool update if applicable
618 16 ▶ if ($prevUpdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prevUpdate) 13 3 16
619 13 my $mainFree;
620 # extract free entries from our previous Main xref stream
621 13 ▶ if ($$mainDict{Type} and $$mainDict{Type} eq '/XRef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mainDict->{'Type'} and $mainDict->{'Type'} eq '/XRef') { } 0 13 13
Condition33%1/3
$mainDict->{"Type"} and $mainDict->{"Type"} eq "/XRef"
!ll&&!rl&&r
1300
MC/DC0%0/2
$mainDict->{"Type"} and $mainDict->{"Type"} eq "/XRef"
$mainDict->{"Type"} $mainDict->{"Type"} eq "/XRef"
Truth table33%1/3
A and B
A= $mainDict->{"Type"}B= $mainDict->{"Type"} eq "/XRef"
ABresult
0X0
100
111
622 0 $mainFree = GetFreeEntries($xref->{dicts}->[0]);
623 } else {
624 # free entries from Main xref table already captured for us
625 13 $mainFree = $capture{mainFree};
626 }
627 13 foreach $id (sort { $a <=> $b } keys %$mainFree) {
628 19 ▶ $newXRef{$id} = $$mainFree{$id} unless $newXRef{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $newXRef{$id} 5 14 19
629 }
630 }
631  
632 # connect linked list of free object in our xref table
633 16 my $prevFree = 0;
634 16 foreach $id (sort { $b <=> $a } keys %newXRef) { # (reverse sort)
635 59 ▶ next unless $newXRef{$id}->[2] eq 'f'; # skip if not free
Branch: 1 branch
BranchTrueFalseTotal
unless $newXRef{$id}[2] eq "f" 31 28 59
636 # no need to add free entry for objects added by us
637 # in previous edits then freed again
638 28 ▶ if ($id >= $nextObject) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id >= $nextObject) 3 25 28
639 3 delete $newXRef{$id}; # Note: deleting newXRef entry!
640 3 next;
641 }
642 25 $newXRef{$id}->[0] = $prevFree;
643 25 $prevFree = $id;
644 }
645  
646 # prepare our main dictionary for writing
647 16 $$mainDict{Size} = $nextObject; # update number of objects
648 # must change the ID if it exists
649 16 ▶ if (ref $$mainDict{ID} eq 'ARRAY' and @{$$mainDict{ID}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mainDict->{"ID"} eq "ARRAY" and @{$mainDict->{"ID"};} > 1) 6 10 16
Condition66%2/3
ref $mainDict->{"ID"} eq "ARRAY" and @{$mainDict->{"ID"};} > 1
!ll&&!rl&&r
1006
MC/DC50%1/2
ref $mainDict->{"ID"} eq "ARRAY" and @{$mainDict->{"ID"};} > 1
ref $mainDict->{"ID"} eq "ARRAY" @{$mainDict->{"ID"};} > 1
Truth table66%2/3
A and B
A= ref $mainDict->{"ID"} eq "ARRAY"B= @{$mainDict->{"ID"};} > 1
ABresult
0X0
100
111
650 # increment first byte since this is an easy change to make
651 6 $id = $mainDict->{ID}->[1];
652 6 ▶ if ($id =~ /^<([0-9a-f]{2})/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($id =~ /^<([0-9a-f]{2})/i) { } 6 0 6
elsif ($id =~ /^\((.)/s and $1 ne '\\' and $1 ne ')' and $1 ne '(') { } 0 0 0
653 6 my $byte = unpack('H2',chr((hex($1) + 1) & 0xff));
654 6 substr($id, 1, 2) = $byte;
655 ▶ } elsif ($id =~ /^\((.)/s and $1 ne '\\' and $1 ne ')' and $1 ne '(') {
Condition0%0/9
$id =~ /^\((.)/s and $1 ne "\\" and $1 ne ")" and $1 ne "("
!ll&&!rl&&r
000
$id =~ /^\((.)/s and $1 ne "\\" and $1 ne ")"
!ll&&!rl&&r
000
$id =~ /^\((.)/s and $1 ne "\\"
!ll&&!rl&&r
000
MC/DC0%0/4
$id =~ /^\((.)/s and $1 ne "\\" and $1 ne ")" and $1 ne "("
$id =~ /^\((.)/s $1 ne "\\" $1 ne ")" $1 ne "("
Truth table0%0/5
A and B and C and D
A= $id =~ /^\((.)/sB= $1 ne "\\"C= $1 ne ")"D= $1 ne "("
ABCDresult
0XXX0
10XX0
110X0
11100
11111
656 0 my $ch = chr((ord($1) + 1) & 0xff);
657 # avoid generating characters that could cause problems
658 0 ▶ $ch = 'a' if $ch =~ /[()\\\x00-\x08\x0a-\x1f\x7f\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
if $ch =~ /[()\\\x00-\x08\x0a-\x1f\x7f\xff]/ 0 0 0
659 0 substr($id, 1, 1) = $ch;
660 }
661 6 $mainDict->{ID}->[1] = $id;
662 }
663  
664 # remember position of xref table in file (we will write this next)
665 16 my $startxref = Tell($outfile) - $$et{PDFBase} + length($/);
666  
667 # must write xref as a stream in xref-stream-only files
668 16 ▶ if ($$mainDict{Type} and $$mainDict{Type} eq '/XRef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mainDict->{'Type'} and $mainDict->{'Type'} eq '/XRef') { } 0 16 16
Condition33%1/3
$mainDict->{"Type"} and $mainDict->{"Type"} eq "/XRef"
!ll&&!rl&&r
1600
MC/DC0%0/2
$mainDict->{"Type"} and $mainDict->{"Type"} eq "/XRef"
$mainDict->{"Type"} $mainDict->{"Type"} eq "/XRef"
Truth table33%1/3
A and B
A= $mainDict->{"Type"}B= $mainDict->{"Type"} eq "/XRef"
ABresult
0X0
100
111
669  
670 # create entry for the xref stream object itself
671 0 $newXRef{$nextObject++} = [ Tell($outfile) - $$et{PDFBase} + length($/), 0, 'n' ];
672 0 $$mainDict{Size} = $nextObject;
673 # create xref stream and Index entry
674 0 my $bits = 4;
675 0 Restart: for (;;) {
676 0 $$mainDict{W} = [ 1, $bits, 2 ]; # int8u, int32u/int64u, int16u ('CNn' or 'CNNn')
677 0 $$mainDict{Index} = [ ];
678 0 $$mainDict{_stream} = '';
679 0 my @ids = sort { $a <=> $b } keys %newXRef;
680 0 ▶ while (@ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ids) 0 0 0
681 0 my $startID = $ids[0];
682 0 for (;;) {
683 0 $id = shift @ids;
684 0 my ($pos, $gen, $type) = @{$newXRef{$id}};
685 0 ▶ if ($pos > 0xffffffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > 4294967295) 0 0 0
686 0 ▶ if ($bits == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits == 4) 0 0 0
687 # switch to 64-bit integer offsets
688 0 $bits = 8;
689 0 next Restart;
690 }
691 }
692 0 ▶ if ($bits == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits == 4) { } 0 0 0
693 0 ▶ $$mainDict{_stream} .= pack('CNn', $type eq 'f' ? 0 : 1, $pos, $gen);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'f' ? : 0 0 0
694 } else {
695 0 my $hi = int($pos / 4294967296);
696 0 my $lo = $pos - $hi * 4294967296;
697 0 ▶ $$mainDict{_stream} .= pack('CNNn', $type eq 'f' ? 0 : 1, $hi, $lo, $gen);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'f' ? : 0 0 0
698 }
699 0 ▶ last if not @ids or $ids[0] != $id + 1;
Branch: 1 branch
BranchTrueFalseTotal
if not @ids or $ids[0] != $id + 1 0 0 0
Condition0%0/3
not @ids or $ids[0] != $id + 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not @ids or $ids[0] != $id + 1
not @ids $ids[0] != $id + 1
Truth table0%0/3
not(A) or B
A= not @idsB= $ids[0] != $id + 1
ABresult
1X1
011
000
700 }
701 # add Index entries for this section of the xref stream
702 0 push @{$$mainDict{Index}}, $startID, $id - $startID + 1;
703 }
704 0 last;
705 }
706 # write the xref stream object
707 0 $keyExt = "$id 0 obj"; # (set anyway, but xref stream should NOT be encrypted)
708 0 ▶ Write($outfile, $/, $keyExt) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $/, $keyExt) or $rtn = -1 0 0 0
709 0 ▶ WriteObject($outfile, $mainDict) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&WriteObject($outfile, $mainDict) or $rtn = -1 0 0 0
710 0 ▶ Write($outfile, $/, 'endobj') or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $/, "endobj") or $rtn = -1 0 0 0
711  
712 } else {
713  
714 # write new xref table
715 16 ▶ Write($outfile, $/, 'xref', $/) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $/, "xref", $/) or $rtn = -1 16 0 16
716 # lines must be exactly 20 bytes, so pad newline if necessary
717 16 ▶ my $endl = (length($/) == 1 ? ' ' : '') . $/;
Branch: 1 branch
BranchTrueFalseTotal
length $/ == 1 ? : 16 0 16
718 16 my @ids = sort { $a <=> $b } keys %newXRef;
719 16 ▶ while (@ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ids) 34 16 50
720 34 my $startID = $ids[0];
721 34 $buff = '';
722 34 for (;;) {
723 56 $id = shift @ids;
724 56 $buff .= sprintf("%.10d %.5d %s%s", @{$newXRef{$id}}, $endl);
725 56 ▶ last if not @ids or $ids[0] != $id + 1;
Branch: 1 branch
BranchTrueFalseTotal
if not @ids or $ids[0] != $id + 1 34 22 56
Condition100%3/3
not @ids or $ids[0] != $id + 1
l!l&&r!l&&!r
161822
MC/DC100%2/2
not @ids or $ids[0] != $id + 1
not @ids $ids[0] != $id + 1
Truth table100%3/3
not(A) or B
A= not @idsB= $ids[0] != $id + 1
ABresult
1X1
011
000
726 }
727 # write this (contiguous-numbered object) section of the xref table
728 34 ▶ Write($outfile, $startID, ' ', $id - $startID + 1, $/, $buff) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $startID, " ", $id - $startID + 1, $/, $buff) or $rtn = -1 34 0 34
729 }
730  
731 # write main (trailer) dictionary
732 16 ▶ Write($outfile, 'trailer') or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, "trailer") or $rtn = -1 16 0 16
733 16 ▶ WriteObject($outfile, $mainDict) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&WriteObject($outfile, $mainDict) or $rtn = -1 16 0 16
734 }
735 # write trailing comment (marker to allow edits to be reverted)
736 16 ▶ Write($outfile, $/, $endComment, $oldEOF, $/) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $/, $endComment, $oldEOF, $/) or $rtn = -1 16 0 16
737  
738 # write pointer to main xref table and EOF marker
739 16 ▶ Write($outfile, 'startxref', $/, $startxref, $/, '%%EOF', $/) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, "startxref", $/, $startxref, $/, "%%EOF", $/) or $rtn = -1 16 0 16
740  
741 } elsif ($prevUpdate) {
742  
743 # nothing new changed, so copy over previous incremental update
744 1 ▶ $raf->Seek($prevUpdate+$$et{PDFBase}, 0) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($prevUpdate + $et->{"PDFBase"}, 0) or $rtn = -1 1 0 1
745 1 ▶ while ($raf->Read($buff, 65536)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raf->Read($buff, 65536)) 1 1 2
746 1 ▶ Write($outfile, $buff) or $rtn = -1;
Branch: 1 branch
BranchTrueFalseTotal
&Write($outfile, $buff) or $rtn = -1 1 0 1
747 }
748 }
749 17 ▶ if ($rtn > 0 and $$et{CHANGED} and ($$et{DEL_GROUP}{PDF} or $$et{DEL_GROUP}{XMP})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rtn > 0 and $et->{"CHANGED"} and $et->{"DEL_GROUP"}{"PDF"} || $et->{"DEL_GROUP"}{"XMP"}) 6 11 17
Condition88%8/9
$rtn > 0 and $et->{"CHANGED"} and $et->{"DEL_GROUP"}{"PDF"} || $et->{"DEL_GROUP"}{"XMP"}
!ll&&!rl&&r
1106
$rtn > 0 and $et->{"CHANGED"}
!ll&&!rl&&r
0116
$et->{"DEL_GROUP"}{"PDF"} or $et->{"DEL_GROUP"}{"XMP"}
l!l&&r!l&&!r
4210
MC/DC75%3/4
$rtn > 0 and $et->{"CHANGED"} and $et->{"DEL_GROUP"}{"PDF"} || $et->{"DEL_GROUP"}{"XMP"}
$rtn > 0 $et->{"CHANGED"} $et->{"DEL_GROUP"}{"PDF"} $et->{"DEL_GROUP"}{"XMP"}
Truth table80%4/5
A and B and C or D
A= $rtn > 0B= $et->{"CHANGED"}C= $et->{"DEL_GROUP"}{"PDF"}D= $et->{"DEL_GROUP"}{"XMP"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
750 6 $et->Warn('ExifTool PDF edits are reversible. Deleted tags may be recovered!', 1);
751 }
752 17 undef $newTool;
753 17 undef %capture;
754 17 return $rtn;
755 }
756  
757  
758 19 1; # end
759  
760 __END__
« Image/ExifTool/Import.pm ↑ Summary Image/ExifTool/Validate.pm »