1 |
60370e04
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
class FilterLZW
|
24 |
|
|
{
|
25 |
|
|
protected $_sTable = array();
|
26 |
|
|
protected $_data = null;
|
27 |
|
|
protected $_dataLength = 0;
|
28 |
|
|
protected $_tIdx;
|
29 |
|
|
protected $_bitsToGet = 9;
|
30 |
|
|
protected $_bytePointer;
|
31 |
|
|
protected $_bitPointer;
|
32 |
|
|
protected $_nextData = 0;
|
33 |
|
|
protected $_nextBits = 0;
|
34 |
|
|
protected $_andTable = array(511, 1023, 2047, 4095);
|
35 |
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
public function decode($data)
|
44 |
|
|
{
|
45 |
|
|
if ($data[0] == 0x00 && $data[1] == 0x01) {
|
46 |
|
|
throw new Exception('LZW flavour not supported.');
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
$this->_initsTable();
|
50 |
|
|
|
51 |
|
|
$this->_data = $data;
|
52 |
|
|
$this->_dataLength = strlen($data);
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
$this->_bytePointer = 0;
|
56 |
|
|
$this->_bitPointer = 0;
|
57 |
|
|
|
58 |
|
|
$this->_nextData = 0;
|
59 |
|
|
$this->_nextBits = 0;
|
60 |
|
|
|
61 |
|
|
$oldCode = 0;
|
62 |
|
|
|
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 |
|
|
if (!isset($this->_sTable[$code])) {
|
75 |
|
|
throw new Exception('Error while decompression LZW compressed data.');
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
$unCompData .= $this->_sTable[$code];
|
79 |
|
|
$oldCode = $code;
|
80 |
|
|
|
81 |
|
|
} else {
|
82 |
|
|
|
83 |
|
|
if ($code < $this->_tIdx) {
|
84 |
|
|
$string = $this->_sTable[$code];
|
85 |
|
|
$unCompData .= $string;
|
86 |
|
|
|
87 |
|
|
$this->_addStringToTable($this->_sTable[$oldCode], $string[0]);
|
88 |
|
|
$oldCode = $code;
|
89 |
|
|
} else {
|
90 |
|
|
$string = $this->_sTable[$oldCode];
|
91 |
|
|
$string = $string . $string[0];
|
92 |
|
|
$unCompData .= $string;
|
93 |
|
|
|
94 |
|
|
$this->_addStringToTable($string);
|
95 |
|
|
$oldCode = $code;
|
96 |
|
|
}
|
97 |
|
|
}
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
return $unCompData;
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
protected function _initsTable()
|
108 |
|
|
{
|
109 |
|
|
$this->_sTable = array();
|
110 |
|
|
|
111 |
|
|
for ($i = 0; $i < 256; $i++)
|
112 |
|
|
$this->_sTable[$i] = chr($i);
|
113 |
|
|
|
114 |
|
|
$this->_tIdx = 258;
|
115 |
|
|
$this->_bitsToGet = 9;
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
protected function _addStringToTable($oldString, $newString = '')
|
122 |
|
|
{
|
123 |
|
|
$string = $oldString . $newString;
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
$this->_sTable[$this->_tIdx++] = $string;
|
127 |
|
|
|
128 |
|
|
if ($this->_tIdx == 511) {
|
129 |
|
|
$this->_bitsToGet = 10;
|
130 |
|
|
} else if ($this->_tIdx == 1023) {
|
131 |
|
|
$this->_bitsToGet = 11;
|
132 |
|
|
} else if ($this->_tIdx == 2047) {
|
133 |
|
|
$this->_bitsToGet = 12;
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
protected function _getNextCode()
|
143 |
|
|
{
|
144 |
|
|
if ($this->_bytePointer == $this->_dataLength) {
|
145 |
|
|
return 257;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
$this->_nextData = ($this->_nextData << 8) | (ord($this->_data[$this->_bytePointer++]) & 0xff);
|
149 |
|
|
$this->_nextBits += 8;
|
150 |
|
|
|
151 |
|
|
if ($this->_nextBits < $this->_bitsToGet) {
|
152 |
|
|
$this->_nextData = ($this->_nextData << 8) | (ord($this->_data[$this->_bytePointer++]) & 0xff);
|
153 |
|
|
$this->_nextBits += 8;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
$code = ($this->_nextData >> ($this->_nextBits - $this->_bitsToGet)) & $this->_andTable[$this->_bitsToGet-9];
|
157 |
|
|
$this->_nextBits -= $this->_bitsToGet;
|
158 |
|
|
|
159 |
|
|
return $code;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
public function encode($in)
|
170 |
|
|
{
|
171 |
|
|
throw new LogicException("LZW encoding not implemented.");
|
172 |
|
|
}
|
173 |
|
|
} |