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 ASCII base-85 encoded data
15
 */
16
class Ascii85 implements FilterInterface
17
{
18
    /**
19
     * Decode ASCII85 encoded string.
20
     *
21
     * @param string $data The input string
22
     * @return string
23
     * @throws Ascii85Exception
24
     */
25
    public function decode($data)
26
    {
27
        $out = '';
28
        $state = 0;
29
        $chn = null;
30
 
31
        $data = \preg_replace('/\s/', '', $data);
32
 
33
        $l = \strlen($data);
34
 
35
        /** @noinspection ForeachInvariantsInspection */
36
        for ($k = 0; $k < $l; ++$k) {
37
            $ch = \ord($data[$k]) & 0xff;
38
 
39
            //Start <~
40
            if ($k === 0 && $ch === 60 && isset($data[$k + 1]) && (\ord($data[$k + 1]) & 0xFF) === 126) {
41
                $k++;
42
                continue;
43
            }
44
            //End ~>
45
            if ($ch === 126 && isset($data[$k + 1]) && (\ord($data[$k + 1]) & 0xFF) === 62) {
46
                break;
47
            }
48
 
49
            if ($ch === 122 /* z */ && $state === 0) {
50
                $out .= \chr(0) . \chr(0) . \chr(0) . \chr(0);
51
                continue;
52
            }
53
 
54
            if ($ch < 33 /* ! */ || $ch > 117 /* u */) {
55
                throw new Ascii85Exception(
56
                    'Illegal character found while ASCII85 decode.',
57
                    Ascii85Exception::ILLEGAL_CHAR_FOUND
58
                );
59
            }
60
 
61
            $chn[$state] = $ch - 33;/* ! */
62
            $state++;
63
 
64
            if ($state === 5) {
65
                $state = 0;
66
                $r = 0;
67
                for ($j = 0; $j < 5; ++$j) {
68
                    /** @noinspection UnnecessaryCastingInspection */
69
                    $r = (int)($r * 85 + $chn[$j]);
70
                }
71
 
72
                $out .= \chr($r >> 24)
73
                    . \chr($r >> 16)
74
                    . \chr($r >> 8)
75
                    . \chr($r);
76
            }
77
        }
78
 
79
        if ($state === 1) {
80
            throw new Ascii85Exception(
81
                'Illegal length while ASCII85 decode.',
82
                Ascii85Exception::ILLEGAL_LENGTH
83
            );
84
        }
85
 
86
        if ($state === 2) {
87
            $r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1] + 1) * 85 * 85 * 85;
88
            $out .= \chr($r >> 24);
89
        } elseif ($state === 3) {
90
            $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + ($chn[2] + 1) * 85 * 85;
91
            $out .= \chr($r >> 24);
92
            $out .= \chr($r >> 16);
93
        } elseif ($state === 4) {
94
            $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + $chn[2] * 85 * 85 + ($chn[3] + 1) * 85;
95
            $out .= \chr($r >> 24);
96
            $out .= \chr($r >> 16);
97
            $out .= \chr($r >> 8);
98
        }
99
 
100
        return $out;
101
    }
102
}