Commit 1c7c6dd161856c121416c1acb39239a073713104
1 parent
8bd09112
fix offset calculation bug when writing object streams
git-svn-id: svn+q:///qpdf/trunk@657 71b93d88-0707-0410-a8cf-f5a4172ac649
Showing
1 changed file
with
29 additions
and
9 deletions
qpdf/fix-qdf
| ... | ... | @@ -72,6 +72,7 @@ my $st_done = ++$cur_state; |
| 72 | 72 | |
| 73 | 73 | my @ostream = (); |
| 74 | 74 | my @ostream_offsets = (); |
| 75 | +my @ostream_discarded = (); | |
| 75 | 76 | my $ostream_idx = 0; |
| 76 | 77 | my $ostream_id = 0; |
| 77 | 78 | my $ostream_extends = ""; |
| ... | ... | @@ -130,13 +131,17 @@ while (defined($line = get_line())) |
| 130 | 131 | } |
| 131 | 132 | elsif ($state == $st_in_ostream_dict) |
| 132 | 133 | { |
| 133 | - if ($line =~ m,/Extends (\d+ 0 R),) | |
| 134 | + if ($line =~ m/^stream/) | |
| 134 | 135 | { |
| 135 | - $ostream_extends = $1; | |
| 136 | + $state = $st_in_ostream_offsets; | |
| 136 | 137 | } |
| 137 | - elsif ($line =~ m/^stream/) | |
| 138 | + else | |
| 138 | 139 | { |
| 139 | - $state = $st_in_ostream_offsets; | |
| 140 | + push(@ostream_discarded, $line); | |
| 141 | + if ($line =~ m,/Extends (\d+ 0 R),) | |
| 142 | + { | |
| 143 | + $ostream_extends = $1; | |
| 144 | + } | |
| 140 | 145 | } |
| 141 | 146 | # discard line |
| 142 | 147 | } |
| ... | ... | @@ -149,6 +154,10 @@ while (defined($line = get_line())) |
| 149 | 154 | $state = $st_in_ostream_outer; |
| 150 | 155 | push(@ostream, $line); |
| 151 | 156 | } |
| 157 | + else | |
| 158 | + { | |
| 159 | + push(@ostream_discarded, $line); | |
| 160 | + } | |
| 152 | 161 | # discard line |
| 153 | 162 | } |
| 154 | 163 | elsif ($state == $st_in_ostream_outer) |
| ... | ... | @@ -327,16 +336,20 @@ sub write_ostream |
| 327 | 336 | ++$onum; |
| 328 | 337 | $offsets .= "$onum $_\n"; |
| 329 | 338 | } |
| 339 | + my $offset_adjust = length($offsets); | |
| 330 | 340 | $first += length($offsets); |
| 331 | 341 | $stream_length += length($offsets); |
| 332 | - print " /Length $stream_length\n"; | |
| 333 | - print " /N $n\n"; | |
| 334 | - print " /First $first\n"; | |
| 342 | + my $dict_data = ""; | |
| 343 | + $dict_data .= " /Length $stream_length\n"; | |
| 344 | + $dict_data .= " /N $n\n"; | |
| 345 | + $dict_data .= " /First $first\n"; | |
| 335 | 346 | if ($ostream_extends) |
| 336 | 347 | { |
| 337 | - print " /Extends $ostream_extends\n"; | |
| 348 | + $dict_data .= " /Extends $ostream_extends\n"; | |
| 338 | 349 | } |
| 339 | - print ">>\n"; | |
| 350 | + $dict_data .= ">>\n"; | |
| 351 | + $offset_adjust += length($dict_data); | |
| 352 | + print $dict_data; | |
| 340 | 353 | print "stream\n"; |
| 341 | 354 | print $offsets; |
| 342 | 355 | foreach (@ostream) |
| ... | ... | @@ -344,9 +357,16 @@ sub write_ostream |
| 344 | 357 | print $_; |
| 345 | 358 | } |
| 346 | 359 | |
| 360 | + for (@ostream_discarded) | |
| 361 | + { | |
| 362 | + $offset -= length($_); | |
| 363 | + } | |
| 364 | + $offset += $offset_adjust; | |
| 365 | + | |
| 347 | 366 | $ostream_idx = 0; |
| 348 | 367 | $ostream_id = 0; |
| 349 | 368 | @ostream = (); |
| 350 | 369 | @ostream_offsets = (); |
| 370 | + @ostream_discarded = (); | |
| 351 | 371 | $ostream_extends = ""; |
| 352 | 372 | } | ... | ... |