Projet

Général

Profil

Paste
Télécharger (2,96 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / fpdi-1.5.4 / filters / FilterASCII85.php @ a1cafe7e

1
<?php
2
//
3
//  FPDI - Version 1.5.4
4
//
5
//    Copyright 2004-2015 Setasign - Jan Slabon
6
//
7
//  Licensed under the Apache License, Version 2.0 (the "License");
8
//  you may not use this file except in compliance with the License.
9
//  You may obtain a copy of the License at
10
//
11
//      http://www.apache.org/licenses/LICENSE-2.0
12
//
13
//  Unless required by applicable law or agreed to in writing, software
14
//  distributed under the License is distributed on an "AS IS" BASIS,
15
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
//  See the License for the specific language governing permissions and
17
//  limitations under the License.
18
//
19

    
20
/**
21
 * Class FilterASCII85
22
 */
23
class FilterASCII85
24
{
25
    /**
26
     * Decode ASCII85 encoded string
27
     *
28
     * @param string $in
29
     * @return string
30
     * @throws Exception
31
     */
32
    public function decode($in)
33
    {
34
        $ord = array(
35
            '~' => ord('~'),
36
            'z' => ord('z'),
37
            'u' => ord('u'),
38
            '!' => ord('!')
39
        );
40

    
41
        $out = '';
42
        $state = 0;
43
        $chn = null;
44

    
45
        $l = strlen($in);
46

    
47
        for ($k = 0; $k < $l; ++$k) {
48
            $ch = ord($in[$k]) & 0xff;
49

    
50
            if ($ch == $ord['~']) {
51
                break;
52
            }
53
            if (preg_match('/^\s$/',chr($ch))) {
54
                continue;
55
            }
56
            if ($ch == $ord['z'] && $state == 0) {
57
                $out .= chr(0) . chr(0) . chr(0) . chr(0);
58
                continue;
59
            }
60
            if ($ch < $ord['!'] || $ch > $ord['u']) {
61
                throw new Exception('Illegal character in ASCII85Decode.');
62
            }
63

    
64
            $chn[$state++] = $ch - $ord['!'];
65

    
66
            if ($state == 5) {
67
                $state = 0;
68
                $r = 0;
69
                for ($j = 0; $j < 5; ++$j)
70
                    $r = $r * 85 + $chn[$j];
71
                $out .= chr($r >> 24);
72
                $out .= chr($r >> 16);
73
                $out .= chr($r >> 8);
74
                $out .= chr($r);
75
            }
76
        }
77
        $r = 0;
78

    
79
        if ($state == 1) {
80
            throw new Exception('Illegal length in ASCII85Decode.');
81
        }
82

    
83
        if ($state == 2) {
84
            $r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1]+1) * 85 * 85 * 85;
85
            $out .= chr($r >> 24);
86

    
87
        } else if ($state == 3) {
88
            $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85  + ($chn[2]+1) * 85 * 85;
89
            $out .= chr($r >> 24);
90
            $out .= chr($r >> 16);
91

    
92
        } else if ($state == 4) {
93
            $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85  + $chn[2] * 85 * 85  + ($chn[3]+1) * 85 ;
94
            $out .= chr($r >> 24);
95
            $out .= chr($r >> 16);
96
            $out .= chr($r >> 8);
97
        }
98

    
99
        return $out;
100
    }
101

    
102
    /**
103
     * NOT IMPLEMENTED
104
     *
105
     * @param string $in
106
     * @return string
107
     * @throws LogicException
108
     */
109
    public function encode($in)
110
    {
111
        throw new LogicException("ASCII85 encoding not implemented.");
112
    }
113
}