Lzw.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package Fpdi
  6. * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. */
  9. namespace Fpdi\PdfParser\Filter;
  10. /**
  11. * Class for handling LZW encoded data
  12. */
  13. class Lzw implements FilterInterface
  14. {
  15. /**
  16. * @var null|string
  17. */
  18. protected $data;
  19. /**
  20. * @var array
  21. */
  22. protected $sTable = [];
  23. /**
  24. * @var int
  25. */
  26. protected $dataLength = 0;
  27. /**
  28. * @var int
  29. */
  30. protected $tIdx;
  31. /**
  32. * @var int
  33. */
  34. protected $bitsToGet = 9;
  35. /**
  36. * @var int
  37. */
  38. protected $bytePointer;
  39. /**
  40. * @var int
  41. */
  42. protected $nextData = 0;
  43. /**
  44. * @var int
  45. */
  46. protected $nextBits = 0;
  47. /**
  48. * @var array
  49. */
  50. protected $andTable = [511, 1023, 2047, 4095];
  51. /**
  52. * Method to decode LZW compressed data.
  53. *
  54. * @param string $data The compressed data
  55. * @return string The uncompressed data
  56. * @throws LzwException
  57. */
  58. public function decode($data)
  59. {
  60. if ($data[0] === "\x00" && $data[1] === "\x01") {
  61. throw new LzwException(
  62. 'LZW flavour not supported.',
  63. LzwException::LZW_FLAVOUR_NOT_SUPPORTED
  64. );
  65. }
  66. $this->initsTable();
  67. $this->data = $data;
  68. $this->dataLength = \strlen($data);
  69. // Initialize pointers
  70. $this->bytePointer = 0;
  71. $this->nextData = 0;
  72. $this->nextBits = 0;
  73. $oldCode = 0;
  74. $uncompData = '';
  75. while (($code = $this->getNextCode()) !== 257) {
  76. if ($code === 256) {
  77. $this->initsTable();
  78. $code = $this->getNextCode();
  79. if ($code === 257) {
  80. break;
  81. }
  82. $uncompData .= $this->sTable[$code];
  83. $oldCode = $code;
  84. } else {
  85. if ($code < $this->tIdx) {
  86. $string = $this->sTable[$code];
  87. $uncompData .= $string;
  88. $this->addStringToTable($this->sTable[$oldCode], $string[0]);
  89. $oldCode = $code;
  90. } else {
  91. $string = $this->sTable[$oldCode];
  92. $string .= $string[0];
  93. $uncompData .= $string;
  94. $this->addStringToTable($string);
  95. $oldCode = $code;
  96. }
  97. }
  98. }
  99. return $uncompData;
  100. }
  101. /**
  102. * Initialize the string table.
  103. */
  104. protected function initsTable()
  105. {
  106. $this->sTable = [];
  107. for ($i = 0; $i < 256; $i++) {
  108. $this->sTable[$i] = \chr($i);
  109. }
  110. $this->tIdx = 258;
  111. $this->bitsToGet = 9;
  112. }
  113. /**
  114. * Add a new string to the string table.
  115. *
  116. * @param string $oldString
  117. * @param string $newString
  118. */
  119. protected function addStringToTable($oldString, $newString = '')
  120. {
  121. $string = $oldString . $newString;
  122. // Add this new String to the table
  123. $this->sTable[$this->tIdx++] = $string;
  124. if ($this->tIdx === 511) {
  125. $this->bitsToGet = 10;
  126. } elseif ($this->tIdx === 1023) {
  127. $this->bitsToGet = 11;
  128. } elseif ($this->tIdx === 2047) {
  129. $this->bitsToGet = 12;
  130. }
  131. }
  132. /**
  133. * Returns the next 9, 10, 11 or 12 bits.
  134. *
  135. * @return integer
  136. */
  137. protected function getNextCode()
  138. {
  139. if ($this->bytePointer === $this->dataLength) {
  140. return 257;
  141. }
  142. $this->nextData = ($this->nextData << 8) | (\ord($this->data[$this->bytePointer++]) & 0xff);
  143. $this->nextBits += 8;
  144. if ($this->nextBits < $this->bitsToGet) {
  145. $this->nextData = ($this->nextData << 8) | (\ord($this->data[$this->bytePointer++]) & 0xff);
  146. $this->nextBits += 8;
  147. }
  148. $code = ($this->nextData >> ($this->nextBits - $this->bitsToGet)) & $this->andTable[$this->bitsToGet - 9];
  149. $this->nextBits -= $this->bitsToGet;
  150. return $code;
  151. }
  152. }