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

gzuncompress function returns Data Error #160

Closed
zumoberhaus opened this issue Jan 9, 2023 · 5 comments
Closed

gzuncompress function returns Data Error #160

zumoberhaus opened this issue Jan 9, 2023 · 5 comments

Comments

@zumoberhaus
Copy link

$data = (($data !== '') ? @\gzuncompress($data) : '');

The gzuncompress function returns sometimes a Data Error as explaned in the User Contributed Notes from
https://www.php.net/manual/en/function.gzuncompress.php

The workaround from the user with the gzuncompress_crc32 function works for me in these cases.

@JanSlabon
Copy link
Member

Can you share the PDF in question? We normally already have a fallback implemented:

$fh = fopen('php://temp', 'w+b');
fwrite($fh, "\x1f\x8b\x08\x00\x00\x00\x00\x00" . $oData);
stream_filter_append($fh, 'zlib.inflate', STREAM_FILTER_READ, ['window' => 30]);
fseek($fh, 0);
$data = @stream_get_contents($fh);
fclose($fh);

@zumoberhaus
Copy link
Author

zumoberhaus commented Jan 9, 2023

With this PDF for example it was not working for me.
https://dev.data-infrasens.ch/uploads/directaccess?token=52ec7d4a34669bb51c58c565acdaa18c5c73e8f3

@JanSlabon
Copy link
Member

The file works without a problem and already the fallback I quoted before is used. I isolated the logic to this and all PHP versions work there, too.

What PHP version are you using? And what is the zlib version that is used? (see phpinfo() output).

JanSlabon added a commit that referenced this issue Jan 10, 2023
Added test case that fails with gzuncompress() but works with the fallback logic (extracted from file in #160).
@zumoberhaus
Copy link
Author

PHP 8.0.26
zlib 1.2.7

I think, I have found a possible problem for me.
You supress the errors with "@" before the gzuncompress and then the fallback will raised.

I have a custom Error Handler implemented, which prints the warning "Warning: gzuncompress(): data error" anyway.
So my script fails, because of this output there.

Think this solves the problem for me.

@JanSlabon
Copy link
Member

Ok, so you simply need to adjust your error handler to take care of the current error level:

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting, so let it fall
        // through to the standard PHP error handler
        return false;
    }
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants