Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| Repair | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| scan | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Pdf\Extract; |
| 16 | |
| 17 | /** |
| 18 | * Pdf extract repair class |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop\Pdf |
| 22 | * @author Nick Sagona, III <nick@popphp.org> |
| 23 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 6.0.0 |
| 26 | */ |
| 27 | class Repair |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Scan raw PDF data for object offsets and a trailer, bypassing xref entirely |
| 32 | * |
| 33 | * @param string $data |
| 34 | * @return array |
| 35 | */ |
| 36 | public static function scan(string $data): array |
| 37 | { |
| 38 | $offsets = []; |
| 39 | $matches = []; |
| 40 | |
| 41 | preg_match_all('/(?<![0-9])(\d+)[ \t\r\n]+(\d+)[ \t\r\n]+obj\b/', $data, $matches, PREG_OFFSET_CAPTURE); |
| 42 | |
| 43 | foreach ($matches[1] as $i => $objMatch) { |
| 44 | $objNum = (int) $objMatch[0]; |
| 45 | $offset = $matches[0][$i][1]; |
| 46 | $offsets[$objNum] = ['offset' => $offset]; |
| 47 | } |
| 48 | |
| 49 | $trailer = []; |
| 50 | $trailerPos = strrpos($data, 'trailer'); |
| 51 | |
| 52 | if ($trailerPos !== false) { |
| 53 | try { |
| 54 | $tokenizer = new Tokenizer($data, $trailerPos + strlen('trailer')); |
| 55 | $parser = new ObjectParser($tokenizer); |
| 56 | $candidate = $parser->parseValue(); |
| 57 | if (is_array($candidate)) { |
| 58 | $trailer = $candidate; |
| 59 | } |
| 60 | } catch (Exception $e) { |
| 61 | // Malformed/truncated trailer dict - degrade to an empty |
| 62 | // trailer rather than losing the offsets already scanned. |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return ['offsets' => $offsets, 'trailer' => $trailer]; |
| 67 | } |
| 68 | |
| 69 | } |