PageRenderTime 63ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/fpdfi/filters/FilterLZW.php

https://bitbucket.org/speedealing/speedealing
PHP | 157 lines | 93 code | 33 blank | 31 comment | 26 complexity | fa772f98c3ab7837d92df74b7b808226 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. //
  3. // FPDI - Version 1.4.2
  4. //
  5. // Copyright 2004-2011 Setasign - Jan Slabon
  6. //
  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
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  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.
  18. //
  19. if (!class_exists('FilterLZW', false)) {
  20. class FilterLZW {
  21. var $sTable = array();
  22. var $data = null;
  23. var $dataLength = 0;
  24. var $tIdx;
  25. var $bitsToGet = 9;
  26. var $bytePointer;
  27. var $bitPointer;
  28. var $nextData = 0;
  29. var $nextBits = 0;
  30. var $andTable = array(511, 1023, 2047, 4095);
  31. function error($msg) {
  32. die($msg);
  33. }
  34. /**
  35. * Method to decode LZW compressed data.
  36. *
  37. * @param string data The compressed data.
  38. */
  39. function decode($data) {
  40. if($data[0] == 0x00 && $data[1] == 0x01) {
  41. $this->error('LZW flavour not supported.');
  42. }
  43. $this->initsTable();
  44. $this->data = $data;
  45. $this->dataLength = strlen($data);
  46. // Initialize pointers
  47. $this->bytePointer = 0;
  48. $this->bitPointer = 0;
  49. $this->nextData = 0;
  50. $this->nextBits = 0;
  51. $oldCode = 0;
  52. $string = '';
  53. $uncompData = '';
  54. while (($code = $this->getNextCode()) != 257) {
  55. if ($code == 256) {
  56. $this->initsTable();
  57. $code = $this->getNextCode();
  58. if ($code == 257) {
  59. break;
  60. }
  61. $uncompData .= $this->sTable[$code];
  62. $oldCode = $code;
  63. } else {
  64. if ($code < $this->tIdx) {
  65. $string = $this->sTable[$code];
  66. $uncompData .= $string;
  67. $this->addStringToTable($this->sTable[$oldCode], $string[0]);
  68. $oldCode = $code;
  69. } else {
  70. $string = $this->sTable[$oldCode];
  71. $string = $string . $string[0];
  72. $uncompData .= $string;
  73. $this->addStringToTable($string);
  74. $oldCode = $code;
  75. }
  76. }
  77. }
  78. return $uncompData;
  79. }
  80. /**
  81. * Initialize the string table.
  82. */
  83. function initsTable() {
  84. $this->sTable = array();
  85. for ($i = 0; $i < 256; $i++)
  86. $this->sTable[$i] = chr($i);
  87. $this->tIdx = 258;
  88. $this->bitsToGet = 9;
  89. }
  90. /**
  91. * Add a new string to the string table.
  92. */
  93. function addStringToTable ($oldString, $newString='') {
  94. $string = $oldString.$newString;
  95. // Add this new String to the table
  96. $this->sTable[$this->tIdx++] = $string;
  97. if ($this->tIdx == 511) {
  98. $this->bitsToGet = 10;
  99. } else if ($this->tIdx == 1023) {
  100. $this->bitsToGet = 11;
  101. } else if ($this->tIdx == 2047) {
  102. $this->bitsToGet = 12;
  103. }
  104. }
  105. // Returns the next 9, 10, 11 or 12 bits
  106. function getNextCode() {
  107. if ($this->bytePointer == $this->dataLength) {
  108. return 257;
  109. }
  110. $this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
  111. $this->nextBits += 8;
  112. if ($this->nextBits < $this->bitsToGet) {
  113. $this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
  114. $this->nextBits += 8;
  115. }
  116. $code = ($this->nextData >> ($this->nextBits - $this->bitsToGet)) & $this->andTable[$this->bitsToGet-9];
  117. $this->nextBits -= $this->bitsToGet;
  118. return $code;
  119. }
  120. function encode($in) {
  121. $this->error("LZW encoding not implemented.");
  122. }
  123. }
  124. }