PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/fabiancarlos/feature_seguimentos
PHP | 135 lines | 62 code | 15 blank | 58 comment | 23 complexity | 55b913a4924f880c3556d60f07b948e1 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. * @category Zend
  16. * @package Zend_Pdf
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: AsciiHex.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /** Zend_Pdf_Filter_Interface */
  22. require_once 'Zend/Pdf/Filter/Interface.php';
  23. /**
  24. * AsciiHex stream filter
  25. *
  26. * @package Zend_Pdf
  27. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Pdf_Filter_AsciiHex implements Zend_Pdf_Filter_Interface
  31. {
  32. /**
  33. * Encode data
  34. *
  35. * @param string $data
  36. * @param array $params
  37. * @return string
  38. * @throws Zend_Pdf_Exception
  39. */
  40. public static function encode($data, $params = null)
  41. {
  42. return bin2hex($data) . '>';
  43. }
  44. /**
  45. * Decode data
  46. *
  47. * @param string $data
  48. * @param array $params
  49. * @return string
  50. * @throws Zend_Pdf_Exception
  51. */
  52. public static function decode($data, $params = null)
  53. {
  54. $output = '';
  55. $oddCode = true;
  56. $commentMode = false;
  57. for ($count = 0; $count < strlen($data) && $data[$count] != '>'; $count++) {
  58. $charCode = ord($data[$count]);
  59. if ($commentMode) {
  60. if ($charCode == 0x0A || $charCode == 0x0D ) {
  61. $commentMode = false;
  62. }
  63. continue;
  64. }
  65. switch ($charCode) {
  66. //Skip white space
  67. case 0x00: // null character
  68. // fall through to next case
  69. case 0x09: // Tab
  70. // fall through to next case
  71. case 0x0A: // Line feed
  72. // fall through to next case
  73. case 0x0C: // Form Feed
  74. // fall through to next case
  75. case 0x0D: // Carriage return
  76. // fall through to next case
  77. case 0x20: // Space
  78. // Do nothing
  79. break;
  80. case 0x25: // '%'
  81. // Switch to comment mode
  82. $commentMode = true;
  83. break;
  84. default:
  85. if ($charCode >= 0x30 /*'0'*/ && $charCode <= 0x39 /*'9'*/) {
  86. $code = $charCode - 0x30;
  87. } else if ($charCode >= 0x41 /*'A'*/ && $charCode <= 0x46 /*'F'*/) {
  88. $code = $charCode - 0x37/*0x41 - 0x0A*/;
  89. } else if ($charCode >= 0x61 /*'a'*/ && $charCode <= 0x66 /*'f'*/) {
  90. $code = $charCode - 0x57/*0x61 - 0x0A*/;
  91. } else {
  92. require_once 'Zend/Pdf/Exception.php';
  93. throw new Zend_Pdf_Exception('Wrong character in a encoded stream');
  94. }
  95. if ($oddCode) {
  96. // Odd pass. Store hex digit for next pass
  97. // Scope of $hexCodeHigh variable is whole function
  98. $hexCodeHigh = $code;
  99. } else {
  100. // Even pass.
  101. // Add decoded character to the output
  102. // ($hexCodeHigh is stored in previous pass)
  103. $output .= chr($hexCodeHigh*16 + $code);
  104. }
  105. $oddCode = !$oddCode;
  106. break;
  107. }
  108. }
  109. /* Check that stream is terminated by End Of Data marker */
  110. if ($data[$count] != '>') {
  111. require_once 'Zend/Pdf/Exception.php';
  112. throw new Zend_Pdf_Exception('Wrong encoded stream End Of Data marker.');
  113. }
  114. /* Last '0' character is omitted */
  115. if (!$oddCode) {
  116. $output .= chr($hexCodeHigh*16);
  117. }
  118. return $output;
  119. }
  120. }