Projet

Général

Profil

Paste
Télécharger (4,57 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / fpdi-version / filters / FilterLZW.php @ c22e192e

1
<?php
2
//
3
//  FPDI - Version 1.4.4
4
//
5
//    Copyright 2004-2013 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
if (!class_exists('FilterLZW', false)) {
21

    
22
    class FilterLZW {
23
        
24
        var $sTable = array();
25
        var $data = null;
26
        var $dataLength = 0;
27
        var $tIdx;
28
        var $bitsToGet = 9;
29
        var $bytePointer;
30
        var $bitPointer;
31
        var $nextData = 0;
32
        var $nextBits = 0;
33
        var $andTable = array(511, 1023, 2047, 4095);
34
    
35
        function error($msg) {
36
            die($msg);
37
        }
38
        
39
        /**
40
         * Method to decode LZW compressed data.
41
         *
42
         * @param string data    The compressed data.
43
         */
44
        function decode($data) {
45
    
46
            if($data[0] == 0x00 && $data[1] == 0x01) {
47
                $this->error('LZW flavour not supported.');
48
            }
49
    
50
            $this->initsTable();
51
    
52
            $this->data = $data;
53
            $this->dataLength = strlen($data);
54
    
55
            // Initialize pointers
56
            $this->bytePointer = 0;
57
            $this->bitPointer = 0;
58
    
59
            $this->nextData = 0;
60
            $this->nextBits = 0;
61
    
62
            $oldCode = 0;
63
    
64
            $string = '';
65
            $uncompData = '';
66
    
67
            while (($code = $this->getNextCode()) != 257) {
68
                if ($code == 256) {
69
                    $this->initsTable();
70
                    $code = $this->getNextCode();
71
    
72
                    if ($code == 257) {
73
                        break;
74
                    }
75
    
76
                    $uncompData .= $this->sTable[$code];
77
                    $oldCode = $code;
78
    
79
                } else {
80
    
81
                    if ($code < $this->tIdx) {
82
                        $string = $this->sTable[$code];
83
                        $uncompData .= $string;
84
    
85
                        $this->addStringToTable($this->sTable[$oldCode], $string[0]);
86
                        $oldCode = $code;
87
                    } else {
88
                        $string = $this->sTable[$oldCode];
89
                        $string = $string . $string[0];
90
                        $uncompData .= $string;
91
    
92
                        $this->addStringToTable($string);
93
                        $oldCode = $code;
94
                    }
95
                }
96
            }
97
            
98
            return $uncompData;
99
        }
100
    
101
    
102
        /**
103
         * Initialize the string table.
104
         */
105
        function initsTable() {
106
            $this->sTable = array();
107
    
108
            for ($i = 0; $i < 256; $i++)
109
                $this->sTable[$i] = chr($i);
110
    
111
            $this->tIdx = 258;
112
            $this->bitsToGet = 9;
113
        }
114
    
115
        /**
116
         * Add a new string to the string table.
117
         */
118
        function addStringToTable ($oldString, $newString='') {
119
            $string = $oldString.$newString;
120
    
121
            // Add this new String to the table
122
            $this->sTable[$this->tIdx++] = $string;
123
    
124
            if ($this->tIdx == 511) {
125
                $this->bitsToGet = 10;
126
            } else if ($this->tIdx == 1023) {
127
                $this->bitsToGet = 11;
128
            } else if ($this->tIdx == 2047) {
129
                $this->bitsToGet = 12;
130
            }
131
        }
132
    
133
        // Returns the next 9, 10, 11 or 12 bits
134
        function getNextCode() {
135
            if ($this->bytePointer == $this->dataLength) {
136
                return 257;
137
            }
138
    
139
            $this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
140
            $this->nextBits += 8;
141
    
142
            if ($this->nextBits < $this->bitsToGet) {
143
                $this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
144
                $this->nextBits += 8;
145
            }
146
    
147
            $code = ($this->nextData >> ($this->nextBits - $this->bitsToGet)) & $this->andTable[$this->bitsToGet-9];
148
            $this->nextBits -= $this->bitsToGet;
149
    
150
            return $code;
151
        }
152
        
153
        function encode($in) {
154
            $this->error("LZW encoding not implemented.");
155
        }
156
    }
157
}