/framework/vendor/zend/Zend/Pdf/Filter/Ascii85.php

http://zoop.googlecode.com/ · PHP · 181 lines · 91 code · 32 blank · 58 comment · 18 complexity · 1f16d049fa2b4df656813b9e493b236e 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Ascii85.php 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /** Zend_Pdf_Filter_Interface */
  22. require_once 'Zend/Pdf/Filter/Interface.php';
  23. /**
  24. * ASCII85 stream filter
  25. *
  26. * @package Zend_Pdf
  27. * @copyright Copyright (c) 2005-2010 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_Ascii85 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. $output = '';
  43. $dataLength = strlen($data);
  44. for ($i = 0; $i < $dataLength; $i += 4) {
  45. //convert the 4 characters into a 32-bit number
  46. $chunk = substr($data, $i, 4);
  47. if (strlen($chunk) < 4) {
  48. //partial chunk
  49. break;
  50. }
  51. $b = unpack("N", $chunk);
  52. $b = $b[1];
  53. //special char for all 4 bytes = 0
  54. if ($b == 0) {
  55. $output .= 'z';
  56. continue;
  57. }
  58. //encode into 5 bytes
  59. for ($j = 4; $j >= 0; $j--) {
  60. $foo = (int) (($b / pow(85,$j)) + 33);
  61. $b %= pow(85,$j);
  62. $output .= chr($foo);
  63. }
  64. }
  65. //encode partial chunk
  66. if ($i < $dataLength) {
  67. $n = $dataLength - $i;
  68. $chunk = substr($data, -$n);
  69. //0 pad the rest
  70. for ($j = $n;$j < 4;$j++) {
  71. $chunk .= chr(0);
  72. }
  73. $b = unpack("N", $chunk);
  74. $b = $b[1];
  75. //encode just $n + 1
  76. for ($j = 4; $j >= (4 - $n); $j--) {
  77. $foo = (int) (($b / pow(85,$j)) + 33);
  78. $b %= pow(85,$j);
  79. $output .= chr($foo);
  80. }
  81. }
  82. //EOD
  83. $output .= '~>';
  84. //make sure lines are split
  85. $output = chunk_split($output, 76, "\n");
  86. //get rid of new line at the end
  87. $output = substr($output, 0, -1);
  88. return $output;
  89. }
  90. /**
  91. * Decode data
  92. *
  93. * @param string $data
  94. * @param array $params
  95. * @return string
  96. * @throws Zend_Pdf_Exception
  97. */
  98. public static function decode($data, $params = null)
  99. {
  100. $output = '';
  101. //get rid of the whitespaces
  102. $whiteSpace = array("\x00", "\x09", "\x0A", "\x0C", "\x0D", "\x20");
  103. $data = str_replace($whiteSpace, '', $data);
  104. if (substr($data, -2) != '~>') {
  105. require_once 'Zend/Pdf/Exception.php';
  106. throw new Zend_Pdf_Exception('Invalid EOF marker');
  107. return '';
  108. }
  109. $data = substr($data, 0, (strlen($data) - 2));
  110. $dataLength = strlen($data);
  111. for ($i = 0; $i < $dataLength; $i += 5) {
  112. $b = 0;
  113. if (substr($data, $i, 1) == "z") {
  114. $i -= 4;
  115. $output .= pack("N", 0);
  116. next;
  117. }
  118. $c = substr($data, $i, 5);
  119. if(strlen($c) < 5) {
  120. //partial chunk
  121. break;
  122. }
  123. $c = unpack('C5', $c);
  124. $value = 0;
  125. for ($j = 1; $j <= 5; $j++) {
  126. $value += (($c[$j] - 33) * pow(85, (5 - $j)));
  127. }
  128. $output .= pack("N", $value);
  129. }
  130. //decode partial
  131. if ($i < $dataLength) {
  132. $value = 0;
  133. $chunk = substr($data, $i);
  134. $partialLength = strlen($chunk);
  135. //pad the rest of the chunk with u's
  136. //until the lenght of the chunk is 5
  137. for ($j = 0; $j < (5 - $partialLength); $j++) {
  138. $chunk .= 'u';
  139. }
  140. $c = unpack('C5', $chunk);
  141. for ($j = 1; $j <= 5; $j++) {
  142. $value += (($c[$j] - 33) * pow(85, (5 - $j)));
  143. }
  144. $foo = pack("N", $value);
  145. $output .= substr($foo, 0, ($partialLength - 1));
  146. }
  147. return $output;
  148. }
  149. }