-
Notifications
You must be signed in to change notification settings - Fork 538
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 hexadecimal decoding (fixes #715) #716
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a604278
Fix hexadecimal decoding (issue #715)
krzyc edaa7f4
More round bracket related tests
krzyc 8e6d3b7
Fix PHP 7.x compatibility
krzyc 4ba5b8d
Rewrite ElementString parser
krzyc e738b8e
mostly refinements and some code refactoring (see below)
k00ni 53d5ccd
ElementStringTest: added further tests, ...
k00ni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,51 @@ public function equals($value): bool | |
return $value == $this->value; | ||
} | ||
|
||
/** | ||
* Part of parsing process to handle escaped characters. | ||
* Note, most parameters are passed by reference. | ||
* | ||
* Further information in PDF specification (page 53): | ||
* https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/pdfreference1.7old.pdf | ||
*/ | ||
private static function handleEscapedCharacters(string &$name, int &$position, string &$processedName, string $char): void | ||
{ | ||
// escaped chars | ||
$nextChar = substr($name, 0, 1); | ||
switch ($nextChar) { | ||
// end-of-line markers (CR, LF, CRLF) should be ignored | ||
case "\r": | ||
case "\n": | ||
preg_match('/^\\r?\\n?/', $name, $matches); | ||
$name = substr($name, \strlen($matches[0])); | ||
$position += \strlen($matches[0]); | ||
break; | ||
// process LF, CR, HT, BS, FF | ||
case 'n': | ||
case 't': | ||
case 'r': | ||
case 'b': | ||
case 'f': | ||
$processedName .= stripcslashes('\\'.$nextChar); | ||
$name = substr($name, 1); | ||
++$position; | ||
break; | ||
// decode escaped parentheses and backslash | ||
case '(': | ||
case ')': | ||
case '\\': | ||
case ' ': // TODO: this should probably be removed - kept for compatibility | ||
$processedName .= $nextChar; | ||
$name = substr($name, 1); | ||
++$position; | ||
break; | ||
Comment on lines
+87
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean with "kept for compatibility"? To what? |
||
// TODO: process octal encoding (but it is also processed later) | ||
// keep backslash in other cases | ||
default: | ||
$processedName .= $char; | ||
} | ||
} | ||
|
||
/** | ||
* @return bool|ElementString | ||
*/ | ||
|
@@ -59,25 +104,38 @@ public static function parse(string $content, ?Document $document = null, int &$ | |
if (preg_match('/^\s*\((?P<name>.*)/s', $content, $match)) { | ||
$name = $match['name']; | ||
|
||
// Find next ')' not escaped. | ||
$cur_start_text = $start_search_end = 0; | ||
while (false !== ($cur_start_pos = strpos($name, ')', $start_search_end))) { | ||
$cur_extract = substr($name, $cur_start_text, $cur_start_pos - $cur_start_text); | ||
preg_match('/(?P<escape>[\\\]*)$/s', $cur_extract, $match); | ||
if (!(\strlen($match['escape']) % 2)) { | ||
break; | ||
$delimiterCount = 0; | ||
$position = 0; | ||
$processedName = ''; | ||
do { | ||
$char = substr($name, 0, 1); | ||
$name = substr($name, 1); | ||
++$position; | ||
switch ($char) { | ||
// matched delimiters should be treated as part of string | ||
case '(': | ||
$processedName .= $char; | ||
++$delimiterCount; | ||
break; | ||
case ')': | ||
if (0 === $delimiterCount) { | ||
$name = substr($name, 1); | ||
break 2; | ||
} | ||
$processedName .= $char; | ||
--$delimiterCount; | ||
break; | ||
case '\\': | ||
self::handleEscapedCharacters($name, $position, $processedName, $char); | ||
break; | ||
default: | ||
$processedName .= $char; | ||
} | ||
$start_search_end = $cur_start_pos + 1; | ||
} | ||
} while ('' !== $name); | ||
|
||
$offset += strpos($content, '(') + 1 + $position; | ||
|
||
// Extract string. | ||
$name = substr($name, 0, (int) $cur_start_pos); | ||
$offset += strpos($content, '(') + $cur_start_pos + 2; // 2 for '(' and ')' | ||
$name = str_replace( | ||
['\\\\', '\\ ', '\\/', '\(', '\)', '\n', '\r', '\t'], | ||
['\\', ' ', '/', '(', ')', "\n", "\r", "\t"], | ||
$name | ||
); | ||
$name = $processedName; | ||
|
||
// Decode string. | ||
$name = Font::decodeOctal($name); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please give me a short summary what you try to accomplish here.
Why removing the reference to
$document
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also why switching from
ElementString
toElementHexa
? Is it to match others existing behavior from other elements classes?