Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
/**
4
 * This file is part of FPDI
5
 *
6
 * @package   setasign\Fpdi
7
 * @copyright Copyright (c) 2023 Setasign GmbH & Co. KG (https://www.setasign.com)
8
 * @license   http://opensource.org/licenses/mit-license The MIT License
9
 */
10
 
11
namespace setasign\Fpdi\PdfParser\Filter;
12
 
13
/**
14
 * Class for handling zlib/deflate encoded data
15
 */
16
class Flate implements FilterInterface
17
{
18
    /**
19
     * Checks whether the zlib extension is loaded.
20
     *
21
     * Used for testing purpose.
22
     *
23
     * @return boolean
24
     * @internal
25
     */
26
    protected function extensionLoaded()
27
    {
28
        return \extension_loaded('zlib');
29
    }
30
 
31
    /**
32
     * Decodes a flate compressed string.
33
     *
34
     * @param string|false $data The input string
35
     * @return string
36
     * @throws FlateException
37
     */
38
    public function decode($data)
39
    {
40
        if ($this->extensionLoaded()) {
41
            $oData = $data;
42
            $data = (($data !== '') ? @\gzuncompress($data) : '');
43
            if ($data === false) {
44
                // let's try if the checksum is CRC32
45
                $fh = fopen('php://temp', 'w+b');
46
                fwrite($fh, "\x1f\x8b\x08\x00\x00\x00\x00\x00" . $oData);
47
                // "window" == 31 -> 16 + (8 to 15): Uses the low 4 bits of the value as the window size logarithm.
48
                //                   The input must include a gzip header and trailer (via 16).
49
                stream_filter_append($fh, 'zlib.inflate', STREAM_FILTER_READ, ['window' => 31]);
50
                fseek($fh, 0);
51
                $data = @stream_get_contents($fh);
52
                fclose($fh);
53
 
54
                if ($data) {
55
                    return $data;
56
                }
57
 
58
                // Try this fallback (remove the zlib stream header)
59
                $data = @(gzinflate(substr($oData, 2)));
60
 
61
                if ($data === false) {
62
                    throw new FlateException(
63
                        'Error while decompressing stream.',
64
                        FlateException::DECOMPRESS_ERROR
65
                    );
66
                }
67
            }
68
        } else {
69
            throw new FlateException(
70
                'To handle FlateDecode filter, enable zlib support in PHP.',
71
                FlateException::NO_ZLIB
72
            );
73
        }
74
 
75
        return $data;
76
    }
77
}