PageRenderTime 48ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/include/fpdi/filters/FilterLZW.php

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