PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Pdf/Filter/AsciiHex.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 132 lines | 60 code | 16 blank | 56 comment | 23 complexity | 1ab5eabd5ca13ab63643055a32cb9e4b MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @package Zend_Pdf
  16. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. /** Zend_Pdf_Filter_Interface */
  20. require_once 'Zend/Pdf/Filter/Interface.php';
  21. /**
  22. * AsciiHex stream filter
  23. *
  24. * @package Zend_Pdf
  25. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class Zend_Pdf_Filter_AsciiHex implements Zend_Pdf_Filter_Interface
  29. {
  30. /**
  31. * Encode data
  32. *
  33. * @param string $data
  34. * @param array $params
  35. * @return string
  36. * @throws Zend_Pdf_Exception
  37. */
  38. public static function encode($data, $params = null)
  39. {
  40. return bin2hex($data) . '>';
  41. }
  42. /**
  43. * Decode data
  44. *
  45. * @param string $data
  46. * @param array $params
  47. * @return string
  48. * @throws Zend_Pdf_Exception
  49. */
  50. public static function decode($data, $params = null)
  51. {
  52. $output = '';
  53. $oddCode = true;
  54. $commentMode = false;
  55. for ($count = 0; $count < strlen($data) && $data[$count] != '>'; $count++) {
  56. $charCode = ord($data[$count]);
  57. if ($commentMode) {
  58. if ($charCode == 0x0A || $charCode == 0x0D ) {
  59. $commentMode = false;
  60. }
  61. continue;
  62. }
  63. switch ($charCode) {
  64. //Skip white space
  65. case 0x00: // null character
  66. // fall through to next case
  67. case 0x09: // Tab
  68. // fall through to next case
  69. case 0x0A: // Line feed
  70. // fall through to next case
  71. case 0x0C: // Form Feed
  72. // fall through to next case
  73. case 0x0D: // Carriage return
  74. // fall through to next case
  75. case 0x20: // Space
  76. // Do nothing
  77. break;
  78. case 0x25: // '%'
  79. // Switch to comment mode
  80. $commentMode = true;
  81. break;
  82. default:
  83. if ($charCode >= 0x30 /*'0'*/ && $charCode <= 0x39 /*'9'*/) {
  84. $code = $charCode - 0x30;
  85. } else if ($charCode >= 0x41 /*'A'*/ && $charCode <= 0x46 /*'F'*/) {
  86. $code = $charCode - 0x37/*0x41 - 0x0A*/;
  87. } else if ($charCode >= 0x61 /*'a'*/ && $charCode <= 0x66 /*'f'*/) {
  88. $code = $charCode - 0x57/*0x61 - 0x0A*/;
  89. } else {
  90. throw new Zend_Pdf_Exception('Wrong character in a encoded stream');
  91. }
  92. if ($oddCode) {
  93. // Odd pass. Store hex digit for next pass
  94. // Scope of $hexCodeHigh variable is whole function
  95. $hexCodeHigh = $code;
  96. } else {
  97. // Even pass.
  98. // Add decoded character to the output
  99. // ($hexCodeHigh is stored in previous pass)
  100. $output .= chr($hexCodeHigh*16 + $code);
  101. }
  102. $oddCode = !$oddCode;
  103. break;
  104. }
  105. }
  106. /* Check that stream is terminated by End Of Data marker */
  107. if ($data[$count] != '>') {
  108. throw new Zend_Pdf_Exception('Wrong encoded stream End Of Data marker.');
  109. }
  110. /* Last '0' character is omitted */
  111. if (!$oddCode) {
  112. $output .= chr($hexCodeHigh*16);
  113. }
  114. return $output;
  115. }
  116. }