3 // FPDI - Version 1.5.4
5 // Copyright 2004-2015 Setasign - Jan Slabon
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
25 protected $_sTable = array();
26 protected $_data = null;
27 protected $_dataLength = 0;
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);
37 * Decodes LZW compressed data.
39 * @param string $data The compressed data.
43 public function decode($data)
45 if ($data[0] == 0x00 && $data[1] == 0x01) {
46 throw new Exception('LZW flavour not supported.');
52 $this->_dataLength = strlen($data);
54 // Initialize pointers
55 $this->_bytePointer = 0;
56 $this->_bitPointer = 0;
65 while (($code = $this->_getNextCode()) != 257) {
68 $code = $this->_getNextCode();
74 if (!isset($this->_sTable[$code])) {
75 throw new Exception('Error while decompression LZW compressed data.');
78 $unCompData .= $this->_sTable[$code];
83 if ($code < $this->_tIdx) {
84 $string = $this->_sTable[$code];
85 $unCompData .= $string;
87 $this->_addStringToTable($this->_sTable[$oldCode], $string[0]);
90 $string = $this->_sTable[$oldCode];
91 $string = $string . $string[0];
92 $unCompData .= $string;
94 $this->_addStringToTable($string);
105 * Initialize the string table.
107 protected function _initsTable()
109 $this->_sTable = array();
111 for ($i = 0; $i < 256; $i++)
112 $this->_sTable[$i] = chr($i);
115 $this->_bitsToGet = 9;
119 * Add a new string to the string table.
121 protected function _addStringToTable($oldString, $newString = '')
123 $string = $oldString . $newString;
125 // Add this new String to the table
126 $this->_sTable[$this->_tIdx++] = $string;
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;
138 * Returns the next 9, 10, 11 or 12 bits
142 protected function _getNextCode()
144 if ($this->_bytePointer == $this->_dataLength) {
148 $this->_nextData = ($this->_nextData << 8) | (ord($this->_data[$this->_bytePointer++]) & 0xff);
149 $this->_nextBits += 8;
151 if ($this->_nextBits < $this->_bitsToGet) {
152 $this->_nextData = ($this->_nextData << 8) | (ord($this->_data[$this->_bytePointer++]) & 0xff);
153 $this->_nextBits += 8;
156 $code = ($this->_nextData >> ($this->_nextBits - $this->_bitsToGet)) & $this->_andTable[$this->_bitsToGet-9];
157 $this->_nextBits -= $this->_bitsToGet;
167 * @throws LogicException
169 public function encode($in)
171 throw new LogicException("LZW encoding not implemented.");