source-class-Com.Tecnick.Pdf.Filter.Type.Lzw

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * Lzw.php
  4:  *
  5:  * @since       2011-05-23
  6:  * @category    Library
  7:  * @package     PdfFilter
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-pdf-filter
 12:  *
 13:  * This file is part of tc-lib-pdf-filter software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Pdf\Filter\Type;
 17: 
 18: use \Com\Tecnick\Pdf\Filter\Exception as PPException;
 19: 
 20: /**
 21:  * Com\Tecnick\Pdf\Filter\Type\Lzw
 22:  *
 23:  * LZWDecode
 24:  * Decompresses data encoded using the LZW (Lempel-Ziv-Welch) adaptive compression method,
 25:  * reproducing the original text or binary data.
 26:  *
 27:  * @since       2011-05-23
 28:  * @category    Library
 29:  * @package     PdfFilter
 30:  * @author      Nicola Asuni <info@tecnick.com>
 31:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 32:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 33:  * @link        https://github.com/tecnickcom/tc-lib-pdf-filter
 34:  */
 35: class Lzw
 36: {
 37:     /**
 38:      * Decode the data
 39:      *
 40:      * @param string $data Data to decode.
 41:      *
 42:      * @return string Decoded data string.
 43:      */
 44:     public function decode($data)
 45:     {
 46:         // initialize string to return
 47:         $decoded = '';
 48:         // data length
 49:         $data_length = strlen($data);
 50:         // convert string to binary string
 51:         $bitstring = '';
 52:         for ($i = 0; $i < $data_length; ++$i) {
 53:             $bitstring .= sprintf('%08b', ord($data[$i]));
 54:         }
 55:         // get the number of bits
 56:         $data_length = strlen($bitstring);
 57:         // initialize code length in bits
 58:         $bitlen = 9;
 59:         // initialize dictionary index
 60:         $dix = 258;
 61:         // initialize the dictionary (with the first 256 entries).
 62:         $dictionary = array();
 63:         for ($i = 0; $i < 256; ++$i) {
 64:             $dictionary[$i] = chr($i);
 65:         }
 66:         // previous val
 67:         $prev_index = 0;
 68:         // while we encounter EOD marker (257), read code_length bits
 69:         while (($data_length > 0) && (($index = bindec(substr($bitstring, 0, $bitlen))) != 257)) {
 70:             $this->process($decoded, $bitstring, $bitlen, $data_length, $index, $dictionary, $dix, $prev_index);
 71:         }
 72:         return $decoded;
 73:     }
 74: 
 75:     /**
 76:      * Internal processing
 77:      *
 78:      * @param string $decoded
 79:      * @param string $bitstring
 80:      * @param int    $bitlen
 81:      * @param int    $data_length
 82:      * @param int    $index
 83:      * @param array  $dictionary
 84:      * @param int    $dix
 85:      * @param int    $prev_index
 86:      */
 87:     protected function process(
 88:         &$decoded,
 89:         &$bitstring,
 90:         &$bitlen,
 91:         &$data_length,
 92:         &$index,
 93:         &$dictionary,
 94:         &$dix,
 95:         &$prev_index
 96:     ) {
 97:         // remove read bits from string
 98:         $bitstring = substr($bitstring, $bitlen);
 99:         // update number of bits
100:         $data_length -= $bitlen;
101:         if ($index == 256) { // clear-table marker
102:             // reset code length in bits
103:             $bitlen = 9;
104:             // reset dictionary index
105:             $dix = 258;
106:             $prev_index = 256;
107:             // reset the dictionary (with the first 256 entries).
108:             $dictionary = array();
109:             for ($i = 0; $i < 256; ++$i) {
110:                 $dictionary[$i] = chr($i);
111:             }
112:         } elseif ($prev_index == 256) {
113:             // first entry
114:             $decoded .= $dictionary[$index];
115:             $prev_index = $index;
116:         } else {
117:             // check if index exist in the dictionary
118:             if ($index < $dix) {
119:                 // index exist on dictionary
120:                 $decoded .= $dictionary[$index];
121:                 $dic_val = $dictionary[$prev_index].$dictionary[$index][0];
122:                 // store current index
123:                 $prev_index = $index;
124:             } else {
125:                 // index do not exist on dictionary
126:                 $dic_val = $dictionary[$prev_index].$dictionary[$prev_index][0];
127:                 $decoded .= $dic_val;
128:             }
129:             // update dictionary
130:             $dictionary[$dix] = $dic_val;
131:             ++$dix;
132:             // change bit length by case
133:             if ($dix == 2047) {
134:                 $bitlen = 12;
135:             } elseif ($dix == 1023) {
136:                 $bitlen = 11;
137:             } elseif ($dix == 511) {
138:                 $bitlen = 10;
139:             }
140:         }
141:     }
142: }
143: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy