PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/library/html2pdf/fpdi/filters/FilterASCII85.php

https://bitbucket.org/astawiarski/openemr
PHP | 101 lines | 71 code | 13 blank | 17 comment | 28 complexity | af0a3a7c79322bd04c29f37d5ad7015f MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MPL-2.0
  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 (!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. if (!class_exists('FilterASCII85', false)) {
  28. class FilterASCII85 {
  29. function error($msg) {
  30. die($msg);
  31. }
  32. function decode($in) {
  33. $out = '';
  34. $state = 0;
  35. $chn = null;
  36. $l = strlen($in);
  37. for ($k = 0; $k < $l; ++$k) {
  38. $ch = ord($in[$k]) & 0xff;
  39. if ($ch == ORD_tilde) {
  40. break;
  41. }
  42. if (preg_match('/^\s$/',chr($ch))) {
  43. continue;
  44. }
  45. if ($ch == ORD_z && $state == 0) {
  46. $out .= chr(0) . chr(0) . chr(0) . chr(0);
  47. continue;
  48. }
  49. if ($ch < ORD_exclmark || $ch > ORD_u) {
  50. return $this->error('Illegal character in ASCII85Decode.');
  51. }
  52. $chn[$state++] = $ch - ORD_exclmark;
  53. if ($state == 5) {
  54. $state = 0;
  55. $r = 0;
  56. for ($j = 0; $j < 5; ++$j)
  57. $r = $r * 85 + $chn[$j];
  58. $out .= chr($r >> 24);
  59. $out .= chr($r >> 16);
  60. $out .= chr($r >> 8);
  61. $out .= chr($r);
  62. }
  63. }
  64. $r = 0;
  65. if ($state == 1)
  66. return $this->error('Illegal length in ASCII85Decode.');
  67. if ($state == 2) {
  68. $r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1]+1) * 85 * 85 * 85;
  69. $out .= chr($r >> 24);
  70. }
  71. else if ($state == 3) {
  72. $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + ($chn[2]+1) * 85 * 85;
  73. $out .= chr($r >> 24);
  74. $out .= chr($r >> 16);
  75. }
  76. else if ($state == 4) {
  77. $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + $chn[2] * 85 * 85 + ($chn[3]+1) * 85 ;
  78. $out .= chr($r >> 24);
  79. $out .= chr($r >> 16);
  80. $out .= chr($r >> 8);
  81. }
  82. return $out;
  83. }
  84. function encode($in) {
  85. return $this->error("ASCII85 encoding not implemented.");
  86. }
  87. }
  88. }