Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check that the previous xref is not the just processed xref #727

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added samples/bugs/Issue727.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/RawData/FilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected function decodeFilterLZWDecode(string $data): string
// convert string to binary string
$bitstring = '';
for ($i = 0; $i < $data_length; ++$i) {
$bitstring .= sprintf('%08b', \ord($data[$i]));
$bitstring .= \sprintf('%08b', \ord($data[$i]));
}
// get the number of bits
$data_length = \strlen($bitstring);
Expand Down
7 changes: 5 additions & 2 deletions src/Smalot/PdfParser/RawData/RawDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,11 @@ protected function decodeXref(string $pdfData, int $startxref, array $xref = [])
}
}
if (preg_match('/Prev[\s]+([0-9]+)/i', $trailer_data, $matches) > 0) {
// get previous xref
$xref = $this->getXrefData($pdfData, (int) $matches[1], $xref);
$offset = (int) $matches[1];
if (0 != $offset) {
// get previous xref
$xref = $this->getXrefData($pdfData, $offset, $xref);
}
}
} else {
throw new \Exception('Unable to find trailer');
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPUnit/Integration/RawData/RawDataParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,24 @@ public function testGetXrefDataIssue673(): void

self::assertStringContainsString('6 rue des Goutais', $text);
}

/**
* Handle self referencing xref
*
* It seems that some pdf creators output `Prev 0` when there is no previous
* xref
tkegan marked this conversation as resolved.
Show resolved Hide resolved
*
* @see https://github.com/smalot/pdfparser/pull/727
*/
public function testDecodeXrefIssue727(): void
{
$filename = $this->rootDir.'/samples/bugs/Issue727.pdf';

// Parsing this document would previously cause an infinite loop
$parser = $this->getParserInstance();
$document = $parser->parseFile($filename);
$text = $document->getText();

self::assertStringContainsString('', $text);
}
}
Loading