123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace Fpdi\PdfParser\CrossReference;
- use Fpdi\PdfParser\PdfParser;
- use Fpdi\PdfParser\StreamReader;
- class FixedReader extends AbstractReader implements ReaderInterface
- {
-
- protected $reader;
-
- protected $subSections;
-
- public function __construct(PdfParser $parser)
- {
- $this->reader = $parser->getStreamReader();
- $this->read();
- parent::__construct($parser);
- }
-
- public function getSubSections()
- {
- return $this->subSections;
- }
-
- public function getOffsetFor($objectNumber)
- {
- foreach ($this->subSections as $offset => list($startObject, $objectCount)) {
-
- if ($objectNumber >= $startObject && $objectNumber < ($startObject + $objectCount)) {
- $position = $offset + 20 * ($objectNumber - $startObject);
- $this->reader->ensure($position, 20);
- $line = $this->reader->readBytes(20);
- if ($line[17] === 'f') {
- return false;
- }
- return (int) \substr($line, 0, 10);
- }
- }
- return false;
- }
-
- protected function read()
- {
- $subSections = [];
- $startObject = $entryCount = $lastLineStart = null;
- $validityChecked = false;
- while (($line = $this->reader->readLine(20)) !== false) {
- if (\strpos($line, 'trailer') !== false) {
- $this->reader->reset($lastLineStart);
- break;
- }
-
- if (\sscanf($line, '%d %d', $startObject, $entryCount) !== 2) {
- continue;
- }
- $oldPosition = $this->reader->getPosition();
- $position = $oldPosition + $this->reader->getOffset();
- if (!$validityChecked && $entryCount > 0) {
- $nextLine = $this->reader->readBytes(21);
-
- if (\strlen(\trim($nextLine)) !== 21) {
- throw new CrossReferenceException(
- 'Cross-reference entries are larger than 20 bytes.',
- CrossReferenceException::ENTRIES_TOO_LARGE
- );
- }
-
- if (\strlen(\trim(\substr($nextLine, 0, 20))) !== 18) {
- throw new CrossReferenceException(
- 'Cross-reference entries are less than 20 bytes.',
- CrossReferenceException::ENTRIES_TOO_SHORT
- );
- }
- $validityChecked = true;
- }
- $subSections[$position] = [$startObject, $entryCount];
- $lastLineStart = $position + $entryCount * 20;
- $this->reader->reset($lastLineStart);
- }
-
- $this->reader->reset($lastLineStart);
- if (\count($subSections) === 0) {
- throw new CrossReferenceException(
- 'No entries found in cross-reference.',
- CrossReferenceException::NO_ENTRIES
- );
- }
- $this->subSections = $subSections;
- }
-
- public function fixFaultySubSectionShift()
- {
- $subSections = $this->getSubSections();
- if (\count($subSections) > 1) {
- return false;
- }
- $subSection = \current($subSections);
- if ($subSection[0] != 1) {
- return false;
- }
- if ($this->getOffsetFor(1) === false) {
- foreach ($subSections as $offset => list($startObject, $objectCount)) {
- $this->subSections[$offset] = [$startObject - 1, $objectCount];
- }
- return true;
- }
- return false;
- }
- }
|