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

Filter ElementHexa::decode() of non-hex chars #687

Merged
merged 3 commits into from
Mar 12, 2024

Conversation

GreyWyvern
Copy link
Contributor

@GreyWyvern GreyWyvern commented Mar 6, 2024

Type of pull request

  • Bug fix (involves code and configuration changes)

About

Add a preg_replace() to ElementHexa::decode() so incoming strings are filtered of all non hexadecimal characters. Resolves #683.

Also remove check for the BOM (feff) if it exists. The existing function does a check for characters '00' at the beginning of the string to decide whether to 4-byte or 2-byte decode this string. It does not account for the 4-byte BOM and decodes such a string in a 2-byte fashion. It depends on further functions (in this case Parser::parseHeaderElement()) to repair the incorrectly decoded contents. Removing Checking for the BOM allows ElementHexa::decode() to return the correctly decoded contents the first time.

Checklist for code / configuration changes

In case you changed the code/configuration, please read each of the following checkboxes as they contain valuable information:

  • Please add at least one test case (unit test, system test, ...) to demonstrate that the change is working. If existing code was changed, your tests cover these code parts as well.
  • Please run PHP-CS-Fixer before committing, to confirm with our coding styles. See https://github.com/smalot/pdfparser/blob/master/.php-cs-fixer.php for more information about our coding styles.
  • In case you fix an existing issue, please do one of the following:
    • Write in this text something like fixes #1234 to outline that you are providing a fix for the issue #1234.
    • After the pull request was created, you will find on the right side a section called Development. There issues can be selected which will be closed after the your pull request got merged.

Add a `preg_replace()` to `ElementHexa::decode()` so incoming strings are filtered of all non hexadecimal characters.

Also remove the BOM (`feff`) if it exists. The function does a check for characters '00' at the beginning of the string to decide whether to 4-byte or 2-byte decode this string. It does not account for the 4-byte BOM and decodes such a string in a 2-byte fashion. It depends on further functions (in this case `Parser::parseHeaderElement()`) to repair the incorrectly decoded contents. Checking for and removing the BOM allows `ElementHexa::decode()` to return the correctly decoded contents the first time.
Instead of just deleting/ignoring it, separate out the BE BOM `feff` as an additional check for 4-byte hexadecimal content.
Copy link
Collaborator

@k00ni k00ni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @GreyWyvern, looks good to me. Just a small remark.

src/Smalot/PdfParser/Element/ElementHexa.php Outdated Show resolved Hide resolved
@k00ni k00ni merged commit ca3fea6 into smalot:master Mar 12, 2024
29 checks passed
@GreyWyvern GreyWyvern deleted the dirty-hex-string branch May 10, 2024 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird UTF-8 Characters when parsing hex string (keywords)
2 participants