Skip to content

Commit

Permalink
Fix ASCII85 decoding in a specific case
Browse files Browse the repository at this point in the history
When the start or the end of the string is "<~>". In this case
we must be sure of the exact position of this substring: e.g.
when there is no "<~" header but "<~>" at the end, we must not
remove the first two characters even if "<~" is found somewhere.

Issue: #599
  • Loading branch information
Seb35 committed May 27, 2023
1 parent 9094d77 commit 2503f62
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Smalot/PdfParser/RawData/FilterHelper.php
Expand Up @@ -150,13 +150,13 @@ protected function decodeFilterASCII85Decode(string $data): string
// all white-space characters shall be ignored
$data = preg_replace('/[\s]/', '', $data);
// remove start sequence 2-character sequence <~ (3Ch)(7Eh)
if (false !== strpos($data, '<~')) {
if (0 === strpos($data, '<~')) {
// remove EOD and extra data (if any)
$data = substr($data, 2);
}
// check for EOD: 2-character sequence ~> (7Eh)(3Eh)
$eod = strpos($data, '~>');
if (false !== $eod) {
if (\strlen($data) - 2 === $eod) {
// remove EOD and extra data (if any)
$data = substr($data, 0, $eod);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPUnit/Integration/RawData/FilterHelperTest.php
Expand Up @@ -59,6 +59,30 @@ public function testDecodeFilterASCII85Decode(): void
$this->assertEquals('Compressed string', $result);
}

public function testDecodeFilterASCII85DecodeInitSequence(): void
{
$compressed = '<~6Z6g\Eb0<5ARlp)FE2)5B)'; // = Compressed string
$result = $this->fixture->decodeFilter('ASCII85Decode', $compressed);

$this->assertEquals('Compressed string', $result);
}

public function testDecodeFilterASCII85DecodeEndSequence(): void
{
$compressed = '6Z6g\Eb0<5ARlp)FE2)5B)~>'; // = Compressed string
$result = $this->fixture->decodeFilter('ASCII85Decode', $compressed);

$this->assertEquals('Compressed string', $result);
}

public function testDecodeFilterASCII85DecodeSpecificEndSequence(): void
{
$compressed = '+^6b<~>'; // = 0x215B33C0 = "![3\xC0"
$result = $this->fixture->decodeFilter('ASCII85Decode', $compressed);

$this->assertEquals("\x21\x5B\x33\xC0", $result);
}

/*
* Tests for filter ASCIIHexDecode
*/
Expand Down

0 comments on commit 2503f62

Please sign in to comment.