LineReader.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package Fpdi
  6. * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. */
  9. namespace Fpdi\PdfParser\CrossReference;
  10. use Fpdi\PdfParser\PdfParser;
  11. use Fpdi\PdfParser\StreamReader;
  12. /**
  13. * Class LineReader
  14. *
  15. * This reader class read all cross-reference entries in a single run.
  16. * It supports reading cross-references with e.g. invalid data (e.g. entries with a length < or > 20 bytes).
  17. */
  18. class LineReader extends AbstractReader implements ReaderInterface
  19. {
  20. /**
  21. * The object offsets.
  22. *
  23. * @var array
  24. */
  25. protected $offsets;
  26. /**
  27. * LineReader constructor.
  28. *
  29. * @param PdfParser $parser
  30. * @throws CrossReferenceException
  31. */
  32. public function __construct(PdfParser $parser)
  33. {
  34. $this->read($this->extract($parser->getStreamReader()));
  35. parent::__construct($parser);
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function getOffsetFor($objectNumber)
  41. {
  42. if (isset($this->offsets[$objectNumber])) {
  43. return $this->offsets[$objectNumber][0];
  44. }
  45. return false;
  46. }
  47. /**
  48. * Get all found offsets.
  49. *
  50. * @return array
  51. */
  52. public function getOffsets()
  53. {
  54. return $this->offsets;
  55. }
  56. /**
  57. * Extracts the cross reference data from the stream reader.
  58. *
  59. * @param StreamReader $reader
  60. * @return string
  61. * @throws CrossReferenceException
  62. */
  63. protected function extract(StreamReader $reader)
  64. {
  65. $bytesPerCycle = 100;
  66. $reader->reset(null, $bytesPerCycle);
  67. $cycles = 0;
  68. do {
  69. // 6 = length of "trailer" - 1
  70. $pos = \max(($bytesPerCycle * $cycles) - 6, 0);
  71. $trailerPos = \strpos($reader->getBuffer(false), 'trailer', $pos);
  72. $cycles++;
  73. } while ($trailerPos === false && $reader->increaseLength($bytesPerCycle) !== false);
  74. if ($trailerPos === false) {
  75. throw new CrossReferenceException(
  76. 'Unexpected end of cross reference. "trailer"-keyword not found.',
  77. CrossReferenceException::NO_TRAILER_FOUND
  78. );
  79. }
  80. $xrefContent = \substr($reader->getBuffer(false), 0, $trailerPos);
  81. $reader->reset($reader->getPosition() + $trailerPos);
  82. return $xrefContent;
  83. }
  84. /**
  85. * Read the cross-reference entries.
  86. *
  87. * @param string $xrefContent
  88. * @throws CrossReferenceException
  89. */
  90. protected function read($xrefContent)
  91. {
  92. // get eol markers in the first 100 bytes
  93. \preg_match_all("/(\r\n|\n|\r)/", \substr($xrefContent, 0, 100), $m);
  94. if (\count($m[0]) === 0) {
  95. throw new CrossReferenceException(
  96. 'No data found in cross-reference.',
  97. CrossReferenceException::INVALID_DATA
  98. );
  99. }
  100. // count(array_count_values()) is faster then count(array_unique())
  101. // @see https://github.com/symfony/symfony/pull/23731
  102. // can be reverted for php7.2
  103. $differentLineEndings = \count(\array_count_values($m[0]));
  104. if ($differentLineEndings > 1) {
  105. $lines = \preg_split("/(\r\n|\n|\r)/", $xrefContent, -1, PREG_SPLIT_NO_EMPTY);
  106. } else {
  107. $lines = \explode($m[0][0], $xrefContent);
  108. }
  109. unset($differentLineEndings, $m);
  110. if (!\is_array($lines)) {
  111. $this->offsets = [];
  112. return;
  113. }
  114. $start = 0;
  115. $offsets = [];
  116. // trim all lines and remove empty lines
  117. $lines = \array_filter(\array_map('\trim', $lines));
  118. foreach ($lines as $line) {
  119. $pieces = \explode(' ', $line);
  120. switch (\count($pieces)) {
  121. case 2:
  122. $start = (int) $pieces[0];
  123. break;
  124. case 3:
  125. switch ($pieces[2]) {
  126. case 'n':
  127. $offsets[$start] = [(int) $pieces[0], (int) $pieces[1]];
  128. $start++;
  129. break 2;
  130. case 'f':
  131. $start++;
  132. break 2;
  133. }
  134. // fall through if pieces doesn't match
  135. default:
  136. throw new CrossReferenceException(
  137. \sprintf('Unexpected data in xref table (%s)', \implode(' ', $pieces)),
  138. CrossReferenceException::INVALID_DATA
  139. );
  140. }
  141. }
  142. $this->offsets = $offsets;
  143. }
  144. }