3 // FPDI - Version 1.2.1
5 // Copyright 2004-2008 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.
22 var $sTable = array();
30 var $andTable = array(511, 1023, 2047, 4095);
32 function __construct(&$fpdi) {
37 * Method to decode LZW compressed data.
39 * @param string data The compressed data.
41 function decode($data) {
43 if($data[0] == 0x00 && $data[1] == 0x01) {
44 $this->fpdi->error("LZW flavour not supported.");
51 // Initialize pointers
52 $this->bytePointer = 0;
53 $this->bitPointer = 0;
63 while (($code = $this->getNextCode()) != 257) {
66 $code = $this->getNextCode();
72 $uncompData .= $this->sTable[$code];
77 if ($code < $this->tIdx) {
78 $string = $this->sTable[$code];
79 $uncompData .= $string;
81 $this->addStringToTable($this->sTable[$oldCode], $string[0]);
84 $string = $this->sTable[$oldCode];
85 $string = $string.$string[0];
86 $uncompData .= $string;
88 $this->addStringToTable($string);
99 * Initialize the string table.
101 function initsTable() {
102 $this->sTable = array();
104 for ($i = 0; $i < 256; $i++)
105 $this->sTable[$i] = chr($i);
108 $this->bitsToGet = 9;
112 * Add a new string to the string table.
114 function addStringToTable ($oldString, $newString="") {
115 $string = $oldString.$newString;
117 // Add this new String to the table
118 $this->sTable[$this->tIdx++] = $string;
120 if ($this->tIdx == 511) {
121 $this->bitsToGet = 10;
122 } else if ($this->tIdx == 1023) {
123 $this->bitsToGet = 11;
124 } else if ($this->tIdx == 2047) {
125 $this->bitsToGet = 12;
129 // Returns the next 9, 10, 11 or 12 bits
130 function getNextCode() {
131 if ($this->bytePointer == strlen($this->data))
134 $this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
135 $this->nextBits += 8;
137 if ($this->nextBits < $this->bitsToGet) {
138 $this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
139 $this->nextBits += 8;
142 $code = ($this->nextData >> ($this->nextBits - $this->bitsToGet)) & $this->andTable[$this->bitsToGet-9];
143 $this->nextBits -= $this->bitsToGet;