Skip to content

Commit

Permalink
Fix hexadecimal decoding (issue smalot#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyc committed May 27, 2024
1 parent 4b86c66 commit a604278
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
Binary file added samples/bugs/Issue715.pdf
Binary file not shown.
7 changes: 3 additions & 4 deletions src/Smalot/PdfParser/Element/ElementHexa.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ public static function parse(string $content, ?Document $document = null, int &$
if (preg_match('/^\s*\<(?P<name>[A-F0-9]+)\>/is', $content, $match)) {
$name = $match['name'];
$offset += strpos($content, '<'.$name) + \strlen($name) + 2; // 1 for '>'
// repackage string as standard
$name = '('.self::decode($name).')';
$element = ElementDate::parse($name, $document);
$name = self::decode($name);
$element = ElementDate::parse('('.$name.')', $document);

if (!$element) {
$element = ElementString::parse($name, $document);
$element = new self($name);
}

return $element;
Expand Down
2 changes: 1 addition & 1 deletion src/Smalot/PdfParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ protected function parseHeaderElement(?string $type, $value, ?Document $document
return ElementString::parse('('.$value.')', $document);

case '<':
return $this->parseHeaderElement('(', ElementHexa::decode($value), $document);
return ElementHexa::parse('<'.$value.'>', $document);

case '/':
return ElementName::parse('/'.$value, $document);
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPUnit/Integration/Element/ElementHexaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,9 @@ public function testParse(): void
007000610 073007100750061002c0020> ');

$this->assertEquals('pasqua, primavera, resurrezione, festa cristiana, gesù, uova di cioccolata, coniglietti, pulcini, pasquale, campane, dina rebucci, uova di pasqua, ', $element);

$testString = '()\\';
$element = ElementHexa::parse('<'.bin2hex($testString).'>', null, $offset);
$this->assertEquals($testString, (string) $element);
}
}
14 changes: 14 additions & 0 deletions tests/PHPUnit/Integration/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,20 @@ public function testIgnoreEncryption(): void

// without the configuration option set, an exception would be thrown.
}

/**
* Tests special chars encoded as hex.
*/
public function testSpecialCharsEncodedAsHex(): void
{
$filename = $this->rootDir.'/samples/bugs/Issue715.pdf';

$this->fixture = new Parser();
$document = $this->fixture->parseFile($filename);
$result = (string) ($document->getObjectsByType('Sig')['4_0'] ?? null)?->getHeader()->get('Contents');

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

class ParserSub extends Parser
Expand Down

0 comments on commit a604278

Please sign in to comment.