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

Better octal and hex-entity decode #640

Merged
merged 4 commits into from
Sep 26, 2023
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
35 changes: 16 additions & 19 deletions src/Smalot/PdfParser/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,20 @@ public static function decodeHexadecimal(string $hexa, bool $add_braces = false)
*/
public static function decodeOctal(string $text): string
{
$parts = preg_split('/(?<!\\\\)(\\\\[0-7]{1,3})/s', $text, -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE);
$text = '';
// Replace all double backslashes \\ with a special string
$text = strtr($text, ['\\\\' => '[**pdfparserdblslsh**]']);

foreach ($parts as $part) {
if (preg_match('/^\\\\[0-7]{1,3}$/', $part)) {
$text .= \chr(octdec(trim($part, '\\')));
} else {
$text .= str_replace(['\\\\', '\\(', '\\)'], ['\\', '(', ')'], $part);
}
}
// Now we can replace all octal codes without worrying about
// escaped backslashes
$text = preg_replace_callback('/\\\\([0-7]{1,3})/', function ($m) {
return \chr(octdec($m[1]));
}, $text);

// Unescape any parentheses
$text = str_replace(['\\(', '\\)'], ['(', ')'], $text);

// Replace instances of the special string with a single backslash
$text = str_replace('[**pdfparserdblslsh**]', '\\', $text);

return $text;
GreyWyvern marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -368,16 +372,9 @@ public static function decodeOctal(string $text): string
*/
public static function decodeEntities(string $text): string
{
$parts = preg_split('/(#\d{2})/s', $text, -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE);
$text = '';

foreach ($parts as $part) {
if (preg_match('/^#\d{2}$/', $part)) {
$text .= \chr(hexdec(trim($part, '#')));
} else {
$text .= $part;
}
}
$text = preg_replace_callback('/#([0-9a-f]{2})/i', function ($m) {
return \chr(hexdec($m[1]));
}, $text);

return $text;
GreyWyvern marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPUnit/Integration/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,29 @@ public function testDecodeOctal(): void
$this->assertEquals('AB C', Font::decodeOctal('\\101\\102\\040\\103'));
$this->assertEquals('AB CD', Font::decodeOctal('\\101\\102\\040\\103D'));
$this->assertEquals('AB \199', Font::decodeOctal('\\101\\102\\040\\\\199'));

// Test that series of backslashes of arbitrary length are decoded properly
$this->assertEquals('-', Font::decodeOctal('\\055')); // \055
$this->assertEquals('\\055', Font::decodeOctal('\\\\055')); // \\055
$this->assertEquals('\\-', Font::decodeOctal('\\\\\\055')); // \\\055
$this->assertEquals('\\\\055', Font::decodeOctal('\\\\\\\\055')); // \\\\055
$this->assertEquals('\\\\-', Font::decodeOctal('\\\\\\\\\\055')); // \\\\\055
$this->assertEquals('\\\\\\055', Font::decodeOctal('\\\\\\\\\\\\055')); // \\\\\\055
$this->assertEquals('\\\\\\-', Font::decodeOctal('\\\\\\\\\\\\\\055')); // \\\\\\\055

// Make sure we're unescaping ( and ) before returning the escaped
// backslashes to the string
$this->assertEquals('\\(', Font::decodeOctal('\\\\(')); // \\( - nothing to unescape
$this->assertEquals('\\(', Font::decodeOctal('\\\\\\(')); // \\\( - parenthesis unescaped
$this->assertEquals('\\\\(', Font::decodeOctal('\\\\\\\\(')); // \\\\( - nothing to unescape
$this->assertEquals('\\\\(', Font::decodeOctal('\\\\\\\\\\(')); // \\\\\( - parenthesis unescaped
}

public function testDecodeEntities(): void
{
$this->assertEquals('File Type', Font::decodeEntities('File#20Type'));
$this->assertEquals('File# Ty#pe', Font::decodeEntities('File##20Ty#pe'));
$this->assertEquals('Fi#le#-Ty#p#e ', Font::decodeEntities('Fi#23le##2DTy#p#e '));
}

public function testDecodeUnicode(): void
Expand Down