Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

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