smalot / pdfparser

PdfParser, a standalone PHP library, provides various tools to extract data from a PDF file.
GNU Lesser General Public License v3.0
2.3k stars 534 forks source link

gzuncompress(): data error #659

Open NickHahac opened 6 months ago

NickHahac commented 6 months ago

I encountered a problem while using it.

image

Attempting to update to the latest version, the issue still exists. How should I block this exception.

https://github.com/smalot/pdfparser/pull/622

k00ni commented 6 months ago

We need a little bit more information. What PHP version do you run? I assume you tried 2.8.0-RC2?

Can you provide the PDF which causes this behavior? If not, try to debug to the point where the error is triggered and give us the parameters of the functions so we can reproduce the error locally.

And last, why did you reference #622?

NickHahac commented 6 months ago

PHP version is 7.4.2

This error appears in Smalot/PdfParser/RawData/FilterHelper. php: 241

Function is:

/**

 * FlateDecode

 *

 * Decompresses data encoded using the zlib/deflate compression method, reproducing the original text or binary data.

 *

 * @param string $data              Data to decode

 * @param int    $decodeMemoryLimit Memory limit on deflation

 *

 * @return string data string

 *

 * @throws \Exception

 */

protected function decodeFilterFlateDecode(string $data, int $decodeMemoryLimit): ?string

{

    // Uncatchable E_WARNING for "data error" is @ suppressed

    // so execution may proceed with an alternate decompression

    // method.

    $decoded = @gzuncompress($data, $decodeMemoryLimit);

    if (false === $decoded) {

        // If gzuncompress() failed, try again using the compress.zlib://

        // wrapper to decode it in a file-based context.

        // See: https://www.php.net/manual/en/function.gzuncompress.php#79042

        // Issue: https://github.com/smalot/pdfparser/issues/592

        $ztmp = tmpfile();

        if (false != $ztmp) {

            fwrite($ztmp, "\x1f\x8b\x08\x00\x00\x00\x00\x00".$data);

            $file = stream_get_meta_data($ztmp)['uri'];

            if (0 === $decodeMemoryLimit) {

                $decoded = file_get_contents('compress.zlib://'.$file);

            } else {

                $decoded = file_get_contents('compress.zlib://'.$file, false, null, 0, $decodeMemoryLimit);

            }

            fclose($ztmp);

        }

    }

    if (false === \is_string($decoded) || '' === $decoded) {

        // If the decoded string is empty, that means decoding failed.

        throw new \Exception('decodeFilterFlateDecode: invalid data');

    }

    return $decoded;

}

params is

$data = "�s��8�S4z�2A�ٮ�������n�O��)q,�ӕ�ik�7l�B:��<Lgz?��C�/�UL�"XZ�@���ui~-�����٥~�&K��"&8_�E����A�f

@.***�Kj��s����!3�Q�<�������#

ŀ>�����3�|�L"

$decodeMemoryLimit = 1000000

At 2023-12-20 16:07:38, "Konrad Abicht" @.***> wrote:

We need a little bit more information. What PHP version do you run? I assume you tried 2.8.0-RC?

Can you provide the PDF which causes this behavior? If not, try to debug to the point where the error is triggered and give us the parameters of the functions so we can reproduce the error locally.

And last, why did you reference #622?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

NickHahac commented 6 months ago

HI, k00ni Hope everything goes well with you.

Is there a way to solve this problem, I hope to receive your help!

NickHahac commented 4 months ago

Can you help me solve my problem?

k00ni commented 4 months ago

Sorry for the late response, I hope I can get back to you the next week.

NickHahac commented 3 months ago

Hi k00ni Thank you for your reply.

If you need to reproduce this error, you can do it like this, $decoded in the code will return false. At the same time, you can see the error "[error] [2] gzuncompress(): data error" in PHP error. log

Please run this code to reproduce the issue

/**

GreyWyvern commented 3 months ago

Might this have something to do with your PHP version? You're using 7.4.2. I'm using 8.3.0 and can't reproduce your error with the code you've given. No errors appear in my PHP error log.

NickHahac commented 3 months ago

If it is a problem with the PHP version, can you fix this issue with compatibility? I hope this error will not be reported in version 7.4.2

k00ni commented 3 months ago

I hope this error will not be reported in version 7.4.2

Can we assume you have to use PHP 7.4.2?