FilterLZW

Class FilterLZW.

Defined (1)

The class is defined in the following location(s).

/vendor/setasign/fpdi/filters/FilterLZW.php  
  1. class FilterLZW 
  2. protected $_sTable = array(); 
  3. protected $_data = null; 
  4. protected $_dataLength = 0; 
  5. protected $_tIdx; 
  6. protected $_bitsToGet = 9; 
  7. protected $_bytePointer; 
  8. protected $_bitPointer; 
  9. protected $_nextData = 0; 
  10. protected $_nextBits = 0; 
  11. protected $_andTable = array(511, 1023, 2047, 4095); 
  12.  
  13. /** 
  14. * Decodes LZW compressed data. 
  15. * @param string $data The compressed data. 
  16. * @throws Exception 
  17. * @return string 
  18. */ 
  19. public function decode($data) 
  20. if ($data[0] == 0x00 && $data[1] == 0x01) { 
  21. throw new Exception('LZW flavour not supported.'); 
  22.  
  23. $this->_initsTable(); 
  24.  
  25. $this->_data = $data; 
  26. $this->_dataLength = strlen($data); 
  27.  
  28. // Initialize pointers 
  29. $this->_bytePointer = 0; 
  30. $this->_bitPointer = 0; 
  31.  
  32. $this->_nextData = 0; 
  33. $this->_nextBits = 0; 
  34.  
  35. $oldCode = 0; 
  36.  
  37. $unCompData = ''; 
  38.  
  39. while (($code = $this->_getNextCode()) != 257) { 
  40. if ($code == 256) { 
  41. $this->_initsTable(); 
  42. $code = $this->_getNextCode(); 
  43.  
  44. if ($code == 257) { 
  45. break; 
  46.  
  47. if (!isset($this->_sTable[$code])) { 
  48. throw new Exception('Error while decompression LZW compressed data.'); 
  49.  
  50. $unCompData .= $this->_sTable[$code]; 
  51. $oldCode = $code; 
  52.  
  53. } else { 
  54.  
  55. if ($code < $this->_tIdx) { 
  56. $string = $this->_sTable[$code]; 
  57. $unCompData .= $string; 
  58.  
  59. $this->_addStringToTable($this->_sTable[$oldCode], $string[0]); 
  60. $oldCode = $code; 
  61. } else { 
  62. $string = $this->_sTable[$oldCode]; 
  63. $string = $string . $string[0]; 
  64. $unCompData .= $string; 
  65.  
  66. $this->_addStringToTable($string); 
  67. $oldCode = $code; 
  68.  
  69. return $unCompData; 
  70.  
  71.  
  72. /** 
  73. * Initialize the string table. 
  74. */ 
  75. protected function _initsTable() 
  76. $this->_sTable = array(); 
  77.  
  78. for ($i = 0; $i < 256; $i++) 
  79. $this->_sTable[$i] = chr($i); 
  80.  
  81. $this->_tIdx = 258; 
  82. $this->_bitsToGet = 9; 
  83.  
  84. /** 
  85. * Add a new string to the string table. 
  86. */ 
  87. protected function _addStringToTable($oldString, $newString = '') 
  88. $string = $oldString . $newString; 
  89.  
  90. // Add this new String to the table 
  91. $this->_sTable[$this->_tIdx++] = $string; 
  92.  
  93. if ($this->_tIdx == 511) { 
  94. $this->_bitsToGet = 10; 
  95. } else if ($this->_tIdx == 1023) { 
  96. $this->_bitsToGet = 11; 
  97. } else if ($this->_tIdx == 2047) { 
  98. $this->_bitsToGet = 12; 
  99.  
  100. /** 
  101. * Returns the next 9, 10, 11 or 12 bits 
  102. * @return int 
  103. */ 
  104. protected function _getNextCode() 
  105. if ($this->_bytePointer == $this->_dataLength) { 
  106. return 257; 
  107.  
  108. $this->_nextData = ($this->_nextData << 8) | (ord($this->_data[$this->_bytePointer++]) & 0xff); 
  109. $this->_nextBits += 8; 
  110.  
  111. if ($this->_nextBits < $this->_bitsToGet) { 
  112. $this->_nextData = ($this->_nextData << 8) | (ord($this->_data[$this->_bytePointer++]) & 0xff); 
  113. $this->_nextBits += 8; 
  114.  
  115. $code = ($this->_nextData >> ($this->_nextBits - $this->_bitsToGet)) & $this->_andTable[$this->_bitsToGet-9]; 
  116. $this->_nextBits -= $this->_bitsToGet; 
  117.  
  118. return $code; 
  119.  
  120. /** 
  121. * NOT IMPLEMENTED 
  122. * @param string $in 
  123. * @return string 
  124. * @throws LogicException 
  125. */ 
  126. public function encode($in) 
  127. throw new LogicException("LZW encoding not implemented.");