PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/include/fpdi/filters/FilterASCII85.php

https://bitbucket.org/ite/on-track-code-base
PHP | 98 lines | 69 code | 12 blank | 17 comment | 27 complexity | f02caf5a17552956eee4c0968ee95130 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. if (!defined('ORD_z'))
  20. define('ORD_z',ord('z'));
  21. if (!defined('ORD_exclmark'))
  22. define('ORD_exclmark', ord('!'));
  23. if (!defined('ORD_u'))
  24. define('ORD_u', ord('u'));
  25. if (!defined('ORD_tilde'))
  26. define('ORD_tilde', ord('~'));
  27. class FilterASCII85 {
  28. function error($msg) {
  29. die($msg);
  30. }
  31. function decode($in) {
  32. $out = '';
  33. $state = 0;
  34. $chn = null;
  35. $l = strlen($in);
  36. for ($k = 0; $k < $l; ++$k) {
  37. $ch = ord($in[$k]) & 0xff;
  38. if ($ch == ORD_tilde) {
  39. break;
  40. }
  41. if (preg_match('/^\s$/',chr($ch))) {
  42. continue;
  43. }
  44. if ($ch == ORD_z && $state == 0) {
  45. $out .= chr(0).chr(0).chr(0).chr(0);
  46. continue;
  47. }
  48. if ($ch < ORD_exclmark || $ch > ORD_u) {
  49. $this->error('Illegal character in ASCII85Decode.');
  50. }
  51. $chn[$state++] = $ch - ORD_exclmark;
  52. if ($state == 5) {
  53. $state = 0;
  54. $r = 0;
  55. for ($j = 0; $j < 5; ++$j)
  56. $r = $r * 85 + $chn[$j];
  57. $out .= chr($r >> 24);
  58. $out .= chr($r >> 16);
  59. $out .= chr($r >> 8);
  60. $out .= chr($r);
  61. }
  62. }
  63. $r = 0;
  64. if ($state == 1)
  65. $this->error('Illegal length in ASCII85Decode.');
  66. if ($state == 2) {
  67. $r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1]+1) * 85 * 85 * 85;
  68. $out .= chr($r >> 24);
  69. }
  70. else if ($state == 3) {
  71. $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + ($chn[2]+1) * 85 * 85;
  72. $out .= chr($r >> 24);
  73. $out .= chr($r >> 16);
  74. }
  75. else if ($state == 4) {
  76. $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + $chn[2] * 85 * 85 + ($chn[3]+1) * 85 ;
  77. $out .= chr($r >> 24);
  78. $out .= chr($r >> 16);
  79. $out .= chr($r >> 8);
  80. }
  81. return $out;
  82. }
  83. function encode($in) {
  84. $this->error("ASCII85 encoding not implemented.");
  85. }
  86. }